def set_cookies(self, request, response, vote, created, deleted): """ Called by *success_response* when the vote is by an nonymous user. Set the cookie to the response. """ cookie_name = str(cookies.get_name(vote.content_object, vote.key)) if deleted: response.delete_cookie(cookie_name) else: response.set_cookie(cookie_name, vote.cookie, self.cookie_max_age)
def get_vote_data(self, request, allow_anonymous): """ Return two dicts of data to be used to look for a vote and to create a vote. Subclasses in custom ratings apps that override *get_vote_model* can override this method too to add extra fields into a custom vote model. If the first dict is None, then the lookup is not performed. """ content_type = ContentType.objects.get_for_model(self.target_object) ip_address = request.META.get('REMOTE_ADDR') lookups = { 'content_type': content_type, 'object_id': self.target_object.pk, 'key': self.cleaned_data['key'], } data = lookups.copy() data.update({ 'score': self.cleaned_data['score'], 'ip_address': ip_address, }) if allow_anonymous: # votes are handled by cookies if not ip_address: raise exceptions.DataError('Invalid ip address') cookie_name = cookies.get_name(self.target_object, self.key) cookie_value = request.COOKIES.get(cookie_name) if cookie_value: # the user maybe voted this object (it has a cookie) lookups.update({'cookie': cookie_value, 'user__isnull':True}) data['cookie'] = cookie_value else: lookups = None data['cookie'] = cookies.get_value(ip_address) elif request.user.is_authenticated(): # votes are handled by database (django users) lookups.update({'user': request.user, 'cookie__isnull': True}) data['user'] = request.user else: # something went very wrong: if anonymous votes are not allowed # and the user is not authenticated the view should have blocked # the voting process raise exceptions.DataError('Anonymous user cannot vote.') return lookups, data
def get_vote_data(self, request, allow_anonymous): """ Return two dicts of data to be used to look for a vote and to create a vote. Subclasses in custom ratings apps that override *get_vote_model* can override this method too to add extra fields into a custom vote model. If the first dict is None, then the lookup is not performed. """ content_type = ContentType.objects.get_for_model(self.target_object) ip_address = request.META.get('REMOTE_ADDR') lookups = { 'content_type': content_type, 'object_id': self.target_object.pk, 'key': self.cleaned_data['key'], } data = lookups.copy() data.update({ 'score': self.cleaned_data['score'], 'ip_address': ip_address, }) if request.user.is_authenticated(): # votes are handled by database (django users) lookups.update({'user': request.user, 'cookie__isnull': True}) data['user'] = request.user elif allow_anonymous: # votes are handled by cookies if not ip_address: raise exceptions.DataError('Invalid ip address') cookie_name = cookies.get_name(self.target_object, self.key) cookie_value = request.COOKIES.get(cookie_name) if cookie_value: # the user maybe voted this object (it has a cookie) lookups.update({'cookie': cookie_value, 'user__isnull': True}) data['cookie'] = cookie_value else: lookups = None data['cookie'] = cookies.get_value(ip_address) else: # something went very wrong: if anonymous votes are not allowed # and the user is not authenticated the view should have blocked # the voting process raise exceptions.DataError('Anonymous user cannot vote.') return lookups, data
def _get_user_lookups(self, instance, key, user_or_cookies): """ Return the correct db model lookup for given *user_or_cookies*. Return an empty dict if the lookup is for cookies and the user does not own a cookie corresponding to given *instance* and *key*. A *ValueError* is raised if you cookies are given but anonymous votes are not allowed by the handler. """ # here comes your duck if hasattr(user_or_cookies, 'pk'): return {'user': user_or_cookies} elif self.allow_anonymous: cookie_name = cookies.get_name(instance, key) if cookie_name in user_or_cookies: return {'cookie': user_or_cookies[cookie_name]} return {} raise ValueError('Anonymous vote not allowed')