def vote_publication(request, publication_id, rate): #pdb.set_trace() publication = Publication.objects.get(pk=publication_id) is_voted = False try: #Check if there's a vote already publication_score = PublicationScore.objects.get( publication = publication, who_vote = request.user ) is_voted = True except PublicationScore.DoesNotExist: publication_score = PublicationScore() publication_score.rate = 0 is_voted = False if not is_voted: publication_score.publication = publication publication_score.who_vote = request.user publication_score.vote_date = datetime.datetime.now() publication_score.rate = rate publication_score.save() publications_scores = PublicationScore.objects.filter( publication = publication ) total_rates = 0 if publications_scores.count() > 10: for rates in publications_scores: total_rates += int(rates.rate) publication.rate = str(float((float(total_rates)/publications_scores.count())*20)) publication.save() is_voted = True
def detailspublication(request, id, username, template_name="publications/details.html"): """ show the publication details """ mypublication = get_object_or_404(Publication, id=id) title = mypublication.title host = "http://%s" % get_host(request) publications = [] followingUsers = [] followerUsers = [] is_follow = False try: follow = FollowAuthor.objects.get(UserFrom=request.user, UserTo=mypublication.author) if follow: is_follow = True else: is_follow = False except: pass if request.user.is_authenticated(): publications = getPublications(request, mypublication.author, True) followerUsers = getFollowers(request, mypublication.author) followingUsers = getFollowings(request, mypublication.author) elif mypublication.is_public == True: return HttpResponseRedirect(reverse("publication_viewer", args=(mypublication.author, mypublication.id))) else: return HttpResponseRedirect(reverse("acct_login")) if mypublication.author == request.user: is_me = True else: is_me = False mypublication.incr_views() is_voted = False # import pdb; pdb.set_trace() if is_me == False: calc_age(request.user.get_profile()) if mypublication.rated > request.user.get_profile().age: raise Http404 try: # Check if there's a vote already publication_score = PublicationScore.objects.get(publication=mypublication, who_vote=request.user) is_voted = True except PublicationScore.DoesNotExist: publication_score = PublicationScore() publication_score.rate = 0 is_voted = False # Found Related Publications try: related_publications = TaggedItem.objects.get_by_model(Publication, mypublication.tags).exclude( author=mypublication.author )[:4] except Publication.DoesNotExist: related_publications = None if mypublication.rate == None: mypublication.rate = 0 mypublication.save() pages = range(1, mypublication.nr_pages + 1) paginator = Paginator(pages, 1) # Make sure page request is an int. If not, deliver first page. try: page = int(request.GET.get("page", "1")) except ValueError: page = 1 # If page request (9999) is out of range, deliver last page of results. try: publication_pages = paginator.page(page) except (EmptyPage, InvalidPage): publication_pages = paginator.page(paginator.num_pages) return render_to_response( template_name, { "host": host, "publication": mypublication, "related_publications": related_publications, "is_me": is_me, "is_voted": is_voted, "other_user": mypublication.author, "publication_score": publication_score.rate, "publications": publications, "followers": followerUsers, "pages": publication_pages, "file_ext": mypublication.images_ext, "followings": followingUsers, "is_follow": is_follow, }, context_instance=RequestContext(request), )