Пример #1
0
 def update(self, *args, **kwargs):
     request = args[0]
     if request.method != "PUT":
         return http.HttpResponseRedirect(reverse('post'))
     try:
         obj = PostModel.objects.get(pk=kwargs.get('id'))
         form = PostForm(request.PUT)
         if form.is_valid():
             obj = form.save(obj_post=obj)
             obj_dict ={'ok': {'obj': {'id': obj.id, 'slug': obj.slug,
                                       'title': obj.title, 'content': obj.content,
                                       'create_datetime': str(obj.create_datetime)},
                               'msg': _('post updated successfully :)')
                                       }}
             return self.json_to_response(obj=obj_dict)
         else:
             return self.json_to_response(obj={'errors': form.errors})
     except PostModel.DoesNotExist:
         return self.json_to_response(obj={'errors': 'can not update'})
Пример #2
0
    def create(self, *args, **kwargs):
        request = args[0]
        if request.method != "POST":
            return http.HttpResponseRedirect(reverse('post'))
        form = PostForm(data=request.POST)
        if form.is_valid():
            try:
                obj = form.save()
                obj_dict ={'ok': {'obj': {'id': obj.id, 'slug': obj.slug,
                                          'title': obj.title, 'content': obj.content,
                                          'create_datetime': str(obj.create_datetime)},
                                  'msg': _('post created successfully :)')
                                          }}

                return self.json_to_response(obj=obj_dict)
            except  ModelError as error:
                return self.json_to_response(obj={'errors': {'__all__': [str(error)]}})
            except Exception as error:
                logger.error(error)
                return self.json_to_response(obj={'errors': [_('we have make a mistake, please try again :-(')]})
        else:
            return self.json_to_response(obj={'errors': form.errors})