Esempio n. 1
0
def create_favorite(request):
    """

    :param request:
    :return:
    """
    params = request.POST.dict()
    action = params.get('action')
    user_id = params.get('user_id')

    if not user_id and not action:
        return Response()

    data = dict(user=request.user)
    if action == 'attention':
        data['attention_id'] = user_id
    elif action == 'favorite':
        data['favorite_id'] = user_id

    # 防止重复点击
    ins = UserFavorites.objects.filter(**data).first()
    if not ins:
        ins = UserFavorites.objects.create(**data)
        user = User.objects.get(id=user_id)
        update_score(user, 'attention', user.info.id)
    return Response(data=dict(id=ins.id))
Esempio n. 2
0
    def create(self, request, *args, **kwargs):
        data = request.POST.dict()
        serial = ReplyBlogCreateSerializer(data=data)
        if not serial.is_valid():
            return Response(status=400, error=serial.errors)

        ins = serial.save()
        if ins:
            update_score(request.user, 'comments', ins.id)
        return HttpResponseRedirect('/blogs/block/{}'.format(data.get('blog_id')))
Esempio n. 3
0
    def update(self, request, params):
        for item in ['file', 'method']:
            item in params and params.pop(item)
        id = params.get('id')
        if 'buyer' in params:
            buyer = params.pop('buyer')
            user = User.objects.filter(username=buyer).first()
            if user:
                params['buyer_id'] = user.id

        trade = Trade.objects.filter(pk=id).update(**params)
        if trade:
            update_score(request.user, 'trade')

        return HttpResponseRedirect('/accounts/center')
Esempio n. 4
0
    def create(self, request, *args, **kwargs):
        data = request.POST.dict()
        serial = TradeLikeCreateSerializer(data=data)

        if TheoryLike.objects.filter(theory_id=data.get('theory_id'),
                                     user_id=data.get('user_id')).exists()\
                or not serial.is_valid():
            return HttpResponseRedirect('/blogs/theory/{}'.format(
                data.get('theory_id')))

        ins = serial.save()
        if ins:
            update_score(request.user, 'theory_like', ins.id)
        return HttpResponseRedirect('/blogs/theory/{}'.format(
            data.get('theory_id')))
Esempio n. 5
0
def sign_up(req):
    """"""
    username = req.POST.get('username', '')
    password = req.POST.get('password', '')
    print(username, password)
    # return HttpResponseRedirect('/index')
    user = User.objects.filter(username=username).first()
    if user:
        login(req, user)
        return HttpResponseRedirect('/index')
        # return HttpResponse('user exists')
    user = User.objects.create(username=username, password=password)
    info_ins = UserInfo.objects.create(user=user)
    # UserScore.objects.create(user=user)
    login(req, user)
    update_score(req.user, 'login', info_ins.id)
    return HttpResponseRedirect('/index')
Esempio n. 6
0
def blog_relation(request):
    """
    收藏/转发博客
    :param request:
    :return:
    """
    params = request.POST.dict()
    blog = Blog.objects.filter(id=params.get('blog_id')).first()
    if not blog or not params.get('type'):
        return Response()

    data = dict(
        user=request.user,
        block='man',
        type=params.get('type'),
        blog=blog,
    )

    ins, is_create = UserBlogFavorites.objects.update_or_create(**data)
    update_score(blog.user, params.get('type'), blog.id)
    return Response(data=dict(id=ins.id))
Esempio n. 7
0
    def create(self, request, *args, **kwargs):
        data = request.POST.dict()
        if data.get('method') == 'put':
            return self.update(request, data)

        for item in ['file', 'method', 'id']:
            item in data and data.pop(item)

        buyer = User.objects.filter(username=data.get('buyer')).first()
        if not buyer:
            return Response(status=400, error='买家不存在')

        self.serializer_class = TradeCreateSerializer
        serial = self.serializer_class(data=data)
        if not serial.is_valid():
            return Response(status=400, error=serial.errors)

        data['buyer'] = buyer
        data['seller'] = request.user
        data['user'] = request.user
        trade = Trade.objects.create(**data)
        if trade:
            update_score(request.user, 'trade')
        return HttpResponseRedirect('/accounts/center')