def get_promotion(request, id): """ API endpoint for getting a promotion object 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: promotion = Promotion.objects.get(pk=id) except ObjectDoesNotExist: return Response({ "msg": _('NO_PROMOTION_FOUNDED'), "status": 404 }, status=404) if promotion.company.owner == user: response.set_data(serialize_dashboard_promotion_object(promotion)) response.set_result_code(200) response.set_result_msg("MSG_PROMOTIONS_NOT_FOUNDED") return JsonResponse(response.get_dict()) return Response({ "msg": _('MSG_USER_IS_NOT_THE_OWNER'), "status": 404 }, status=404)
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())
def get_winnings(request, pk): """ This function permit to get all the information for winning object for a promotion Args: pk: pk of the promotion request: Returns: """ try: user = MyUser.objects.get(pk=request.user.pk) except ObjectDoesNotExist: return Response({ "msg": _('MSG_USER_NOT_EXIST'), "status": 404 }, status=404) response = ApiJsonResponse() try: promotion = Promotion.objects.get(pk=pk) except ObjectDoesNotExist: return Response({ "msg": _('MSG_NO_PROMOTION_FOUNDED'), "status": 404 }, status=404) if promotion.company.owner == user: for winning in promotion.winnings.all(): try: number = Coupon.objects.filter(promotion=promotion, name=winning.name, distributed=False).count() except ObjectDoesNotExist: return Response( { "msg": _('MSG_GIVEAWAY_NOT_EXIST'), "status": 404 }, status=404) response.set_multiples_data( serialize_winning_object(winning, number)) response.set_result_code(200) response.set_result_msg("MSG_WINNER_FOUNDED") else: return Response({ "msg": _('MSG_USER_IS_NOT_THE_OWNER'), "status": 404 }, status=404) return JsonResponse(response.get_dict())
def campaign_id(request): """ API endpoint for listing the promotion on the analytics page :param request: request parameters :returns: :rtype: HttpResponse """ user = None 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: company = Company.objects.get(owner=user) except: return Response({ "msg": _('MSG_COMPANY_NOT_EXIST'), "status": 404 }, status=404) try: promotions = Promotion.objects.filter(company=company) except ObjectDoesNotExist: response.set_data("[]") response.set_result_code(200) response.set_result_msg("MSG_PROMOTIONS_NOT_FOUNDED") return JsonResponse(response.get_dict()) list_of_promotions = [] for promotion in promotions: list_of_promotions.append({ 'name': promotion.campaign_name, 'id': promotion.pk }) return Response( { "msg": _('MSG_PROMOTION_FOUNDED'), "list_of_promotions": list_of_promotions, "status": 200 }, status=200)
def retrieve_company_campaign(request): """ This endpoint return all the campaign from a company :param request: :return: """ 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: company = Company.objects.get(owner=user) except ObjectDoesNotExist: return Response({ "msg": _('MSG_COMPANY_NOT_EXIST'), "status": 404 }, status=404) try: campaigns = Promotion.objects.filter(company=company) except ObjectDoesNotExist: return Response({ "msg": _('MSG_CAMPAIGNS_NOT_EXIST'), "status": 404 }, status=404) for campaign in campaigns: analytics = PromotionNumbers.objects.filter(promotion=campaign).last() response.set_multiples_data( serialize_dashboard_promotion_object(campaign, analytics)) response.set_result_code(200) response.set_result_msg("MSG_PROMOTIONS_FOUNDED") return JsonResponse(response.get_dict())
def list_favorites_company(request): """ This function permit to return all the favorite promotion for an user Args: request: Returns: """ 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: favorites = Subscription.objects.filter(user=user, follow=True) except ObjectDoesNotExist: return Response( { "msg": _('MSG_FAVORITE_COMPANY_NOT_FOUNDED'), "status": 200 }, status=200) try: if not favorites: return Response( { "msg": _('MSG_NO_FAVORITE_COMPANY_FOUNDED'), "status": 200 }, status=200) for favorite in favorites: response.set_multiples_data( serialize_favorite_company_object(favorite)) except Exception: response.set_multiples_data( serialize_favorite_company_object(favorites)) response.set_result_code(200) response.set_result_msg("MSG_FAVORITE_FOUNDED") return JsonResponse(response.get_dict())
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())
def get_bill_with_pdf(request, id): """ Retrieve a bill with the pdf Args: request: Returns: """ response = ApiJsonResponse() try: bill = Bills.objects.get(pk=id) except ObjectDoesNotExist: response.set_error(1) response.set_result_code(404) response.set_result_msg("MSG_NO_BILLS_FOUNDED") return JsonResponse(response.get_dict()) try: with open(bill.bill.path, "rb") as pdf_file: encoded_string = base64.b64encode(pdf_file.read()) except Exception: response.set_error(1) response.set_result_code(404) response.set_result_msg("`MSG_NO_BILL_PDF_FOUNDED`") return JsonResponse(response.get_dict()) response.set_data(encoded_string.decode("utf-8")) response.set_result_code(200) response.set_result_msg("MSG_BILL_PDF_FOUNDED") return JsonResponse(response.get_dict())
def get_bills(request): """ Retrieve all the bills for a company Args: request: Returns: """ 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: company = Company.objects.get(owner=user) except ObjectDoesNotExist: return Response({ "msg": _('MSG_COMPANY_NOT_EXIST'), "status": 404 }, status=404) try: bills = Bills.objects.filter(company=company) except ObjectDoesNotExist: response.set_error(1) response.set_result_code(404) response.set_result_msg("MSG_NO_BILLS_FOUNDED") return JsonResponse(response.get_dict()) try: for bill in bills: response.set_multiples_data(serialize_bill_object(bill)) except Exception: response.set_multiples_data(serialize_bill_object(bills)) response.set_result_code(200) response.set_result_msg("MSG_PROMOTION_FOUNDED") return JsonResponse(response.get_dict())
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())
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())
def prizes_inventory(request): """ API endpoint for getting all the user prizes object in the inventory 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) except ObjectDoesNotExist: response.set_data("[]") response.set_result_code(200) response.set_result_msg("MSG_PROMOTIONS_NOT_FOUNDED") return JsonResponse(response.get_dict()) for coupon in coupons: if coupon.visible: try: winning_object = coupon.promotion.winnings.get( name=coupon.name) except Exception: return Response( { "msg": _('MSG_ERROR_WITH_SERIALIZER'), "status": 500 }, status=500) response.set_multiples_data( serialize_winning_object_for_user(winning_object, coupon.promotion)) response.set_result_code(200) response.set_result_msg("MSG_PROMOTIONS_FOUNDED") return JsonResponse(response.get_dict())
def followers(request, time): try: user = MyUser.objects.get(pk=request.user.pk) except ObjectDoesNotExist: return Response({ "msg": _('MSG_USER_NOT_EXIST'), "status": 404 }, status=404) try: company = Company.objects.get(owner=user) except ObjectDoesNotExist: return Response({ "msg": _('MSG_COMPANY_NOT_EXIST'), "status": 404 }, status=404) try: analytics = None response = ApiJsonResponse() if time == 'day': analytics = SocialNumbers.objects.filter(company=company).latest('emission_date') response.set_multiples_data(serialize_analytics_social_numbers(analytics)) response.set_result_code(200) response.set_result_msg("MSG_OVERVIEW_ANALYTICS_FOUNDED") return JsonResponse(response.get_dict()) elif time == 'week': analytics = SocialNumbers.objects.filter(company=company, emission_date__range=(timezone.now().date() - timedelta(days=6), timezone.now().date() + timedelta(days=1))) \ .order_by('emission_date') elif time == 'month': analytics = SocialNumbers.objects.filter(company=company, emission_date__range=(timezone.now().date() - timedelta(days=31), timezone.now().date() + timedelta(days=1))) \ .order_by('emission_date') elif time == 'year': today = datetime.date.today() nrb_month = 12 analytics = [] while nrb_month >= 0: if today.month - nrb_month < 1: tmp_month = 12 - (nrb_month - today.month) tmp_year = today.year - 1 else: tmp_month = today.month - nrb_month tmp_year = today.year try: analytic = SocialNumbers.objects.get(company=company, emission_date__month=tmp_month, emission_date__day=1, emission_date__year=tmp_year) analytics.append(analytic) except ObjectDoesNotExist: try: analytic = SocialNumbers.objects.filter(company=company, emission_date__month=tmp_month, emission_date__year=tmp_year).last() if analytic: analytics.append(analytic) except ObjectDoesNotExist: logger.debug("Error with month") nrb_month = nrb_month - 1 for analytic in analytics: response.set_multiples_data(serialize_analytics_social_numbers(analytic)) response.set_result_code(200) response.set_result_msg("MSG_OVERVIEW_ANALYTICS_FOUNDED") return JsonResponse(response.get_dict()) except ObjectDoesNotExist: return Response({ "msg": _('MSG_NO_OVERVIEW_ANALYTICS'), "status": 404 }, status=404)
def clicks(request, time, id): """ This function return analytics from the clicks and action participation to campaigns :param request: :param time: :param id: :return: """ try: user = MyUser.objects.get(pk=request.user.pk) except ObjectDoesNotExist: return Response({ "msg": _('MSG_USER_NOT_EXIST'), "status": 404 }, status=404) try: company = Company.objects.get(owner=user) except ObjectDoesNotExist: return Response({ "msg": _('MSG_COMPANY_NOT_EXIST'), "status": 404 }, status=404) try: promotion = Promotion.objects.get(company=company, pk=id) except ObjectDoesNotExist: return Response({ "msg": _('MSG_PROMOTION_NOT_EXIST'), "status": 404 }, status=404) try: analytics = None response = ApiJsonResponse() if time == 'day': analytics = PromotionNumbers.objects.filter(promotion=promotion).latest('start_date') response.set_multiples_data(serialize_analytics_click(analytics)) response.set_result_code(200) response.set_result_msg("MSG_OVERVIEW_ANALYTICS_FOUNDED") return JsonResponse(response.get_dict()) elif time == 'week': analytics = PromotionNumbers.objects.filter(promotion=promotion, start_date__range=(timezone.now().date() - timedelta(days=7), timezone.now().date() + timedelta(days=1))) \ .order_by('start_date') elif time == 'month': analytics = PromotionNumbers.objects.filter(promotion=promotion, start_date__range=(timezone.now().date() - timedelta(days=31), timezone.now().date() + timedelta(days=1))) \ .order_by('start_date') elif time == 'year': today = datetime.date.today() nrb_month = 12 analytics = [] while nrb_month >= 0: if today.month - nrb_month < 1: tmp_month = 12 - (nrb_month - today.month) tmp_year = today.year - 1 else: tmp_month = today.month - nrb_month tmp_year = today.year try: analytic = PromotionNumbers.objects.get(promotion=promotion,start_date__month=tmp_month, start_date__day=1, start_date__year=tmp_year) analytics.append(analytic) except ObjectDoesNotExist: try: analytic = PromotionNumbers.objects.filter(promotion=promotion, start_date__month=tmp_month, start_date__year=tmp_year).last() if analytic: analytics.append(analytic) except ObjectDoesNotExist: logger.debug("Error with month") nrb_month = nrb_month - 1 for analytic in analytics: response.set_multiples_data(serialize_analytics_click(analytic)) response.set_result_code(200) response.set_result_msg("MSG_OVERVIEW_ANALYTICS_FOUNDED") return JsonResponse(response.get_dict()) except ObjectDoesNotExist: return Response({ "msg": _('MSG_NO_OVERVIEW_ANALYTICS'), "status": 404 }, status=404)
def get_categories(request): """ API endpoint for getting a promotion object Args: request: Returns: promotion object """ response = ApiJsonResponse() try: categories = Category.objects.filter(activated=True) except ObjectDoesNotExist: return Response({ "msg": _('MSG_NO_CATEGORIES_FOUNDED'), "status": 404 }, status=404) try: if not categories: return Response( { "msg": _('MSG_NO_CATEGORIES_FOUNDED'), "status": 404 }, status=404) for category in categories: response.set_multiples_data(serialize_categorie_object(category)) except Exception: response.set_multiples_data(serialize_categorie_object(categories)) response.set_result_code(200) response.set_result_msg("MSG_CATEGORIES_FOUNDED") return JsonResponse(response.get_dict())