Exemplo n.º 1
0
 def get(self, entry_id, tag=None, slug=None, date=None):
     if entry_id is not None:
         try:
             entry = Entry.get(entry_id)
             return self.send_200(entry.dict())
         except SQLObjectNotFound:
             return self.send_404()
     else:
         try:
             return self.send_200([entry.dict() for e in Entry.select()])
         except SQLObjectNotFound:
             return self.send_404()
Exemplo n.º 2
0
    def put(self, entry_id):
        if entry_id is not None:
            try:
                entry = Entry.get(entry_id)
            except SQLObjectNotFound:
                return self.send_404()

            if g.user != entry.user or not g.user.admin:
                return self.send_401()
            else:
                if request.json:
                    data = self.entry_from_json(request.json)
                    try:
                        if data:
                            entry.title = data.title
                            entry.body = data.body
                            entry.draft = data.draft
                            entry.post_on = data.post_on
                            for t in entry.tags:
                                t.removeTag(t)
                            for t in self.deserialize_tags(request.json['tags']):
                                t.addTag(t)
                        else:
                            raise InvalidData
                    except InvalidData:
                        return self.send_400("Missing required fields")
                    else:
                        return self.send_200(entry.dict())
                else:
                    return self.send_400('JSON required')
        else:
            return self.send_404()
Exemplo n.º 3
0
    def post(self):
        beerlog.app.logger.info('request data: %s' % request.data)
        if request.json:
            try:
                data = self.get_entry_from_json(request.json)
                if data:
                    entry = Entry(data)
                else:
                    return self.send_400("Missing required fields")

            except InvalidData, e:
                beerlog.app.logger.critical('Validation failed %s')
                return self.send_400(e)
            else:
                beerlog.app.logger.info("Entry created: %s" % entry)
                for t in self.deserialize_tags(request.json['tags']):
                    entry.addTag(t)

                beerlog.app.logger.info("JSON: %s" % entry.dict())
                return self.send_201(entry.dict())
Exemplo n.º 4
0
 def delete(self, entry_id):
     if entry_id is not None:
         try:
             entry = Entry.get(entry_id)
         except SQLObjectNotFound:
             return self.send_404()
         if g.user != entry.user or g.user.admin:
             return self.send_401()
         else:
             entry.delete(entry_id)
             return self.send_200(entry.dict())
     else:
         return self.send_404()
Exemplo n.º 5
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()