Skip to content

Commit

Permalink
Merge pull request #78 from iptch/feature/58-log-distributions
Browse files Browse the repository at this point in the history
track distributions
  • Loading branch information
weber-chris authored Nov 3, 2023
2 parents 1721752 + c7d7bdb commit 385270f
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Generated by Django 4.2.3 on 2023-11-03 19:32

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('core', '0001_initial'),
]

operations = [
migrations.AlterField(
model_name='ownership',
name='last_received',
field=models.DateTimeField(auto_now_add=True),
),
migrations.CreateModel(
name='Distribution',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('quantity', models.PositiveIntegerField(default=1)),
('timestamp', models.DateTimeField(auto_now_add=True)),
('receiver', models.CharField(max_length=200, null=True)),
('user', models.ForeignKey(on_delete=django.db.models.deletion.RESTRICT, to='core.user')),
],
),
]
10 changes: 10 additions & 0 deletions genius_collection/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,13 @@ class Ownership(models.Model):

def __str__(self):
return f'{self.user} besitzt {self.quantity} {self.card}'


class Distribution(models.Model):
def __str__(self):
return f'qty: {self.quantity} triggered_by: {self.user} timestamp: {self.timestamp}'

quantity = models.PositiveIntegerField(default=1)
user = models.ForeignKey(User, on_delete=models.RESTRICT)
timestamp = models.DateTimeField(auto_now_add=True)
receiver = models.CharField(max_length=200, null=True)
34 changes: 24 additions & 10 deletions genius_collection/core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
from genius_collection.core.serializers import UserSerializer, CardSerializer
from django.db.models import Sum
from django.db import connection
from .models import Card, User, Ownership
from django.core.exceptions import ObjectDoesNotExist
from .models import Card, User, Ownership, Distribution
from .jwt_validation import JWTAccessTokenAuthentication
from genius_collection.core.blob_sas import get_blob_sas_url

Expand Down Expand Up @@ -129,6 +130,7 @@ class OverviewViewSet(APIView):
def get(self, request):
user_cards = User.objects.get(email=request.user['email']).cards.all()
total_quantity = Ownership.objects.aggregate(total_quantity=Sum('quantity'))['total_quantity']

rankings = [{
'uniqueCardsCount': u.cards.count(),
'displayName': str(u),
Expand All @@ -138,13 +140,20 @@ def get(self, request):
for i in range(len(rankings)):
rankings[i]['rank'] = i + 1

try:
last_dist = Distribution.objects.latest('timestamp').timestamp
except ObjectDoesNotExist:
last_dist = None


return Response({
'myCardsCount': user_cards.count(),
'totalCardQuantity': total_quantity,
'myUniqueCardsCount': user_cards.distinct().count(),
'allCardsCount': Card.objects.all().count(),
'duplicateCardsCount': user_cards.count() - user_cards.distinct().count(),
'rankingList': rankings
'rankingList': rankings,
'lastDistribution': last_dist
})


Expand All @@ -160,14 +169,19 @@ def post(self, request):
if not current_user.is_admin:
return Response(status=status.HTTP_403_FORBIDDEN,
data={'status': f'Du bist kein Admin.'})
receivers = []
if request.data['receivers'] == 'all':
receivers = User.objects.all()
qty = int(request.data['quantity'])
receivers = request.data['receivers']
distribution = Distribution(quantity=qty, user=current_user, receiver=receivers)
distribution.save()

receiver_users = []
if receivers == 'all':
receiver_users = User.objects.all()
else:
for r in request.data['receivers']:
receivers.append(User.objects.get(email=r))
for receiver in receivers:
Ownership.objects.distribute_random_cards(receiver, int(request.data['quantity']))
for r in receivers:
receiver_users.append(User.objects.get(email=r))
for receiver_user in receiver_users:
Ownership.objects.distribute_random_cards(receiver_user, qty)

return Response(
{'status': f'{request.data["quantity"]} Karten erfolgreich verteilt.'})
{'status': f'{len(receiver_users)} * {qty} = {len(receiver_users) * qty} Karten erfolgreich verteilt.'})

0 comments on commit 385270f

Please sign in to comment.