From 1c046fba38f1450c108a809a3d7d2ec6a3001de3 Mon Sep 17 00:00:00 2001 From: Tom Naccarato <108072398+tnaccarato@users.noreply.github.com> Date: Mon, 15 Apr 2024 21:52:06 +0100 Subject: [PATCH] Add timestamp to homepage --- payapp/models.py | 27 ++++++-------------------- payapp/views.py | 10 +++++++++- templates/payapp/home.html | 3 ++- timestamp_server/timestamp_client.py | 28 +++++++++++++++++++++++++++ webapps.db | Bin 184320 -> 184320 bytes 5 files changed, 45 insertions(+), 23 deletions(-) create mode 100644 timestamp_server/timestamp_client.py diff --git a/payapp/models.py b/payapp/models.py index 6038015..156fcdd 100644 --- a/payapp/models.py +++ b/payapp/models.py @@ -9,34 +9,19 @@ from thrift.transport import TTransport from thrift.protocol import TBinaryProtocol from gen_py.timestamp_service import TimestampService +from timestamp_server.timestamp_client import ThriftTimestampClient class ThriftTimestampField(models.DateTimeField): """Defines a custom field to store the current timestamp using a Thrift service.""" def pre_save(self, model_instance, add): - # If the field is being added to the model instance and the field is empty, retrieve the current timestamp if add and not getattr(model_instance, self.attname): - # Connect to the Thrift server and retrieve the current timestamp - try: - # Create a Thrift client to connect to the Thrift server - transport = TSocket.TSocket('localhost', 9090) - transport = TTransport.TBufferedTransport(transport) - protocol = TBinaryProtocol.TBinaryProtocol(transport) - client = TimestampService.Client(protocol) - transport.open() # Open the connection to the Thrift server - - timestamp = client.getCurrentTimestamp() # Retrieve the current timestamp from the Thrift server - setattr(model_instance, self.attname, timestamp) # Set the field value to the retrieved timestamp - - transport.close() # Close the connection to the Thrift server - - # Handle any exceptions that occur when connecting to the Thrift server - except Exception as e: - # Log or handle the error as needed - print("An error occurred:", e) - - # Call the parent class method to save the value to the database + client = ThriftTimestampClient() + timestamp = client.get_current_timestamp() + if timestamp is not None: + setattr(model_instance, self.attname, timestamp) + return super().pre_save(model_instance, add) diff --git a/payapp/views.py b/payapp/views.py index a33bc76..3915e47 100644 --- a/payapp/views.py +++ b/payapp/views.py @@ -1,3 +1,5 @@ +from datetime import datetime + from django.contrib import messages from django.contrib.auth.models import User from django.db.models import Q @@ -8,6 +10,7 @@ from payapp.models import Transfer, Account, Request, Notification from webapps2024 import settings from django.db import transaction +from timestamp_server.timestamp_client import ThriftTimestampClient currency_symbols = { 'USD': '$', @@ -43,7 +46,12 @@ def home(request): :param request: :return: """ - return render(request, 'payapp/home.html') + # Gets the current timestamp for the dashboard + timestamp = ThriftTimestampClient().get_current_timestamp() + # Converts the timestamp to a datetime object + timestamp = datetime.strptime(timestamp, '%Y-%m-%d %H:%M:%S') + + return render(request, 'payapp/home.html', {'timestamp': timestamp}) @login_required_message diff --git a/templates/payapp/home.html b/templates/payapp/home.html index 3646ce6..2e12f27 100644 --- a/templates/payapp/home.html +++ b/templates/payapp/home.html @@ -11,7 +11,8 @@ {% if user.is_authenticated %}

Welcome, {{ user.username }}, to the FakePal homepage! - You currently have {{ user.account.currency | currency_symbol}}{{ user.account.balance }} in your account. + You currently have {{ user.account.currency | currency_symbol}}{{ user.account.balance }} in your account.
+ It is currently {{ timestamp | time:"g:i:s a" }} on the {{ timestamp | date:"D,d F, Y" }}.

{% else %}

diff --git a/timestamp_server/timestamp_client.py b/timestamp_server/timestamp_client.py new file mode 100644 index 0000000..458ce96 --- /dev/null +++ b/timestamp_server/timestamp_client.py @@ -0,0 +1,28 @@ +from thrift.transport import TSocket +from thrift.transport import TTransport +from thrift.protocol import TBinaryProtocol +from gen_py.timestamp_service import TimestampService + + +class ThriftTimestampClient: + """Thrift client to fetch the current timestamp from the Thrift server.""" + def __init__(self, host='localhost', port=9090): + """Initialize the Thrift client with the host and port of the Thrift server.""" + self.host = host + self.port = port + + def get_current_timestamp(self): + """Fetch the current timestamp from the Thrift server.""" + try: + transport = TSocket.TSocket(self.host, self.port) + transport = TTransport.TBufferedTransport(transport) + protocol = TBinaryProtocol.TBinaryProtocol(transport) + client = TimestampService.Client(protocol) + transport.open() + timestamp = client.getCurrentTimestamp() + transport.close() + return timestamp + + except Exception as e: + print("An error occurred while fetching the timestamp:", e) + return None diff --git a/webapps.db b/webapps.db index 4a1b42cd130128a9ebd5a2f540697eaa67ca697b..22a245489f3239a8d6ed52ce215182b2b3b749b0 100644 GIT binary patch delta 521 zcmZozz}>KbdxA8h`9v9KM)Qpc`|@~9EUgU9^h`~ROe~EzYvtcE;9};V#K1p^e>?x3 z&3p@5_`Ot^eHjhYi;RuRl1nm-^72!RlgkWJD@_cODl)4~4YJbEkMy^#wk>*(zMg~SEx&|h?MwSXj239Z+o0}S#G$}EGttI4gy;QG?uq;%#NI>x{VC2OilDm%#2LT%>d{ynP&h1 delta 127 zcmZozz}>KbdxA8h;Y1l{M#GH>`|@~<46RH}^h``GOw0{8YvtcE;9}&z!vI8T3=Et3 z7PRnB4iNBazO#P&o%M_w3)mR>ZZq)R-mIu_k#GC$I3~?RHb(wO4E&Ea3r@JfFDJ|F b$Z2F?WTI