def create(self, request, *args, **kwargs): print("PK::: ", request.user.pk) ed_programs_ids = request.data["education_programs_ids"] for ed_program_id in ed_programs_ids: favorite = Favorite(user_id=request.user.pk, education_program_id=ed_program_id) favorite.save() return Response(status=status.HTTP_201_CREATED)
def setUp(self): super().setUp() recipes = Recipe.objects.all() favorites = [] for index, recipe in enumerate(recipes): if not index % 2: favorites.append(Favorite(user=self.user, recipe=recipe)) Favorite.objects.bulk_create(favorites) purchases = [] for index, recipe in enumerate(recipes): if not index % 3: favorites.append(Purchase(user=self.user, recipe=recipe)) Favorite.objects.bulk_create(purchases) Subscription.objects.create(user=self.user, author=self.another_user)
def change_paste_favorite(request): """ Add/remove paste from user's favorites, and respond with JSON """ response = {"status": "success", "data": {}} char_id = None or request.POST["char_id"] action = None or request.POST["action"] if not request.user.is_authenticated(): response["status"] = "fail" response["data"]["message"] = "Not logged in." else: paste = cache.get("paste:%s" % char_id) if paste == None: try: paste = Paste.objects.select_related("user").get(char_id=char_id) cache.set("paste:%s" % char_id, paste) except ObjectDoesNotExist: cache.set("paste:%s" % char_id, False) response["status"] = "fail" response["data"]["message"] = "The paste has been removed and can no longer be added to favorites." return HttpResponse(json.dumps(response)) elif paste == False: response["status"] = "fail" response["data"]["message"] = "The paste has been removed and can no longer be added to favorites." return HttpResponse(json.dumps(response)) if action == "add": if Favorite.objects.filter(user=request.user, paste=Paste.objects.get(char_id=char_id)).exists(): response["status"] = "fail" response["data"]["message"] = "You can't favorite a paste multiple times" return HttpResponse(json.dumps(response)) favorite = Favorite(user=request.user, paste=Paste.objects.get(char_id=char_id)) favorite.save() cache.set("paste_favorited:%s:%s" % (request.user.username, char_id), True) # Update/clear related cache entries con = get_redis_connection() con.delete("user_favorite_count:%s" % request.user.username) response["data"]["char_id"] = char_id response["data"]["favorited"] = True elif action == "remove": result = Favorite.objects.filter(user=request.user, paste__char_id=char_id).delete() cache.set("paste_favorited:%s:%s" % (request.user.username, char_id), False) # Update/clear related cache entries con = get_redis_connection() con.delete("user_favorite_count:%s" % request.user.username) response["data"]["char_id"] = char_id response["data"]["favorited"] = False else: response["status"] = "fail" response["data"]["message"] = "Valid action wasn't provided." return HttpResponse(json.dumps(response))
def show_paste(request, char_id, raw=False, download=False, version=None): """ Show the paste, possibly as raw text or as a download """ # If paste has expired, show the ordinary "paste not found" page try: paste = cache.get("paste:%s" % char_id) if paste == None: paste = Paste.objects.select_related("user").get(char_id=char_id) cache.set("paste:%s" % char_id, paste) elif paste == False: return render(request, "pastes/show_paste/show_error.html", {"reason": "not_found"}, status=404) version_number = version if version == None: version = paste.version paste_version = cache.get("paste_version:%s:%s" % (char_id, version)) if paste_version == None: paste_version = PasteVersion.objects.get(paste=paste, version=version) cache.set("paste_version:%s:%s" % (char_id, version), paste_version) except ObjectDoesNotExist: cache.set("paste:%s" % char_id, False) return render(request, "pastes/show_paste/show_error.html", {"reason": "not_found"}, status=404) if paste.is_expired(): return render(request, "pastes/show_paste/show_error.html", {"reason": "expired"}, status=404) if paste.is_removed(): if paste.removed == Paste.USER_REMOVAL: return render(request, "pastes/show_paste/show_error.html", {"reason": "user_removed", "removal_reason": paste.removal_reason}, status=404) elif paste.removed == Paste.ADMIN_REMOVAL: return render(request, "pastes/show_paste/show_error.html", {"reason": "admin_removed", "removal_reason": paste.removal_reason}, status=404) if raw: text = paste.get_text(formatted=False, version=version) response = HttpResponse(text, content_type='text/plain') return response elif download: text = paste.get_text(formatted=False, version=version) response = HttpResponse(text, content_type='application/octet-stream') response["Content-Disposition"] = 'attachment; filename="%s.txt"' % char_id return response else: # Display the paste as normal paste_favorited = False if request.user.is_authenticated(): paste_favorited = Favorite.has_user_favorited_paste(request.user, paste) # Add a hit to this paste if the hit is an unique (1 hit = 1 IP address once per 24 hours) ip_address = get_real_ip(request) if ip_address != None: paste_hits = paste.add_hit(ip_address) else: paste_hits = paste.get_hit_count() comment_count = cache.get("paste_comment_count:%s" % char_id) if comment_count == None: comment_count = Comment.objects.filter(paste=paste).count() cache.set("paste_comment_count:%s" % char_id, comment_count) paste_text = paste.get_text(version=version) return render(request, "pastes/show_paste/show_paste.html", {"paste": paste, "paste_version": paste_version, "paste_text": paste_text, "version_number": version_number, "paste_favorited": paste_favorited, "paste_hits": paste_hits, "comment_count": comment_count})
def show_paste(request, char_id, raw=False, download=False, version=None): """ Show the paste, possibly as raw text or as a download """ # If paste has expired, show the ordinary "paste not found" page try: paste = cache.get("paste:%s" % char_id) if paste == None: paste = Paste.objects.select_related("user").get(char_id=char_id) cache.set("paste:%s" % char_id, paste) elif paste == False: return render(request, "pastes/show_paste/show_error.html", {"reason": "not_found"}, status=404) if version == None: version = paste.version paste_version = cache.get("paste_version:%s:%s" % (char_id, version)) if paste_version == None: paste_version = PasteVersion.objects.get(paste=paste, version=version) cache.set("paste_version:%s:%s" % (char_id, version), paste_version) except ObjectDoesNotExist: cache.set("paste:%s" % char_id, False) return render(request, "pastes/show_paste/show_error.html", {"reason": "not_found"}, status=404) if paste.is_expired(): return render(request, "pastes/show_paste/show_error.html", {"reason": "expired"}, status=404) if paste.is_removed(): if paste.removed == Paste.USER_REMOVAL: return render(request, "pastes/show_paste/show_error.html", {"reason": "user_removed", "removal_reason": paste.removal_reason}, status=404) elif paste.removed == Paste.ADMIN_REMOVAL: return render(request, "pastes/show_paste/show_error.html", {"reason": "admin_removed", "removal_reason": paste.removal_reason}, status=404) if raw: text = paste.get_text(formatted=False, version=version) response = HttpResponse(text, content_type='text/plain') return response elif download: text = paste.get_text(formatted=False, version=version) response = HttpResponse(text, content_type='application/octet-stream') response["Content-Disposition"] = 'attachment; filename="%s.txt"' % char_id return response else: # Display the paste as normal paste_favorited = False if request.user.is_authenticated(): paste_favorited = Favorite.has_user_favorited_paste(request.user, paste) # Add a hit to this paste if the hit is an unique (1 hit = 1 IP address once per 24 hours) ip_address = get_real_ip(request) if ip_address != None: paste_hits = paste.add_hit(ip_address) else: paste_hits = paste.get_hit_count() comment_count = cache.get("paste_comment_count:%s" % char_id) if comment_count == None: comment_count = Comment.objects.filter(paste=paste).count() cache.set("paste_comment_count:%s" % char_id, comment_count) paste_text = paste.get_text(version=version) return render(request, "pastes/show_paste/show_paste.html", {"paste": paste, "paste_version": paste_version, "paste_text": paste_text, "version": version, "paste_favorited": paste_favorited, "paste_hits": paste_hits, "comment_count": comment_count})