def test_remove_unparticipated(self): e, _, _ = generate_participation([0, 4, 3]) users = Account.objects.filter(user__username__iregex=r'^P\d$') oldts_count = Transaction.objects.all().count() ######################################### remove_participants(e, [users[1], users[2]]) ######################################### print_list(Transaction.objects.all(), this_func_name()) self.assertEqual(get_participants(e).count(), 3) self.assertEqual(get_participants(e)[0]['account'], users[0]) # Transactions should be not changed self.assertEqual(oldts_count, Transaction.objects.all().count())
def get(self, req, event_pk, format=None): """ Return participation entity for event: account link + parts """ e = get_event(event_pk) ps = ParticipationSerializer(get_participants(e), many=True, context={'request': req}) return Response(ps.data)
def post(self, req, event_pk, format=None): """ Add participant to event in context. Expect array, like: [{"rate": 1, "account": 1}, ...]. account is pk. """ ser = ParticipationPostSerializer(data=req.data, context={"request": req}, many=True) if ser.is_valid(): newbies = {} for p in ser.validated_data: newbies.update({p["account"]: p["parts"]}) e = get_event(event_pk) add_participants(e, newbies) return Response(ParticipationSerializer(get_participants(e), context={"request": req}, many=True).data) return Response(ser.errors, status=status.HTTP_400_BAD_REQUEST)
def post(self, req, event_pk, format=None): """ Add participant to event in context. Expect array, like: [{"rate": 1, "account": 1}, ...]. account is pk. """ ser = ParticipationPostSerializer(data=req.data, context={'request': req}, many=True) if ser.is_valid(): newbies = {} for p in ser.validated_data: newbies.update({p['account']: p['parts']}) print(newbies) e = get_event(event_pk) add_participants(e, newbies) return Response(ParticipationSerializer(get_participants(e)).data) return Response(ser.errors, status=status.HTTP_400_BAD_REQUEST)
def get_participation(e, pk): """ Internal helper. Get participation dict, or 404. keys: account -- account object rate -- participation parts """ acc = get_object_or_404(Account, pk=pk) tmp = get_participants(e) participation = None for p in tmp: if p['account'] == acc: participation = p break # getting 404 if not participation: raise Http404 return participation
def get_participation(e, pk): """ Internal helper. Get participation dict, or 404. keys: account -- account object rate -- participation parts """ acc = get_object_or_404(Account, pk=pk) tmp = get_participants(e) participation = None for p in tmp: if p["account"] == acc: participation = p break # getting 404 if not participation: raise Http404 return participation
def get(self, req, event_pk, format=None): """ Return participation entity for event: account link + parts """ e = get_event(event_pk) ps = ParticipationSerializer(get_participants(e), many=True, context={"request": req}) return Response(ps.data)