|
8 | 8 | """
|
9 | 9 | import os, sys, cgi, time, re
|
10 | 10 | from collections import defaultdict
|
| 11 | +from datetime import datetime |
11 | 12 | from calendar import timegm
|
12 | 13 |
|
13 | 14 | from utilities import *
|
14 | 15 | from app import *
|
15 | 16 | from models import *
|
16 | 17 |
|
17 |
| -from sqlite3 import IntegrityError |
| 18 | +from sqlite3 import IntegrityError |
18 | 19 |
|
19 |
| -import logging |
20 |
| -log = logging.getLogger() |
21 | 20 |
|
22 | 21 | RE_DIGITS = re.compile('[0-9]+')
|
23 | 22 | RECENTLY_READ_DELTA = 60*60 # 1 hour
|
@@ -97,55 +96,80 @@ def unread_recently_command(request, user, result):
|
97 | 96 | def mark_command(request, user, result):
|
98 | 97 |
|
99 | 98 | try:
|
100 |
| - mark = request.POST['mark'] |
101 |
| - entry_status = request.POST['as'] |
102 |
| - entry_id = request.POST['id'] |
| 99 | + mark, status, object_id = request.POST['mark'], request.POST['as'], request.POST['id'] |
103 | 100 | except KeyError:
|
104 | 101 | return
|
105 | 102 |
|
106 |
| - if not RE_DIGITS.match(entry_id): |
| 103 | + try: |
| 104 | + object_id = int(object_id) |
| 105 | + except ValueError: |
107 | 106 | return
|
108 | 107 |
|
109 |
| - entry_id = int(entry_id) |
110 |
| - |
111 | 108 | if mark == 'item':
|
112 | 109 |
|
113 | 110 | try:
|
114 | 111 | # Sanity check
|
115 |
| - entry = Entry.get(Entry.id == entry_id) |
| 112 | + entry = Entry.get(Entry.id == object_id) |
116 | 113 | except Entry.DoesNotExist:
|
117 |
| - log.warn('could not find requested entry %d, ignored' % entry_id) |
| 114 | + log.warn('could not find requested entry %d, ignored' % object_id) |
118 | 115 | return
|
119 | 116 |
|
120 |
| - if entry_status == 'read': |
| 117 | + if status == 'read': |
121 | 118 | try:
|
122 | 119 | Read.create(user=user, entry=entry)
|
123 | 120 | except IntegrityError:
|
124 |
| - log.warn('entry %d already marked as read, ignored' % entry_id) |
| 121 | + log.warn('entry %d already marked as read, ignored' % object_id) |
125 | 122 | return
|
126 | 123 | #Note: strangely enough 'unread' is not mentioned in
|
127 |
| - # the Fever API, but Reeder app ask for it |
128 |
| - elif entry_status == 'unread': |
| 124 | + # the Fever API, but Reeder app asks for it |
| 125 | + elif status == 'unread': |
129 | 126 | count = Read.delete().where((Read.user==user) & (Read.entry==entry)).execute()
|
130 | 127 | if not count:
|
131 |
| - log.debug('entry %d never marked as read, ignored' % entry_id) |
| 128 | + log.debug('entry %d never marked as read, ignored' % object_id) |
132 | 129 | return
|
133 |
| - elif entry_status == 'saved': |
| 130 | + elif status == 'saved': |
134 | 131 | try:
|
135 | 132 | Saved.create(user=user, entry=entry)
|
136 | 133 | except IntegrityError:
|
137 |
| - log.warn('entry %d already marked as saved, ignored' % entry_id) |
| 134 | + log.warn('entry %d already marked as saved, ignored' % object_id) |
138 | 135 | return
|
139 |
| - elif entry_status == 'unsaved': |
| 136 | + elif status == 'unsaved': |
140 | 137 | count = Saved.delete().where((Saved.user==user) & (Saved.entry==entry)).execute()
|
141 | 138 | if not count:
|
142 |
| - log.debug('entry %d never marked as saved, ignored' % entry_id) |
| 139 | + log.debug('entry %d never marked as saved, ignored' % object_id) |
143 | 140 | return
|
144 | 141 |
|
145 |
| - log.debug('marked entry %d as %s' % (entry_id, entry_status)) |
| 142 | + log.debug('marked entry %d as %s' % (object_id, status)) |
| 143 | + |
| 144 | + |
| 145 | + if mark == 'feed': |
146 | 146 |
|
| 147 | + try: |
| 148 | + # Sanity check |
| 149 | + feed = Feed.get(Feed.id == object_id) |
| 150 | + except Feed.DoesNotExist: |
| 151 | + log.warn('could not find requested feed %d, ignored' % object_id) |
| 152 | + return |
| 153 | + |
| 154 | + if status == 'read': |
| 155 | + # Unix timestamp of the the local client’s last items API request |
| 156 | + try: |
| 157 | + before = datetime.utcfromtimestamp(int(request.POST['before'])) |
| 158 | + except KeyError, ValueError: |
| 159 | + return |
| 160 | + |
| 161 | + with coldsweat_db.transaction(): |
| 162 | + for entry in feed.entries.where(Entry.last_updated_on < before): |
| 163 | + try: |
| 164 | + Read.create(user=user, entry=entry) |
| 165 | + except IntegrityError: |
| 166 | + continue |
| 167 | + |
| 168 | + log.debug('marked feed %d as %s' % (object_id, status)) |
| 169 | + |
| 170 | + |
147 | 171 |
|
148 |
| - #@@TODO: mark:feed, mark:group |
| 172 | + #@@TODO: mark:group |
149 | 173 |
|
150 | 174 |
|
151 | 175 | def links_command(request, user, result):
|
|
0 commit comments