Commit 48b777251353260ed7534b644df0e1a69b7a6f3e
Add timestamp format at server-side; code consistency at views
- Decided to moment.js but later. Until then, the to_dict method of sweet and
user returns formatted date in UTC.
- Making all sweet and user related views to use the to_dict method and be
consistent
| | | | 99 | def getFrontendSwts(): | 99 | def getFrontendSwts(): |
---|
100 | return Sweet.query.order_by(Sweet.created.desc()).all() | 100 | return Sweet.query.order_by(Sweet.created.desc()).all() |
---|
101 | | 101 | |
---|
102 | # get sweets all sweets authored by a particular user | | # get sweets all sweets authored by a particular user |
---|
| | 102 | # get all sweets authored by a particular user | 103 | @staticmethod | 103 | @staticmethod |
---|
104 | def getByCreator(user): | 104 | def getByCreator(user): |
---|
105 | return Sweet.query.filter_by(who=user).\ | 105 | return Sweet.query.filter_by(who=user).\ |
---|
… | | … | |
---|
125 | 'context_id': self.context_id, | 125 | 'context_id': self.context_id, |
---|
126 | 'where': self.where, | 126 | 'where': self.where, |
---|
127 | 'how': self.how, | 127 | 'how': self.how, |
---|
128 | 'created': self.created.isoformat() | | 'created': self.created.isoformat() |
---|
| | 128 | #'created': self.created.isoformat() | | | 129 | 'created': self.created.strftime('%a, %d %b %Y, %I:%M %p UTC'), |
---|
129 | } | 130 | } |
---|
130 | | 131 | |
---|
131 | | 132 | |
---|
| | | | 60 | 'id': self.id, | 60 | 'id': self.id, |
---|
61 | 'username': self.username, | 61 | 'username': self.username, |
---|
62 | 'email': self.email, | 62 | 'email': self.email, |
---|
63 | 'created': self.created.isoformat(), | | 'created': self.created.isoformat(), |
---|
64 | 'last_active': self.last_active.isoformat() | | 'last_active': self.last_active.isoformat() |
---|
| | 63 | #'created': self.created.isoformat(), | | | 64 | 'created': self.created.strftime('%a, %d %b %Y, %I:%M %p UTC'), |
---|
| | 65 | #'last_active': self.last_active.isoformat() |
---|
| | 66 | 'last_active': self.last_active.strftime('%a, %d %b %Y, %I:%M %p UTC') |
---|
65 | } | 67 | } |
---|
66 | | 68 | |
---|
67 | def __repr__(self): | 69 | def __repr__(self): |
---|
| | | | 5 | from flask import Module, jsonify, request, render_template, redirect,\ | 5 | from flask import Module, jsonify, request, render_template, redirect,\ |
---|
6 | url_for, g, current_app | 6 | url_for, g, current_app |
---|
7 | | 7 | |
---|
8 | from sqlalchemy import desc | | from sqlalchemy import desc |
---|
9 | | | |
---|
10 | from swtstore.classes.models import Sweet, User | 8 | from swtstore.classes.models import Sweet, User |
---|
11 | | 9 | |
---|
12 | | 10 | |
---|
… | | … | |
---|
13 | @frontend.route('/', methods=['GET']) | 13 | @frontend.route('/', methods=['GET']) |
---|
14 | def index(): | 14 | def index(): |
---|
15 | sweets = Sweet.getFrontendSwts() | 15 | sweets = Sweet.getFrontendSwts() |
---|
16 | | | |
---|
17 | user = User.getCurrentUser() | | user = User.getCurrentUser() |
---|
| | 16 | sweets = [sweet.to_dict() for sweet in sweets] | 18 | | 17 | |
---|
19 | return render_template('frontend/index.html', sweets=sweets) | 18 | return render_template('frontend/index.html', sweets=sweets) |
---|
20 | | 19 | |
---|
| | | | 21 | sweet = Sweet.query.get(id) | 21 | sweet = Sweet.query.get(id) |
---|
22 | if sweet: | 22 | if sweet: |
---|
23 | print "sweet found " + str(sweet) | 23 | print "sweet found " + str(sweet) |
---|
24 | return render_template('sweet/specific.html', sweet=sweet) | | return render_template('sweet/specific.html', sweet=sweet) |
---|
| | 24 | return render_template('sweet/specific.html', sweet=sweet.to_dict()) | 25 | else: | 25 | else: |
---|
26 | abort(404) | 26 | abort(404) |
---|
| | | | 89 | return redirect(url_for('frontend.index')) | 89 | return redirect(url_for('frontend.index')) |
---|
90 | | 90 | |
---|
91 | if request.method == 'GET': | 91 | if request.method == 'GET': |
---|
92 | return render_template('user/me.html', user=current_user) | | return render_template('user/me.html', user=current_user) |
---|
| | 92 | return render_template('user/me.html', user=current_user.to_dict()) | 93 | | 93 | |
---|
94 | # else POST request | 94 | # else POST request |
---|
95 | username = request.form.get('username') | 95 | username = request.form.get('username') |
---|
… | | … | |
---|
110 | return redirect(url_for('frontend.index')) | 110 | return redirect(url_for('frontend.index')) |
---|
111 | | 111 | |
---|
112 | swts = Sweet.getByCreator(user) | 112 | swts = Sweet.getByCreator(user) |
---|
| | 113 | swts = [swt.to_dict() for swt in swts] |
---|
113 | return render_template('user/sweets.html', sweets=swts) | 114 | return render_template('user/sweets.html', sweets=swts) |
---|
114 | | 115 | |
---|
115 | | 116 | |
---|
| | | | 23 | font-size: 0.8em; | 23 | font-size: 0.8em; |
---|
24 | text-align: center; | 24 | text-align: center; |
---|
25 | } | 25 | } |
---|
| | 26 | .created { |
---|
| | 27 | font-size: 0.9em; |
---|
| | 28 | font-style: italic; |
---|
| | 29 | } |
---|
| | | | 6 | | 6 | |
---|
7 | {% if sweets|length > 0 %} | 7 | {% if sweets|length > 0 %} |
---|
8 | | 8 | |
---|
9 | <ul class="entries unstyled"> | | <ul class="entries unstyled"> |
---|
| | 9 | <ul class="list-group"> | 10 | {% for sweet in sweets %} | 10 | {% for sweet in sweets %} |
---|
11 | <li> | | <li> |
---|
| | 11 | <li class="list-group-item"> | 12 | <span class="who"> | 12 | <span class="who"> |
---|
13 | <a href="#"> | 13 | <a href="#"> |
---|
14 | @{{ sweet.who.username }} | | @{{ sweet.who.username }} |
---|
| | 14 | @{{ sweet.who }} | 15 | </a> | 15 | </a> |
---|
16 | </span> | 16 | </span> |
---|
17 | <span class="what"> | 17 | <span class="what"> |
---|
18 | <b> #{{ sweet.what.name }} </b> | | <b> #{{ sweet.what.name }} </b> |
---|
| | 18 | <b> #{{ sweet.what }} </b> | 19 | </span> | 19 | </span> |
---|
20 | <span class="where"> | 20 | <span class="where"> |
---|
21 | {{ sweet.where }} | 21 | {{ sweet.where }} |
---|
… | | … | |
---|
25 | {{ sweet.how|escape|safe }} | 25 | {{ sweet.how|escape|safe }} |
---|
26 | </span> | 26 | </span> |
---|
27 | | 27 | |
---|
28 | <small><i>created: {{sweet.created }}</i></small> | | <small><i>created: {{sweet.created }}</i></small> |
---|
| | 28 | <p></p> | | | 29 | <span class="created">created: |
---|
| | 30 | <span>{{ sweet.created }}</span> |
---|
| | 31 | </span> |
---|
29 | | 32 | |
---|
30 | <span class="pull-right permalink"> | 33 | <span class="pull-right permalink"> |
---|
31 | <a href="{{ url_for('sweet.showSweet', id=sweet.id) }}"> | 34 | <a href="{{ url_for('sweet.showSweet', id=sweet.id) }}"> |
---|
| | | | 2 | | 2 | |
---|
3 | {% block body %} | 3 | {% block body %} |
---|
4 | | 4 | |
---|
5 | <ul class="entries unstyled"> | | <ul class="entries unstyled"> |
---|
6 | <li> | | <li> |
---|
| | 5 | <ul class="list-group"> | | | 6 | <li class="list-group-item"> |
---|
7 | <span class="who"> | 7 | <span class="who"> |
---|
8 | <a href="#"> | 8 | <a href="#"> |
---|
9 | @{{ sweet.who.username }} | | @{{ sweet.who.username }} |
---|
| | 9 | @{{ sweet.who }} | 10 | </a> | 10 | </a> |
---|
11 | </span> | 11 | </span> |
---|
12 | <span class="what"> | 12 | <span class="what"> |
---|
13 | <b> #{{ sweet.what.name }} </b> | | <b> #{{ sweet.what.name }} </b> |
---|
| | 13 | <b> #{{ sweet.what }} </b> | 14 | </span> | 14 | </span> |
---|
15 | <span class="where"> | 15 | <span class="where"> |
---|
16 | {{ sweet.where }} | 16 | {{ sweet.where }} |
---|
| | | | 26 | </p> | 26 | </p> |
---|
27 | </div> | 27 | </div> |
---|
28 | </div> | 28 | </div> |
---|
| | 29 | <div class="form-group"> |
---|
| | 30 | <label class="control-label">Last Active</label> |
---|
| | 31 | <div class=""> |
---|
| | 32 | <p class="form-control-static" id="last-active"> |
---|
| | 33 | {{ user.last_active }} |
---|
| | 34 | </p> |
---|
| | 35 | </div> |
---|
| | 36 | </div> |
---|
29 | <button type="submit" class="btn btn-primary">Update</button> | 37 | <button type="submit" class="btn btn-primary">Update</button> |
---|
30 | </form> | 38 | </form> |
---|
31 | | | |
---|
32 | {% endblock %} | | {% endblock %} |
---|
33 | | | |
---|
34 | {% block scripts %} | | {% block scripts %} |
---|
35 | | | |
---|
36 | <script> | | <script> |
---|
37 | var created = new Date("{{ user.created }}"); | | var created = new Date("{{ user.created }}"); |
---|
38 | window.onload = function() { | | window.onload = function() { |
---|
39 | $('#user-created').html(created.toString()); | | $('#user-created').html(created.toString()); |
---|
40 | }; | | }; |
---|
41 | </script> | | </script> |
---|
42 | | 39 | |
---|
43 | {% endblock %} | 40 | {% endblock %} |
---|
| | | | 4 | | 4 | |
---|
5 | {% if sweets|length > 0 %} | 5 | {% if sweets|length > 0 %} |
---|
6 | | 6 | |
---|
7 | <ul class="entries unstyled"> | | <ul class="entries unstyled"> |
---|
| | 7 | <ul class="list-group"> | 8 | {% for sweet in sweets %} | 8 | {% for sweet in sweets %} |
---|
9 | <li> | | <li> |
---|
| | 9 | <li class="list-group-item"> | 10 | <span class="who"> | 10 | <span class="who"> |
---|
11 | <a href="#"> | 11 | <a href="#"> |
---|
12 | @{{ sweet.who.username }} | | @{{ sweet.who.username }} |
---|
| | 12 | @{{ sweet.who }} | 13 | </a> | 13 | </a> |
---|
14 | </span> | 14 | </span> |
---|
15 | <span class="what"> | 15 | <span class="what"> |
---|
16 | <b> #{{ sweet.what.name }} </b> | | <b> #{{ sweet.what.name }} </b> |
---|
| | 16 | <b> #{{ sweet.what }} </b> | 17 | </span> | 17 | </span> |
---|
18 | <span class="where"> | 18 | <span class="where"> |
---|
19 | {{ sweet.where }} | 19 | {{ sweet.where }} |
---|