Exemple #1
0
 def post(self):
     if request.json:
         try:
             tag = Tag(name=self.clean_html(request.json['name']))
         except (SQLObjectNotFound, InvalidData, KeyError):
             return self.send_400()
         else:
             return self.send_200(tag.dict())
     else:
         return self.send_400()
Exemple #2
0
 def get(self, tag_id, tag_name=None):
     if tag_id:
         try:
             tag = Tag.get(tag_id)
         except SQLObjectNotFound:
             return self.send_404()
         else:
             return self.send_200(tag.dict())
     elif tag_name:
         try:
             tag = Tag.select(Tag.q.name==name)[0]
         except (SQLObjectNotFound, IndexError):
             return self.send_404()
         else:
             return self.send_200(tag.dict())
     else:
         return self.send_200([t.dict for t in Tag.select()])
Exemple #3
0
    def deserialize_tags(self, tag_list):
        tags = []
        for t in tag_list:
            t = self.clean_html(t)
            beerlog.app.logger.info("Adding tag: %s" % t)
            try:
                tag = Tag.select(Tag.q.name==t)[0]
            except (SQLObjectNotFound, IndexError):
                tag = Tag(name=t)
            finally:
                tags.append(tag)

        return tags
Exemple #4
0
 def delete(self, tag_id):
     if tag_id:
         try:
             tag = Tag.get(tag_id)
         except SQLObjectNotFound:
             return self.send_404()
         else:
             if g.user.admin or tag.author == g.user:
                 if tag.entries:
                     [e.removeTag(tag) for e in tag.entries]
                 tag.delete(tag_id)
                 return self.send_200(tag.dict())
             else:
                 return self.send_401()
     else:
         return self.send_400()
Exemple #5
0
 def put(self, tag_id):
     if tag_id:
         try:
             tag = Tag.get(tag_id)
         except SQLObjectNotFound:
             return self.send_404()
         else:
             if g.user.admin or tag.author == g.user:
                 if request.json:
                     try:
                         tag.name = self.clean_html(request.json['name'])
                     except KeyError:
                         return self.send_400()
                     else:
                         return self.send_200(tag.dict())
                 else:
                     return self.send_400()
     else:
         return self.send_400()
Exemple #6
0
 def get(self, entry_id=None, date=None, slug=None, tag_name=None):
     if entry_id:
         try:
             beerlog.app.logger.info('Returning info for %s' % entry_id)
             entry = Entry.get(entry_id)
             return self.send_200(entry.dict())
         except SQLObjectNotFound:
             return self.send_404()
     elif date and slug:
         try:
             date_obj = datetime.strptime(date, '%Y-%m-%d')
             next_day = date_obj+timedelta(days=1)
         except ValueError:
             return self.send_400('Date not in YYYY-MM-DD format')
         try:
             entry = Entry.select(AND(OR(Entry.q.post_on<next_day,
                                         Entry.q.post_on>date_obj),
                                      Entry.q.slug==slug))[0]
         except (SQLObjectNotFound, IndexError):
             return self.send_404()
         else:
             beerlog.app.logger.info('Entry %s' % entry)
             return self.send_200(entry.dict())
     elif tag_name:
         try:
             tag = Tag.select(Tag.q.name==tag_name)
         except SQLObjectNotFound:
             return self.send_404()
         else:
             if tag.entries:
                 return self.send_200([e.dict() for e in tag.entries])
             else:
                 return self.send_404()
     else:
         try:
             return self.send_200([e.dict() for e in Entry.select()])
         except SQLObjectNotFound:
             return self.send_404()