0

I'm using the djangoratings library found here and I have everything running and set up. In my views.py file I have a button which executes this line of code when pushed:

     myinstance.rating.add(score=1, user=request.user, ip_address=request.META['REMOTE_ADDR'], request.COOKIES)

Everything works fine. The backend works, my columns are updated with the votes etc etc, but how can I access/call the IP and cookie fields and columns in djangoratings so that I can write a quick if condition that refuses to run the 'add' line if the cookies and IP have already voted?

Thanks in advance for any help. I've been really struggling with this a lot.

EazyC
  • 809
  • 1
  • 10
  • 30

1 Answers1

1

myinstance.rating contains method get_ratings() - which returns queryset to calculate all votes related to object. You can easily extend it for retrive necessary information. For example:

# it's lazy object
rating = myinstance.rating.get_ratings()

# do additional query for db
if not rating.filter(user=user, ip_address=request.META['REMOTE_ADDR']).exists():
    ...
Alex Lisovoy
  • 5,767
  • 3
  • 27
  • 28
  • and if I want to filter again with the cookie field as well I just add it accordingly like so right? if not rating.filter(user=user, ip_address=request.META['REMOTE_ADDR'], request.COOKIES).exists(): – EazyC Dec 06 '14 at 10:12
  • 1
    right, but you should use keyword args in filter, like this:`rating.filter(user=user, ip_address=request.META['REMOTE_ADDR'], cookie='concrete cookie').exists()` – Alex Lisovoy Dec 06 '14 at 10:26
  • Right. I can't believe I forgot that. Thanks. I also have one quick question that is slightly out of scope of the thread title, but I'd be beyond appreciative if you could just take a moment to give me a quick hint. In the djangoratings documentation, it describes an IP limit feature that can be set in the settings module. How can I use this IP limit feature? When I set the IP limit counter to 1, it works fine, but it basically crashes my whole django application when the ip limit is reached and gives me an IPlimitReached() error. Do I use an "except" statement here? I'd appreciate any help. – EazyC Dec 06 '14 at 10:37
  • 1
    You are right, you should catch IPlimitReached and handle this case by themselves. Maybe redirect on page with human readable error or something else. – Alex Lisovoy Dec 06 '14 at 10:44
  • Thank you very much for all the insightful points. I'm going to be able to finish my small app tonight thanks to your help. – EazyC Dec 06 '14 at 10:55
  • Hey, one quick question, so when I ran your line of code it comes up with the error 'Float object has no attribute 'filter'" so I think we might be just returning a simple float in the first line of code that can't be used as an instance to filter in the if statement. – EazyC Dec 06 '14 at 21:44
  • 1
    @SwAvGuy Sorry, I missed one letter, instead of `get_rating` you should use `get_ratings`. I updated my snippet. – Alex Lisovoy Dec 06 '14 at 22:38
  • Hi, Sorry for all the questions, I was wondering if you have time, can you explain really quick what 'concrete cookie' is? Your code definitely works, but I am having a hard time actually retrieving the exact vote that was just cast by the user. The condition I am using basically evaluates to true every time meaning that it doesn't retrieve the previous vote just cast. I am thinking I messed up (not your code, it works fine now) because I don't quite understand how to filter the cookie field correctly. – EazyC Dec 07 '14 at 23:30