Esempio n. 1
0
def end_soon(request):
    """
    This function permit to return all the promotion order by end date
    Args:
        request:
    Returns:
    """
    user = None
    response = ApiJsonResponse()
    try:
        token = request.data['token']
    except Exception as e:
        return Response({
            "msg": _('MSG_FIELD_REQUIRED'),
            "status": 404
        }, status=404)
    if token:
        data = {'token': token}
        valid_data = VerifyJSONWebTokenSerializer().validate(data)
        user = valid_data['user']
    try:
        promotions = Promotion.objects.filter(type_of_promotion='public').order_by('end_date')
    except ObjectDoesNotExist:
        response.set_data("[]")
        response.set_result_code(200)
        response.set_result_msg("MSG_NO_PROMOTION_FOUNDED")
        return JsonResponse(response.get_dict())
    for promotion in promotions:
        favorite = False
        user_actions = None
        if user:
            try:
                Favorite.objects.get(promotion=promotion, user=user)
                favorite = True
            except ObjectDoesNotExist:
                pass
            try:
                user_actions = UserSocialAction.objects.get(promotion=promotion, user=user)
            except ObjectDoesNotExist:
                pass
            if promotion.end_date > timezone.now():
                response.set_multiples_data(serialize_promotion_object(promotion, favorite, user_actions))
        else:
            if promotion.end_date > timezone.now():
                response.set_multiples_data(serialize_promotion_object(promotion, favorite, user_actions))
    response.set_result_code(200)
    response.set_result_msg("MSG_PROMOTION_FOUNDED")
    return JsonResponse(response.get_dict())
Esempio n. 2
0
def get_promotion(request, id):
    """
    API endpoint for getting a promotion object
    Args:
        request:
    Returns: promotion object

    """
    user = None
    response = ApiJsonResponse()
    try:
        token = request.data['token']
    except Exception as e:
        return Response({
            "msg": _('MSG_FIELD_REQUIRED'),
            "status": 404
        },
                        status=404)
    if token:
        data = {'token': token}
        valid_data = VerifyJSONWebTokenSerializer().validate(data)
        user = valid_data['user']
    try:
        promotion = Promotion.objects.get(pk=id)
    except ObjectDoesNotExist:
        return Response({
            "msg": _('MSG_PROMOTIONS_NOT_FOUNDED'),
            "status": 404
        },
                        status=404)
    user_actions = None
    favorite = False
    if user:
        try:
            Favorite.objects.get(promotion=promotion, user=user)
            favorite = True
        except ObjectDoesNotExist:
            pass
        try:
            user_actions = UserSocialAction.objects.get(promotion=promotion,
                                                        user=user)
        except ObjectDoesNotExist:
            pass
    try:
        analytics = PromotionNumbers.objects.filter(promotion=promotion).last()
        analytics.click_views = analytics.click_views + 1
        analytics.save()
    except Exception:
        return Response({
            "msg": _('MSG_ERROR_WITH_ANALYTICS'),
            "status": 404
        },
                        status=404)
    response.set_data(
        serialize_promotion_object(promotion, favorite, user_actions))
    response.set_result_code(200)
    response.set_result_msg("MSG_PROMOTIONS_FOUNDED")
    return JsonResponse(response.get_dict())
Esempio n. 3
0
def homepage_promotion(request):
    """

    API endpoint for listing the promotion on the dashboard page
    :param request: request parameters
    :returns:
    :rtype: HttpResponse
    """

    user = None
    response = ApiJsonResponse()
    try:
        token = request.data['token']
    except Exception as e:
        return Response({
            "msg": _('MSG_FIELD_REQUIRED'),
            "status": 404
        },
                        status=404)
    if token:
        data = {'token': token}
        valid_data = VerifyJSONWebTokenSerializer().validate(data)
        user = valid_data['user']
    try:
        promotions = Promotion.objects.filter(type_of_promotion='public')
    except ObjectDoesNotExist:
        response.set_data("[]")
        response.set_result_code(200)
        response.set_result_msg("MSG_PROMOTIONS_NOT_FOUNDED")
        return JsonResponse(response.get_dict())
    for promotion in promotions:
        user_actions = None
        favorite = False
        if user:
            try:
                Favorite.objects.get(promotion=promotion, user=user)
                favorite = True
            except ObjectDoesNotExist:
                pass
            try:
                user_actions = UserSocialAction.objects.get(
                    promotion=promotion, user=user)
            except ObjectDoesNotExist:
                pass
        response.set_multiples_data(
            serialize_promotion_object(promotion, favorite, user_actions))
    response.set_result_code(200)
    response.set_result_msg("MSG_PROMOTIONS_FOUNDED")
    return JsonResponse(response.get_dict())
Esempio n. 4
0
def get_history_promotion(request):
    """

    API endpoint for getting all the user promotion object finished
    Args:
        request:
    Returns: promotion object

    """
    response = ApiJsonResponse()
    try:
        user = MyUser.objects.get(pk=request.user.pk)
    except ObjectDoesNotExist:
        return Response({
            "msg": _('MSG_USER_NOT_EXIST'),
            "status": 404
        },
                        status=404)
    try:
        coupons = Coupon.objects.filter(user=user, distributed=True)
    except ObjectDoesNotExist:
        response.set_data("[]")
        response.set_result_code(200)
        response.set_result_msg("MSG_PROMOTIONS_NOT_FOUNDED")
        return JsonResponse(response.get_dict())
    favorite = False
    user_actions = None
    for coupon in coupons:
        favorite = False
        try:
            Favorite.objects.get(promotion=coupon.promotion, user=user)
            favorite = True
        except ObjectDoesNotExist:
            pass
        try:
            user_actions = UserSocialAction.objects.get(
                promotion=coupon.promotion, user=user)
        except ObjectDoesNotExist:
            pass
#        if coupon.promotion.end_date < timezone.now():
        response.set_multiples_data(
            serialize_promotion_object(coupon.promotion, favorite,
                                       user_actions))
    response.set_result_code(200)
    response.set_result_msg("MSG_PROMOTIONS_FOUNDED")
    return JsonResponse(response.get_dict())