187 verbose_name = u"URL Item" |
187 verbose_name = u"URL Item" |
188 verbose_name_plural = u"URL Items" |
188 verbose_name_plural = u"URL Items" |
189 ordering = ['published'] |
189 ordering = ['published'] |
190 |
190 |
191 @classmethod |
191 @classmethod |
192 def get_item (cls, shorturl, item_id=None, related=()) : |
192 def cache_key (cls, shorturl, item_id) : |
|
193 return 'qrurls/url/{shorturl}/{item}'.format(shorturl=shorturl, item=item_id) |
|
194 |
|
195 @classmethod |
|
196 def get (cls, shorturl, item_id=None, related=()) : |
193 """ |
197 """ |
194 Return the URLItem for a given shorturl, either the given specific one, |
198 Return the URLItem for a given shorturl, either the given specific one, |
195 or the latest, from the database. |
199 or the latest, from the database. |
196 |
200 |
197 Raises URLItem.NotFound |
201 Raises URLItem.NotFound |
211 now = timezone.now() |
215 now = timezone.now() |
212 url_item = url_item.filter(published__lt=now).order_by('-published') |
216 url_item = url_item.filter(published__lt=now).order_by('-published') |
213 |
217 |
214 if item_id : |
218 if item_id : |
215 # specific, but still the published on |
219 # specific, but still the published on |
216 item_id = int(item_id) |
|
217 log.debug("search @ %d", item_id) |
220 log.debug("search @ %d", item_id) |
218 |
221 |
219 return url_item.get(id=item_id) # raises DoesNotExist |
222 return url_item.get(id=item_id) # raises DoesNotExist |
220 else : |
223 else : |
221 # most recent |
224 # most recent |
235 """ |
238 """ |
236 key = URL.cache_key(shorturl) |
239 key = URL.cache_key(shorturl) |
237 get = cache.get(key) |
240 get = cache.get(key) |
238 |
241 |
239 if get : |
242 if get : |
240 url, modified = get |
243 log.debug("get cache: %s", key) |
241 log.debug("get cache: %s: %s", key, url) |
244 return get |
242 else: |
245 else: |
243 # from db |
246 # from db |
244 url_item = cls.get_item(shorturl=shorturl) |
247 url_item = cls.get(shorturl=shorturl) |
245 |
248 set = ( |
246 url = url_item.get_absolute_url() |
249 url_item.get_absolute_url(), |
247 modified = url_item.last_modified() |
250 url_item.last_modified() |
|
251 ) |
248 |
252 |
249 log.debug("set cache: %s: %s", key, url) |
253 log.debug("set cache: %s", key) |
250 cache.set(key, (url, modified)) # XXX: expiry |
254 cache.set(key, set) |
251 |
255 return set |
252 return url, modified |
256 |
|
257 @classmethod |
|
258 def get_item (cls, shorturl, item_id) : |
|
259 """ |
|
260 Return a data dict for the given URLItem, from cache or DB. |
|
261 |
|
262 Returns { url: str, title: str, image: str, last_modified: datetime } |
|
263 Raises URLItem.NotFound |
|
264 """ |
|
265 |
|
266 key = cls.cache_key(shorturl, item_id) |
|
267 get = cache.get(key) |
|
268 |
|
269 if get: |
|
270 log.debug("cache get: %s", key) |
|
271 return get |
|
272 else: |
|
273 url_item = URLItem.get(shorturl, item_id=item_id, |
|
274 related=('shorturl', 'image'), |
|
275 ) |
|
276 set = dict( |
|
277 url = url_item.url, |
|
278 title = url_item.title(), |
|
279 image = url_item.image.get_absolute_url() if url_item.image else None, |
|
280 last_modified = url_item.last_modified(), |
|
281 ) |
|
282 |
|
283 log.debug("cache set: %s: %s", key, set) |
|
284 cache.set(key, set) |
|
285 return set |
253 |
286 |
254 def get_absolute_url (self) : |
287 def get_absolute_url (self) : |
255 if self.url : |
288 if self.url : |
256 return self.url |
289 return self.url |
257 elif self.shorturl and self.id : |
290 elif self.shorturl and self.id : |