Skip to content

Commit

Permalink
Add timestamp to homepage
Browse files Browse the repository at this point in the history
  • Loading branch information
tnaccarato committed Apr 15, 2024
1 parent 4f260e0 commit 1c046fb
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 23 deletions.
27 changes: 6 additions & 21 deletions payapp/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
10 changes: 9 additions & 1 deletion payapp/views.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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': '$',
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion templates/payapp/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
{% if user.is_authenticated %}
<p>
Welcome, <b>{{ user.username }}</b>, 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.<br>
It is currently <b>{{ timestamp | time:"g:i:s a" }}</b> on the <b>{{ timestamp | date:"D,d F, Y" }}</b>.
</p>
{% else %}
<p>
Expand Down
28 changes: 28 additions & 0 deletions timestamp_server/timestamp_client.py
Original file line number Diff line number Diff line change
@@ -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
Binary file modified webapps.db
Binary file not shown.

0 comments on commit 1c046fb

Please sign in to comment.