restructured project to be easier to develop with

added base for flags and teams
This commit is contained in:
faraphel 2025-02-09 22:25:31 +01:00
parent 8410cc33da
commit 2b8197d3c7
23 changed files with 59 additions and 11 deletions

View file

@ -1,6 +1,6 @@
from django.contrib import admin
from source.apps.ctf import models
from apps.ctf import models
@admin.register(models.Team)

View file

@ -3,4 +3,4 @@ from django.apps import AppConfig
class CtfConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'source.apps.ctf'
name = 'apps.ctf'

View file

@ -1,6 +1,11 @@
from django import forms
class LoginForm(forms.Form):
username = forms.CharField(label="Username", max_length=64)
password = forms.CharField(label="Password", widget=forms.PasswordInput)
class TeamForm(forms.Form):
name = forms.CharField(label="Team's name", max_length=32)

View file

@ -0,0 +1,11 @@
{% extends "ctf/base/base.html" %}
{% block title %}Login{% endblock %}
{% block body %}
<form method="POST">
{% csrf_token %}
{{ form }}
<input type="submit"/>
</form>
{% endblock %}

View file

@ -9,5 +9,5 @@
{% endfor %}
</ul>
<a href="{% url "team_create" %}">Create</a>
<a href="{% url "ctf:team_create" %}">Create</a>
{% endblock %}

View file

@ -1,11 +1,18 @@
from django.urls import path
from source.apps.ctf import views
from apps.ctf import views
app_name = 'ctf'
urlpatterns = [
path("", views.view_homepage, name='homepage'),
path("login", views.view_login, name='login'),
path("teams", views.view_teams, name='teams'),
path("teams/create", views.view_team_create, name='team_create'),
path("flags", views.view_flags, name='flags'),
path("flags/submit", views.view_flag_submit, name='flag_submit'),
]

View file

@ -1,6 +1,7 @@
from django.contrib.auth import aauthenticate, alogin
from django.shortcuts import render, redirect
from source.apps.ctf import forms, models
from apps.ctf import forms, models
# Create your views here.
@ -8,6 +9,26 @@ async def view_homepage(request):
return render(request, "ctf/homepage.html")
async def view_login(request):
if request.method == "POST":
form = forms.LoginForm(request.POST)
if form.is_valid():
# get the user
user = await aauthenticate(
request,
username=form.cleaned_data["username"],
password=form.cleaned_data["password"]
)
if user is not None:
await alogin(request, user)
return redirect("ctf:homepage")
else:
form = forms.LoginForm()
return render(request, "ctf/login.html", context={"form": form})
async def view_teams(request):
teams = [team async for team in models.Team.objects.all()]
return render(request, "ctf/team_list.html", context={"teams": teams})
@ -19,7 +40,7 @@ async def view_team_create(request):
if form.is_valid():
# TODO(Faraphel): additional password to prevent unwanted team creation ?
await models.Team.objects.acreate(name=form.cleaned_data["name"])
return redirect("/")
return redirect("ctf:teams")
else:
form = forms.TeamForm()

View file

@ -1,5 +1,5 @@
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys

View file

@ -26,7 +26,11 @@ SECRET_KEY: str = os.environ["DJANGO_SECRET_KEY"]
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG: bool = os.environ.get("DEBUG", "False") == "True"
ALLOWED_HOSTS = []
# get the list of allowed hosts
ALLOWED_HOSTS = os.environ.get("ALLOWED_HOSTS", "").split(",")
# in debug mode, allow any hosts
if DEBUG:
ALLOWED_HOSTS = ["*"]
# Application definition
@ -56,7 +60,7 @@ MIDDLEWARE = [
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'source.project.urls'
ROOT_URLCONF = 'project.urls'
TEMPLATES = [
{
@ -74,7 +78,7 @@ TEMPLATES = [
},
]
WSGI_APPLICATION = 'source.project.wsgi.application'
WSGI_APPLICATION = 'project.wsgi.application'
# Database

View file

@ -18,7 +18,7 @@ from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path("", include("apps.ctf.urls")),
path("", include("apps.ctf.urls", namespace="ctf")),
path('admin/', admin.site.urls),
path('api/', include('rest_framework.urls'))