I am making some view functions to calculate the rank of one user in a community. My problem is that i want to display the rank afferent to each user, in its profile, and i don't know how, since i don;t have a request and a render_to_response (cause i guessed they are not needed) my code:
def calculate_questions_vote(request):
useranswer = Answer.objects.filter (answer_by = request.user)
positive_votes = VoteUpAnswer.objects.filter(answer = useranswer)
negative_votes = VoteDownAnswer.objects.filter(answer = useranswer)
question_vote_rank = sum(positive_votes) - sum(negative_votes.count)
return question_vote_rank
def calculate_replies(request):
the_new = News.objects.filter(created_by = request.user)
reply = Reply.objects.filter(reply_to = the_new)
reply_rank = sum(reply)
return reply_rank
def calculate_votes(request):
the_new = News.objects.filter(created_by = request.user)
vote = Vote.objects.filter(voted = the_new)
vote_rank = sum(vote)
return vote_rank
def personal_rank(request):
personal_rank = question_vote_rank + reply_rank + vote_rank
return personal_rank
and in UserProfiles:
user = request.user
personal_rank = calculate_questions_vote(user) + calculate_replies(user) + personal_rank(user)
but my error is:
Error binding parameter 0 - probably unsupported type.
Is mt approach correct? How should i call the rank function in the profile_view def ?
Thanks!
YOu can call the function in your view like rank = personal_rank(reuest.user) and add rank to your context then.
I wouldnt call this functions "view"-functions, since they are not dealing with a request nor are they returning a HttoResponce; they are more "helper" functions and also don't belong to a model if they are dealing with seperate entities (eg. News & Vote). A descent place for them would be eg. utils.py. You import them from there in your views.py and call them with the user as parameter (if it's the actual user it's request.user).
It makes sense that you can't access request from everywhere, because this forces you to keep more to a mvc-like design, if you need request somewhere you need to pass it on from your original view function!
You should change your last function to:
def personal_rank(user):
personal_rank = calculate_questions_vote(user) + \
calculate_replies(user) + \
calculate_votes(user)
return personl_rank
You could also add this last function to your User or UserProfile model class if you have something like that and then call eg. my_user.personal_rank() or my_user.get_profile().personal_rank().
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With