Django Projektordner von 'core' in ''django' umbenennen
This commit is contained in:
40
django/play/models.py
Normal file
40
django/play/models.py
Normal file
@@ -0,0 +1,40 @@
|
||||
from django.db import models
|
||||
from django.contrib.auth.models import User
|
||||
from library.models import QivipQuiz
|
||||
from django.http import response
|
||||
import random, string
|
||||
|
||||
# Create your models here.
|
||||
class QuizGame(models.Model):
|
||||
host_user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='host_user')
|
||||
join_code = models.CharField(max_length=6, unique=True, blank=True)
|
||||
quiz_id = models.ForeignKey(QivipQuiz, on_delete=models.CASCADE)
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
if not self.join_code:
|
||||
self.join_code = self.generate_unique_code()
|
||||
super().save(*args, **kwargs)
|
||||
|
||||
def generate_unique_code(self):
|
||||
for i in range(10):
|
||||
new_code = ''.join(random.choices(string.digits, k=6))
|
||||
if not QuizGame.objects.filter(join_code=new_code).exists():
|
||||
return new_code
|
||||
|
||||
class QuizGameParticipant(models.Model):
|
||||
participant_id = models.CharField(max_length=200, unique=True)
|
||||
display_name = models.CharField(verbose_name="Anzeigename", max_length=15)
|
||||
quiz_game = models.ForeignKey(QuizGame, on_delete=models.CASCADE, related_name="participant")
|
||||
score = models.IntegerField(verbose_name="Punkte", default=0)
|
||||
avatar = models.CharField(max_length=200, blank=True, default="")
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
if not self.participant_id:
|
||||
self.participant_id = self.generate_unique_id()
|
||||
super().save(*args, **kwargs)
|
||||
|
||||
def generate_unique_id(self):
|
||||
for i in range(10):
|
||||
new_id = ''.join(random.choices(string.digits + string.ascii_lowercase, k=60))
|
||||
if not QuizGameParticipant.objects.filter(participant_id=new_id).exists():
|
||||
return new_id
|
||||
Reference in New Issue
Block a user