Skip to content

Commit

Permalink
feat(backend): Switch Code to UUID & Add QR_Code.png generation to sa…
Browse files Browse the repository at this point in the history
…ve function for codes
  • Loading branch information
Teufelchen1 committed Jul 23, 2021
1 parent 8b52d51 commit d87fb5f
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 3 deletions.
1 change: 1 addition & 0 deletions backend/Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ name = "pypi"
[packages]
django = "3.2.4"
django-cors-headers = "*"
qrcode = {extras = ["pil"], version = "*"}

[dev-packages]

Expand Down
57 changes: 56 additions & 1 deletion backend/Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions backend/dpt_app/trails/migrations/0009_code_image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 3.2.5 on 2021-07-22 19:42

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('trails', '0008_player_action_points'),
]

operations = [
migrations.AddField(
model_name='code',
name='image',
field=models.ImageField(blank=True, upload_to='media/'),
),
]
24 changes: 24 additions & 0 deletions backend/dpt_app/trails/migrations/0010_auto_20210723_0843.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Generated by Django 3.2.5 on 2021-07-23 08:43

from django.db import migrations, models
import uuid


class Migration(migrations.Migration):

dependencies = [
('trails', '0009_code_image'),
]

operations = [
migrations.AddField(
model_name='code',
name='uuid',
field=models.UUIDField(default=uuid.uuid4, editable=False, unique=True),
),
migrations.AlterField(
model_name='code',
name='image',
field=models.ImageField(blank=True, editable=False, upload_to='media/'),
),
]
15 changes: 15 additions & 0 deletions backend/dpt_app/trails/qr_models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import qrcode
import uuid
from .enums import ParameterType, CharacterType, ActionType
from django.db import models
from django.core.files import File


class Code(models.Model):
Expand All @@ -9,15 +12,27 @@ class Code(models.Model):
blank=True
)
one_shot = models.BooleanField(default=False)
uuid = models.UUIDField(default=uuid.uuid4, unique=True)
image = models.ImageField(upload_to="media/", blank=True)

def __str__(self):
ret = ""
if len(self.name) > 0:
ret += self.name + ": "
ret += "One-Shot: {0}, ".format(str(self.one_shot))
ret += str([action.label() for action in self.actions.all()])
ret += " UUID: "+str(self.uuid)
return ret

def save(self, *args, **kwargs):
img = qrcode.make("http://localhost:8080/code/"+str(self.uuid))
file_name = "{0}.png".format(str(self.name))
file_path = "/tmp/{0}".format(file_name)
img.save(file_path)
self.image.delete(save=False)
self.image = File(open(file_path, 'rb'), name=file_name)
super(Code, self).save(*args, **kwargs)


class Action(models.Model):
code = models.ForeignKey(
Expand Down
2 changes: 1 addition & 1 deletion backend/dpt_app/trails/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
path('', views.index, name='index'),
path('games/<str:gameId>/clock', views.clock, name='clock'),
path('games/<str:gameId>/parameters', views.parameter, name='parameter'),
path('games/<str:gameId>/codes/<int:codeId>', views.code, name='code'),
path('games/<str:gameId>/codes/<uuid:codeId>', views.code, name='code'),
]
2 changes: 1 addition & 1 deletion backend/dpt_app/trails/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def has_valid_bearer(bearer, game):
calcTimepassingParameters(game)

try:
code = Code.objects.get(pk=codeId)
code = Code.objects.get(uuid=codeId)
except Code.DoesNotExist:
return JsonResponse({"Errors": [
{
Expand Down

0 comments on commit d87fb5f

Please sign in to comment.