def form_valid(self, form): self.object = form.save(commit=False) # Привязываем код авторизации и создаем для него запись в таблице кодов auth_code = self.request.session.get('auth_code') if auth_code: auth_code = AuthCode.objects.get_by_code(auth_code) self.object.author_code = auth_code # Привязываем правку к выпускнику по id из урла student_id = self.kwargs.get('pk') existing = FieldValue.objects.filter( target_id=student_id, field_name=self.object.field_name, field_value__iexact=self.object.field_value, ) if existing: existing = existing.first() if existing.status == FieldValue.STATUS_DELETED: # it was deleted either by author or by target # if by author and author adds it back — ok # if by author and target adds it back — ok # if by author and another adds it back — ok # if by target and target adds it back — ok # if by target and author adds it back — not ok # if by target and another adds it back — not ok if existing.delete_vote.author_code.owner_id == existing.target_id \ and auth_code != existing.delete_vote.author_code: return HttpResponseRedirect(self.get_success_url()) existing.delete_vote.delete() # add vote up if necessary: # if adder is not author if not self.object.author_code \ or self.object.author_code != existing.author_code: vote = Vote(field_value=existing, value=Vote.VOTE_UP) if auth_code: vote.author_code = auth_code vote.save() rules.update_fields( student_id, self.object.field_name, ) return HttpResponseRedirect(self.get_success_url()) try: self.object.target = Student.objects.get(id=student_id) except Student.objects.DoesNotExist: return Http404() self.object.save() vote = Vote(field_value=self.object, value=Vote.VOTE_ADDED) if auth_code: vote.author_code = auth_code vote.save() rules.update_fields( self.object.target_id, self.object.field_name, ) return HttpResponseRedirect(self.get_success_url())
def run_update(self): for item in FieldValue.objects \ .values('target_id', 'field_name') \ .annotate(last_vote=Max('vote__timestamp')) \ .order_by('last_vote'): rules.update_fields(item['target_id'], item['field_name'], timestamp=item['last_vote'], force_update=True)
def test_deleted(self): c = AuthCode.objects.get(code='2') Vote.objects.create(field_value=self.f, author_code=c, value=Vote.VOTE_TO_DEL) rules.update_fields(self.f.target_id, self.f.field_name) self.f.refresh_from_db() self.assertEqual(self.f.status, FieldValue.STATUS_DELETED) self.assertEqual(self.f.votes, 0)
def test_hidden(self): c = AuthCode.objects.get(code='1') Vote.objects.create(field_value=self.f, author_code=c, value=Vote.VOTE_DOWN) rules.update_fields(self.f.target_id, self.f.field_name) self.f.refresh_from_db() self.assertEqual(self.f.status, FieldValue.STATUS_HIDDEN) self.assertEqual(self.f.votes, -9)
def test_anon_untrusted(self): vote = Vote.objects.all()[0] vote.author_code = None vote.save() self.f.author_code = None self.f.save() rules.update_fields(self.f.target_id, self.f.field_name) self.f.refresh_from_db() self.assertEqual(self.f.status, FieldValue.STATUS_UNTRUSTED) self.assertEqual(self.f.votes, 0.1)
def test_two_trusted(self): c = AuthCode.objects.get(code='1') f = FieldValue(target=self.f.target, author_code=c, field_name=FieldValue.FIELD_CITY, field_value='Osaka') f.save() Vote.objects.create(field_value=f, author_code=c, value=Vote.VOTE_ADDED) rules.update_fields(self.f.target_id, self.f.field_name) self.f.refresh_from_db() self.assertEqual(self.f.status, FieldValue.STATUS_TRUSTED) self.assertEqual(self.f.votes, 1)
def test_deleted_by_owner2(self): vote = Vote.objects.all()[0] vote.author_code = None vote.save() self.f.author_code = None self.f.save() c = AuthCode.objects.get(code='2') Vote.objects.create(field_value=self.f, author_code=c, value=Vote.VOTE_UP) c = AuthCode.objects.get(code='1') Vote.objects.create(field_value=self.f, author_code=c, value=Vote.VOTE_TO_DEL) rules.update_fields(self.f.target_id, self.f.field_name) self.f.refresh_from_db() self.assertEqual(self.f.status, FieldValue.STATUS_DELETED) self.assertEqual(self.f.votes, -8.9)
def test_trusted(self): rules.update_fields(self.f.target_id, self.f.field_name) self.f.refresh_from_db() self.assertEqual(self.f.status, FieldValue.STATUS_TRUSTED) self.assertEqual(self.f.votes, 1)
def handle_vote(request, pk, vote_type): if request.method != 'POST': return HttpResponseBadRequest() obj = Vote() # Идентификатор значения FieldValue try: obj.field_value = FieldValue.objects.get(id=pk) except FieldValue.objects.DoesNotExist: return Http404() # Тип голоса FieldValue if vote_type in (Vote.VOTE_UP, Vote.VOTE_DOWN, Vote.VOTE_TO_DEL): obj.value = vote_type else: return HttpResponseBadRequest() # Привязываем код авторизации и создаем для него запись в таблице кодов auth_code = request.session.get('auth_code') if auth_code: author_code = AuthCode.objects.get_by_code(auth_code) obj.author_code = author_code if obj.value == Vote.VOTE_TO_DEL: if author_code.owner_id != obj.field_value.target_id \ and not Vote.objects.filter( field_value_id=obj.field_value_id, value=Vote.VOTE_ADDED, author_code_id=obj.author_code_id ).exists(): return HttpResponse(status=412) existing = Vote.objects.filter( field_value_id=obj.field_value_id, value=obj.value, author_code_id=obj.author_code_id ) if request.GET.get('remove') == 'yes,please': existing.delete() rules.update_fields( obj.field_value.target_id, obj.field_value.field_name, ) return HttpResponseRedirect( reverse('student-detail', kwargs={ 'pk': str(obj.field_value.target_id)})) elif existing.exists(): return HttpResponse(status=406) if obj.value in (Vote.VOTE_UP, Vote.VOTE_DOWN): # Delete the opposite vote if there is Vote.objects.filter( field_value_id=obj.field_value_id, value=obj.value == Vote.VOTE_UP and Vote.VOTE_DOWN or Vote.VOTE_UP, author_code_id=obj.author_code_id ).delete() elif obj.value == Vote.VOTE_TO_DEL: return HttpResponseBadRequest() obj.save() rules.update_fields( obj.field_value.target_id, obj.field_value.field_name, ) return HttpResponseRedirect( reverse('student-detail', kwargs={ 'pk': str(obj.field_value.target_id)}))