website/src/ctfs/models.py

54 lines
2.1 KiB
Python
Raw Normal View History

2021-07-10 11:18:24 +02:00
from django.db import models
from django.contrib.auth.models import User
from events.models import Event
2021-07-10 11:18:24 +02:00
class Category(models.Model):
name = models.CharField(max_length=200)
description = models.TextField()
slug = models.SlugField(max_length=55)
def __str__(self):
return self.name
class CTF(models.Model):
name = models.CharField(max_length=200)
flag = models.CharField(max_length=100)
disabled = models.BooleanField(default=False)
2021-07-10 11:18:24 +02:00
description = models.TextField(blank=True)
description_en = models.TextField(blank=True)
2021-07-10 11:18:24 +02:00
file = models.FileField(blank=True, upload_to='challenges')
ctf_url = models.URLField(blank=True)
port = models.PositiveSmallIntegerField(null=True, blank=True)
event = models.ForeignKey(Event, null=True, blank=True, on_delete=models.CASCADE)
2021-07-10 11:18:24 +02:00
points = models.PositiveSmallIntegerField()
slug = models.SlugField(max_length=55)
pub_date = models.DateTimeField('Date published')
category = models.ForeignKey(
Category,
on_delete=models.SET_NULL,
null=True,
)
author = models.ForeignKey(User, unique=False, on_delete=models.CASCADE)
2021-07-10 11:18:24 +02:00
def solved_by(self, user):
"""True if the exercise has been solved by the user."""
if not user.is_authenticated:
return False
try:
CTF_flags.objects.get(user=user, ctf=self)
return True
except CTF_flags.DoesNotExist:
return False
def __str__(self):
return self.name
class CTF_flags(models.Model):
user = models.ForeignKey(User, unique=False, on_delete=models.CASCADE)
ctf = models.ForeignKey(CTF, unique=False, on_delete=models.CASCADE)
flag_date = models.DateTimeField('Flag date')
class Meta:
ordering = ['-flag_date']
verbose_name = 'solution'
verbose_name_plural = 'solutions'
# Create your models here.