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
  • Diff rendering mode:
  • inline
  • side by side

swtstore/classes/models/sweet.py

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()
101101
102 # get sweets all sweets authored by a particular user
102 # get all sweets authored by a particular user
103 @staticmethod103 @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()
128 #'created': self.created.isoformat()
129 'created': self.created.strftime('%a, %d %b %Y, %I:%M %p UTC'),
129 }130 }
130131
131132

swtstore/classes/models/user.py

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(),
64 '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 }
6668
67 def __repr__(self):69 def __repr__(self):

swtstore/classes/views/frontend.py

5from flask import Module, jsonify, request, render_template, redirect,\5from flask import Module, jsonify, request, render_template, redirect,\
6 url_for, g, current_app6 url_for, g, current_app
77
8from sqlalchemy import desc
9
10from swtstore.classes.models import Sweet, User8from swtstore.classes.models import Sweet, User
119
1210
13@frontend.route('/', methods=['GET'])13@frontend.route('/', methods=['GET'])
14def index():14def index():
15 sweets = Sweet.getFrontendSwts()15 sweets = Sweet.getFrontendSwts()
16
17 user = User.getCurrentUser()
16 sweets = [sweet.to_dict() for sweet in sweets]
1817
19 return render_template('frontend/index.html', sweets=sweets)18 return render_template('frontend/index.html', sweets=sweets)
2019

swtstore/classes/views/sweet.py

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)
24 return render_template('sweet/specific.html', sweet=sweet.to_dict())
25 else:25 else:
26 abort(404)26 abort(404)

swtstore/classes/views/user.py

89 return redirect(url_for('frontend.index'))89 return redirect(url_for('frontend.index'))
9090
91 if request.method == 'GET':91 if request.method == 'GET':
92 return render_template('user/me.html', user=current_user)
92 return render_template('user/me.html', user=current_user.to_dict())
9393
94 # else POST request94 # 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'))
111111
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)
114115
115116

swtstore/static/css/style.css

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}

swtstore/templates/frontend/index.html

66
7 {% if sweets|length > 0 %}7 {% if sweets|length > 0 %}
88
9 <ul class="entries unstyled">
9 <ul class="list-group">
10 {% for sweet in sweets %}10 {% for sweet in sweets %}
11 <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 }}
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>
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>
2727
28 <small><i>created: {{sweet.created }}</i></small>
28 <p></p>
29 <span class="created">created:
30 <span>{{ sweet.created }}</span>
31 </span>
2932
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) }}">

swtstore/templates/sweet/specific.html

22
3{% block body %}3{% block body %}
44
5<ul class="entries unstyled">
6 <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 }}
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>
13 <b> #{{ sweet.what }} </b>
14 </span>14 </span>
15 <span class="where">15 <span class="where">
16 {{ sweet.where }}16 {{ sweet.where }}

swtstore/templates/user/me.html

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 %}
33
34{% block scripts %}
35
36<script>
37 var created = new Date("{{ user.created }}");
38 window.onload = function() {
39 $('#user-created').html(created.toString());
40 };
41</script>
4239
43{% endblock %}40{% endblock %}

swtstore/templates/user/sweets.html

44
5 {% if sweets|length > 0 %}5 {% if sweets|length > 0 %}
66
7 <ul class="entries unstyled">
7 <ul class="list-group">
8 {% for sweet in sweets %}8 {% for sweet in sweets %}
9 <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 }}
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>
16 <b> #{{ sweet.what }} </b>
17 </span>17 </span>
18 <span class="where">18 <span class="where">
19 {{ sweet.where }}19 {{ sweet.where }}