Skip to content
This repository
Browse code

2.0 compat for example

  • Loading branch information...
commit 24220c813b7ffec569cef67acf9631d6f6ce8f3e 1 parent 54f1a11
Charles Leifer authored October 09, 2012
6  example/admin.py
@@ -29,7 +29,7 @@ def create(self):
29 29
 
30 30
     def get_context(self):
31 31
         return {
32  
-            'note_list': Note.select().order_by(('created_date', 'desc')).paginate(1, 3)
  32
+            'note_list': Note.select().order_by(Note.created_date.desc()).paginate(1, 3)
33 33
         }
34 34
 
35 35
 class UserStatsPanel(AdminPanel):
@@ -37,8 +37,8 @@ class UserStatsPanel(AdminPanel):
37 37
 
38 38
     def get_context(self):
39 39
         last_week = datetime.datetime.now() - datetime.timedelta(days=7)
40  
-        signups_this_week = User.filter(join_date__gt=last_week).count()
41  
-        messages_this_week = Message.filter(pub_date__gt=last_week).count()
  40
+        signups_this_week = User.select().where(User.join_date > last_week).count()
  41
+        messages_this_week = Message.select().where(Message.pub_date > last_week).count()
42 42
         return {
43 43
             'signups': signups_this_week,
44 44
             'messages': messages_this_week,
14  example/models.py
@@ -20,18 +20,18 @@ def __unicode__(self):
20 20
 
21 21
     def following(self):
22 22
         return User.select().join(
23  
-            Relationship, on='to_user_id'
24  
-        ).where(from_user=self).order_by('username')
  23
+            Relationship, on=Relationship.to_user
  24
+        ).where(Relationship.from_user==self).order_by(User.username)
25 25
 
26 26
     def followers(self):
27 27
         return User.select().join(
28  
-            Relationship, on='from_user_id'
29  
-        ).where(to_user=self).order_by('username')
  28
+            Relationship, on=Relationship.from_user
  29
+        ).where(Relationship.to_user==self).order_by(User.username)
30 30
 
31 31
     def is_following(self, user):
32  
-        return Relationship.filter(
33  
-            from_user=self,
34  
-            to_user=user
  32
+        return Relationship.select().where(
  33
+            Relationship.from_user==self,
  34
+            Relationship.to_user==user
35 35
         ).exists()
36 36
 
37 37
     def gravatar_url(self, size=80):
24  example/views.py
@@ -22,21 +22,21 @@ def private_timeline():
22 22
     user = auth.get_logged_in_user()
23 23
 
24 24
     messages = Message.select().where(
25  
-        user__in=user.following()
26  
-    ).order_by(('pub_date', 'desc'))
  25
+        Message.user << user.following()
  26
+    ).order_by(Message.pub_date.desc())
27 27
 
28 28
     return object_list('private_messages.html', messages, 'message_list')
29 29
 
30 30
 @app.route('/public/')
31 31
 def public_timeline():
32  
-    messages = Message.select().order_by(('pub_date', 'desc'))
  32
+    messages = Message.select().order_by(Message.pub_date.desc())
33 33
     return object_list('public_messages.html', messages, 'message_list')
34 34
 
35 35
 @app.route('/join/', methods=['GET', 'POST'])
36 36
 def join():
37 37
     if request.method == 'POST' and request.form['username']:
38 38
         try:
39  
-            user = User.get(username=request.form['username'])
  39
+            user = User.select().where(User.username==request.form['username']).get()
40 40
             flash('That username is already taken')
41 41
         except User.DoesNotExist:
42 42
             user = User(
@@ -66,19 +66,19 @@ def followers():
66 66
 
67 67
 @app.route('/users/')
68 68
 def user_list():
69  
-    users = User.select().order_by('username')
  69
+    users = User.select().order_by(User.username)
70 70
     return object_list('user_list.html', users, 'user_list')
71 71
 
72 72
 @app.route('/users/<username>/')
73 73
 def user_detail(username):
74  
-    user = get_object_or_404(User, username=username)
75  
-    messages = user.message_set.order_by(('pub_date', 'desc'))
  74
+    user = get_object_or_404(User, User.username==username)
  75
+    messages = user.message_set.order_by(Message.pub_date.desc())
76 76
     return object_list('user_detail.html', messages, 'message_list', person=user)
77 77
 
78 78
 @app.route('/users/<username>/follow/', methods=['POST'])
79 79
 @auth.login_required
80 80
 def user_follow(username):
81  
-    user = get_object_or_404(User, username=username)
  81
+    user = get_object_or_404(User, User.username==username)
82 82
     Relationship.get_or_create(
83 83
         from_user=auth.get_logged_in_user(),
84 84
         to_user=user,
@@ -89,10 +89,10 @@ def user_follow(username):
89 89
 @app.route('/users/<username>/unfollow/', methods=['POST'])
90 90
 @auth.login_required
91 91
 def user_unfollow(username):
92  
-    user = get_object_or_404(User, username=username)
  92
+    user = get_object_or_404(User, User.username==username)
93 93
     Relationship.delete().where(
94  
-        from_user=auth.get_logged_in_user(),
95  
-        to_user=user,
  94
+        Relationship.from_user==auth.get_logged_in_user(),
  95
+        Relationship.to_user==user,
96 96
     ).execute()
97 97
     flash('You are no longer following %s' % user.username)
98 98
     return redirect(url_for('user_detail', username=user.username))
@@ -115,7 +115,7 @@ def create():
115 115
 @auth.login_required
116 116
 def edit(message_id):
117 117
     user = auth.get_logged_in_user()
118  
-    message = get_object_or_404(Message, user=user, id=message_id)
  118
+    message = get_object_or_404(Message, Message.user==user, Message.id==message_id)
119 119
     if request.method == 'POST' and request.form['content']:
120 120
         message.content = request.form['content']
121 121
         message.save()

0 notes on commit 24220c8

Please sign in to comment.
Something went wrong with that request. Please try again.