qrurls/models.py
changeset 81 3a2fdc820c41
parent 80 3aaac91a6654
child 82 6442e5c97b48
equal deleted inserted replaced
80:3aaac91a6654 81:3a2fdc820c41
   181         verbose_name = u"URL Item"
   181         verbose_name = u"URL Item"
   182         verbose_name_plural = u"URL Items"
   182         verbose_name_plural = u"URL Items"
   183         ordering = ['published']
   183         ordering = ['published']
   184 
   184 
   185     @classmethod
   185     @classmethod
   186     def search (cls, shorturl=None, shorturl_id=None, item_id=None, related=()) :
   186     def search (cls, shorturl=None, item_id=None, related=()) :
   187         """
   187         """
   188             Return the URLItem for a given shorturl, either the given specific one,
   188             Return the URLItem for a given shorturl, either the given specific one,
   189             or the latest, from the database.
   189             or the latest, from the database.
   190 
   190 
   191             Raises URLItem.NotFound
   191             Raises URLItem.NotFound
   192         """
   192         """
   193         # JOIN against shorturl, urlimage
   193         # JOIN against shorturl, urlimage
   194         url_item = cls.objects.select_related(*related)
   194         url_item = cls.objects.select_related(*related)
   195 
   195 
   196         if shorturl:
   196         if not shorturl:
   197             url_item = url_item.filter(shorturl__shorturl=shorturl)
   197             raise cls.DoesNotExist()
   198         elif shorturl_id:
   198         elif shorturl.isdigit():
   199             shorturl_id = int(shorturl_id)
   199             shorturl_id = int(shorturl)
   200             url_item = url_item.filter(shorturl__id=shorturl_id)
   200             url_item = url_item.filter(shorturl__id=shorturl_id)
   201         else:
   201         else:
   202             raise cls.DoesNotExist()
   202             url_item = url_item.filter(shorturl__shorturl=shorturl)
   203         
   203         
   204         # match for published items
   204         # match for published items
   205         now = timezone.now()
   205         now = timezone.now()
   206         url_item = url_item.filter(published__lt=now).order_by('-published')
   206         url_item = url_item.filter(published__lt=now).order_by('-published')
   207         
   207