diff --git a/wowfunding/bin/utils_request.py b/wowfunding/bin/utils_request.py index 55bfd5c..529a271 100644 --- a/wowfunding/bin/utils_request.py +++ b/wowfunding/bin/utils_request.py @@ -3,18 +3,20 @@ from flask import session, g import settings from wowfunding.factory import app, db_session, summary_data -from wowfunding.orm.orm import Proposal, User +from wowfunding.orm.orm import Proposal, User, Comment @app.context_processor def templating(): global summary_data from flask.ext.login import current_user + recent_comments = db_session.query(Comment).order_by(Comment.date_added.desc()).limit(3).all() return dict(logged_in=current_user.is_authenticated, current_user=current_user, funding_categories=settings.FUNDING_CATEGORIES, funding_statuses=settings.FUNDING_STATUSES, - summary_data=summary_data[1]) + summary_data=summary_data[1], + recent_comments=recent_comments) def fetch_summary(purge=False): diff --git a/wowfunding/bin/utils_time.py b/wowfunding/bin/utils_time.py new file mode 100644 index 0000000..3848b07 --- /dev/null +++ b/wowfunding/bin/utils_time.py @@ -0,0 +1,163 @@ +from datetime import datetime, date +from dateutil import parser +import math +import calendar + + +class TimeMagic(): + def __init__(self): + self.now = datetime.now() + self.weekdays_en = { + 0: 'monday', + 1: 'tuesday', + 2: 'wednesday', + 3: 'thursday', + 4: 'friday', + 5: 'saturday', + 6: 'sunday' + } + self.months_en = { + 0: 'january', + 1: 'february', + 2: 'march', + 3: 'april', + 4: 'may', + 5: 'june', + 6: 'july', + 7: 'august', + 8: 'september', + 9: 'october', + 10: 'november', + 11: 'december' + } + + def get_weekday_from_datetime(self, dt): + n = dt.today().weekday() + return n + + def week_number_get(self): + now = datetime.now() + return int(now.strftime("%V")) + + def week_number_verify(self, week_nr): + if week_nr > 0 or week_nr <= 53: + return True + + def get_weeknr_from_date(self, date): + return date.strftime("%V") + + def year_verify(self, year): + if isinstance(year, str): + try: + year = int(year) + except Exception as ex: + return False + + if 2000 <= year <= 2030: + return True + + def get_day_number(self): + dt = datetime.now() + return dt.today().weekday() + + def get_month_nr(self): + return datetime.now().strftime("%m") + + def get_daynr_from_weekday(self, weekday): + for k, v in self.weekdays_en.items(): + if v == weekday: + return k + + def get_day_from_daynr(self, nr): + return self.weekdays_en[nr] + + def get_month_from_weeknr(self, nr): + nr = float(nr) / float(4) + if nr.is_integer(): + nr -= 1 + else: + nr = math.floor(nr) + if nr < 0: + nr = 0 + + return self.months_en[nr] + + def get_month_nr_from_month(self, month): + for k, v in self.months_en.items(): + if v == month: + return k + + def get_year(self): + return date.today().year + + def get_month(self): + return date.today().month + + def get_amount_of_days_from_month_nr(self, month_nr): + try: + max_days = calendar.monthrange(self.get_year(), int(month_nr))[1] + return max_days + except Exception as e: + pass + + def from_till(self): + m = self.get_month() + d = self.get_amount_of_days_from_month_nr(m) + y = self.get_year() + + if len(str(d)) == 1: + d = '0' + str(d) + else: + d = str(d) + + if len(str(m)) == 1: + m = '0' + str(m) + else: + m = str(m) + + f = '%s/01/%s' % (m, y) + t = '%s/%s/%s' % (m, d, y) + + return {'date_from': f, 'date_till': t} + + def ago_dt(self, datetime): + return self.ago(datetime) + + def ago_str(self, date_str): + date = parser.parse(date_str) + return self.ago(date) + + def ago(self, datetime=None, epoch=None): + import math + + if epoch: + td = int(epoch) + else: + if datetime: + td = (self.now - datetime).total_seconds() + else: + return None + + if td < 60: + if td == 1: + return '%s second ago' + else: + return 'Just now' + elif 60 <= td < 3600: + if 60 <= td < 120: + return '1 minute ago' + else: + return '%s minutes ago' % str(int(math.floor(td / 60))) + elif 3600 <= td < 86400: + if 3600 <= td < 7200: + return '1 hour ago' + else: + return '%s hours ago' % str(int(math.floor(td / 60 / 60))) + elif td >= 86400: + if td <= 86400 < 172800: + return '1 day ago' + else: + x = int(math.floor(td / 24 / 60 / 60)) + if x == 1: + return '1 day ago' + return '%s days ago' % str(x) diff --git a/wowfunding/orm/orm.py b/wowfunding/orm/orm.py index e8d9d2b..ed8e8df 100644 --- a/wowfunding/orm/orm.py +++ b/wowfunding/orm/orm.py @@ -323,6 +323,11 @@ class Comment(base): ix_comment_replied_to = sa.Index("ix_comment_replied_to", replied_to) ix_comment_proposal_id = sa.Index("ix_comment_proposal_id", proposal_id) + @property + def ago(self): + from wowfunding.bin.utils_time import TimeMagic + return TimeMagic().ago(self.date_added) + @staticmethod def find_by_id(cid: int): from wowfunding.factory import db_session diff --git a/wowfunding/static/css/wow.css b/wowfunding/static/css/wow.css index f690338..47ea634 100644 --- a/wowfunding/static/css/wow.css +++ b/wowfunding/static/css/wow.css @@ -420,3 +420,8 @@ span.username a.author{ .form-admin{ border: 1px solid #b60000; } + +ul.b { + list-style-type: square; + padding-left: 1.0rem; +} \ No newline at end of file diff --git a/wowfunding/templates/sidebar.html b/wowfunding/templates/sidebar.html index 336d2ea..acf38ae 100644 --- a/wowfunding/templates/sidebar.html +++ b/wowfunding/templates/sidebar.html @@ -54,9 +54,19 @@
-
Cool widget
+
Recent comments
- Many widgets. Such sidebar. Wow. +