get_age made more robust plus fixed minutes error.
Made Voucher.creation_date, last_edit_date and Attendance.creation_date tz aware
This commit is contained in:
parent
01298ebadb
commit
7650fffea0
@ -23,13 +23,21 @@ class UTC(tzinfo):
|
||||
utc = UTC()
|
||||
|
||||
|
||||
def get_age(old_date):
|
||||
now = datetime.utcnow().replace(tzinfo=utc)
|
||||
def get_age(old_date, now = datetime.utcnow().replace(tzinfo=utc)):
|
||||
def isNaive(date):
|
||||
return date.tzinfo is None or date.tzinfo.utcoffset(date) is None
|
||||
if isNaive(old_date) == isNaive(now):
|
||||
pass
|
||||
elif isNaive(old_date):
|
||||
old_date = old_date.replace(tzinfo=utc)
|
||||
else:
|
||||
now = now.replace(tzinfo=utc)
|
||||
|
||||
delta = now - old_date
|
||||
if delta.days > 0:
|
||||
return '{0} days'.format(delta.days)
|
||||
if delta.seconds > 3600:
|
||||
return '{0} hours'.format(delta.seconds // 3600)
|
||||
if delta.seconds > 60:
|
||||
return '{0} minutes'.format(delta.seconds // 3600)
|
||||
return '{0} minutes'.format(delta.seconds // 60)
|
||||
return '{0} seconds'.format(delta.seconds)
|
||||
|
@ -56,9 +56,9 @@ class Voucher(Base):
|
||||
reconcilliation_date = Column('ReconcilliationDate', DateTime)
|
||||
narration = Column('Narration', Unicode(1000))
|
||||
posted = Column('Posted', Boolean)
|
||||
creation_date = Column('CreationDate', DateTime)
|
||||
creation_date = Column('CreationDate', DateTime(timezone=True))
|
||||
|
||||
last_edit_date = Column('LastEditDate', DateTime)
|
||||
last_edit_date = Column('LastEditDate', DateTime(timezone=True))
|
||||
_type = Column('VoucherType', Integer)
|
||||
user_id = Column('UserID', GUID(), ForeignKey('auth_users.UserID'))
|
||||
poster_id = Column('PosterID', GUID(), ForeignKey('auth_users.UserID'))
|
||||
@ -96,8 +96,8 @@ class Voucher(Base):
|
||||
self.reconcilliation_date = reconcilliation_date
|
||||
self.narration = narration
|
||||
self.posted = posted
|
||||
self.creation_date = datetime.now() if creation_date is None else creation_date
|
||||
self.last_edit_date = datetime.now() if last_edit_date is None else last_edit_date
|
||||
self.creation_date = datetime.utcnow() if creation_date is None else creation_date
|
||||
self.last_edit_date = datetime.utcnow() if last_edit_date is None else last_edit_date
|
||||
self.type = type
|
||||
self.user_id = user_id
|
||||
self.poster_id = poster_id
|
||||
@ -262,7 +262,7 @@ class Attendance(Base):
|
||||
date = Column('Date', DateTime)
|
||||
attendance_type = Column('AttendanceType', Integer)
|
||||
amount = Column('Amount', Numeric)
|
||||
creation_date = Column('CreationDate', DateTime)
|
||||
creation_date = Column('CreationDate', DateTime(timezone=True))
|
||||
user_id = Column('UserID', GUID(), ForeignKey('auth_users.UserID'))
|
||||
is_valid = Column('IsValid', Boolean)
|
||||
|
||||
@ -275,7 +275,7 @@ class Attendance(Base):
|
||||
self.date = date
|
||||
self.attendance_type = attendance_type
|
||||
self.amount = amount if amount is not None else 0
|
||||
self.creation_date = creation_date if creation_date is not None else datetime.now()
|
||||
self.creation_date = datetime.utcnow() if creation_date is None else creation_date
|
||||
self.user_id = user_id
|
||||
self.is_valid = is_valid if is_valid is not None else True
|
||||
|
||||
|
@ -69,7 +69,7 @@ def issue_update_voucher(voucher, json, user):
|
||||
voucher.date = datetime.datetime.strptime(json['Date'], '%d-%b-%Y')
|
||||
voucher.narration = json['Narration']
|
||||
voucher.user_id = user.id
|
||||
voucher.last_edit_date = datetime.datetime.now()
|
||||
voucher.last_edit_date = datetime.datetime.utcnow()
|
||||
|
||||
for item in voucher.journals:
|
||||
if item.debit == 1:
|
||||
|
@ -29,7 +29,7 @@ def journal_update_voucher(voucher, json, user):
|
||||
voucher.narration = json['Narration']
|
||||
voucher.user_id = user.id
|
||||
voucher.posted = False
|
||||
voucher.last_edit_date = datetime.datetime.now()
|
||||
voucher.last_edit_date = datetime.datetime.utcnow()
|
||||
|
||||
newJournals = json['Journals']
|
||||
for i in range(len(voucher.journals), 0, -1):
|
||||
|
@ -70,7 +70,7 @@ def purchase_update_voucher(voucher, json, user):
|
||||
voucher.narration = json['Narration']
|
||||
voucher.user_id = user.id
|
||||
voucher.posted = False
|
||||
voucher.last_edit_date = datetime.datetime.now()
|
||||
voucher.last_edit_date = datetime.datetime.utcnow()
|
||||
|
||||
purchase_update_inventory(voucher, json['Inventories'], json['Date'])
|
||||
purchase_update_journals(voucher, json['Journals'])
|
||||
|
@ -65,7 +65,7 @@ def purchase_return_update_voucher(voucher, json, user):
|
||||
voucher.narration = json['Narration']
|
||||
voucher.user_id = user.id
|
||||
voucher.posted = False
|
||||
voucher.last_edit_date = datetime.datetime.now()
|
||||
voucher.last_edit_date = datetime.datetime.utcnow()
|
||||
|
||||
purchase_return_update_inventory(voucher, json['Inventories'], json['Date'])
|
||||
purchase_return_update_journals(voucher, json['Journals'])
|
||||
|
@ -41,7 +41,7 @@ def salary_deduction_update_voucher(voucher, json, user):
|
||||
days_in_month = voucher.date.day
|
||||
voucher.user_id = user.id
|
||||
voucher.posted = False
|
||||
voucher.last_edit_date = datetime.datetime.now()
|
||||
voucher.last_edit_date = datetime.datetime.utcnow()
|
||||
|
||||
newDeductions = json['SalaryDeductions']
|
||||
exp, total, journals = 0, 0, []
|
||||
|
Loading…
x
Reference in New Issue
Block a user