Example #1
0
    def post(self, request, *args, **kwargs):
        keyword = request.POST.get('keyword', '')
        type_id = request.POST.get('type', 1)
        idx = request.POST.get('idx', 0)
        # valid int
        type_id = int(type_id) if type_id.isdigit() else 1
        idx = int(idx) if idx.isdigit() else 0
        page_limit = 20
        content_type = get_object_or_404(spider_models.ContentType, id=type_id)
        task = spider_models.SpiderTask.objects.filter(
            keyword=keyword, content_type=content_type).order_by('-id')[:1]
        if not task.exists():
            return _ajax_success({
                'idx': 0,
                'status': 1,
                'counts': 0,
                'result': []
            })
        # if task.status ==
        results = spider_models.Items.objects.filter(
            spider_info=task[0])[idx:page_limit]
        ret = []
        # convert queryset to list
        for r in results:
            ret.append(json.loads(r.result))

        return _ajax_success({
            'idx': idx + len(ret),
            'status': 0 if task[0].status <= 1 else 0,
            'counts': len(ret),
            'result': ret
        })
Example #2
0
 def post(self, request, *args, **kwargs):
     form = self.form_class(request.POST)
     if form.is_valid():
         username = request.POST.get('username', '')
         password = request.POST.get('password', '')
         user = authenticate(request, username=username, password=password)
         if user is not None:
             login(request, user)
             return _ajax_success({'username':user.username, 'id':user.id})
     return _ajax_error(101, 'username or password is incorrect.')
Example #3
0
 def post(self, request, *args, **kwargs):
     if not request.user.is_authenticated:
         # Do something for authenticated users.
         return HttpResponse(json.dumps({'status': -1, 'content': 'Invalid user!', 'code': 100}))
     img_id = int(request.POST.get('id', 0))
     user = User.objects.get(username='******')
     img = Cat_models.CatImgs.objects.get(id=img_id)
     if img:
         pic_like = Cat_models.PicLikes(img=img, user=user, is_like=1)
         pic_like.save()
         return _ajax_success(pic_like.id)
     else:
         return _ajax_error(200, 'Invalid img id!')
Example #4
0
 def post(self, request, *args, **kwargs):
     n = request.POST['n']
     result = Cat_models.CatImgs.objects.with_info(int(n), 30)
     content = list()
     for img in result:
         content.append({
             'id': img.id,
             'img_from': img.img_from,
             'img_desc': img.img_desc,
             'n_stars': img.n_stars,
             'n_likes': img.n_likes,
             'n_comments': img.n_comments,
         })
     return _ajax_success(content)
Example #5
0
 def post(self, request, *args, **kwargs):
     return _ajax_success('Hello, World!')