From 2b8197d3c7fd2f3f0b08fb7f5d7fc476279cd1e0 Mon Sep 17 00:00:00 2001
From: faraphel <rc60650@hotmail.com>
Date: Sun, 9 Feb 2025 22:25:31 +0100
Subject: [PATCH] restructured project to be easier to develop with added base
 for flags and teams

---
 {source/apps => apps}/__init__.py             |  0
 {source/apps => apps}/ctf/__init__.py         |  0
 {source/apps => apps}/ctf/admin.py            |  2 +-
 {source/apps => apps}/ctf/apps.py             |  2 +-
 {source/apps => apps}/ctf/forms.py            |  5 ++++
 .../apps => apps}/ctf/migrations/__init__.py  |  0
 {source/apps => apps}/ctf/models.py           |  0
 .../ctf/templates/ctf/base/base.html          |  0
 .../ctf/templates/ctf/flag_list.html          |  0
 .../ctf/templates/ctf/flag_submit.html        |  0
 .../ctf/templates/ctf/homepage.html           |  0
 apps/ctf/templates/ctf/login.html             | 11 ++++++++
 .../ctf/templates/ctf/team_create.html        |  0
 .../ctf/templates/ctf/team_list.html          |  2 +-
 {source/apps => apps}/ctf/tests.py            |  0
 {source/apps => apps}/ctf/urls.py             |  9 ++++++-
 {source/apps => apps}/ctf/views.py            | 25 +++++++++++++++++--
 source/manage.py => manage.py                 |  2 +-
 {source/project => project}/__init__.py       |  0
 {source/project => project}/asgi.py           |  0
 {source/project => project}/settings.py       | 10 +++++---
 {source/project => project}/urls.py           |  2 +-
 {source/project => project}/wsgi.py           |  0
 23 files changed, 59 insertions(+), 11 deletions(-)
 rename {source/apps => apps}/__init__.py (100%)
 rename {source/apps => apps}/ctf/__init__.py (100%)
 rename {source/apps => apps}/ctf/admin.py (90%)
 rename {source/apps => apps}/ctf/apps.py (80%)
 rename {source/apps => apps}/ctf/forms.py (54%)
 rename {source/apps => apps}/ctf/migrations/__init__.py (100%)
 rename {source/apps => apps}/ctf/models.py (100%)
 rename {source/apps => apps}/ctf/templates/ctf/base/base.html (100%)
 rename {source/apps => apps}/ctf/templates/ctf/flag_list.html (100%)
 rename {source/apps => apps}/ctf/templates/ctf/flag_submit.html (100%)
 rename {source/apps => apps}/ctf/templates/ctf/homepage.html (100%)
 create mode 100644 apps/ctf/templates/ctf/login.html
 rename {source/apps => apps}/ctf/templates/ctf/team_create.html (100%)
 rename {source/apps => apps}/ctf/templates/ctf/team_list.html (79%)
 rename {source/apps => apps}/ctf/tests.py (100%)
 rename {source/apps => apps}/ctf/urls.py (77%)
 rename {source/apps => apps}/ctf/views.py (62%)
 rename source/manage.py => manage.py (96%)
 rename {source/project => project}/__init__.py (100%)
 rename {source/project => project}/asgi.py (100%)
 rename {source/project => project}/settings.py (93%)
 rename {source/project => project}/urls.py (93%)
 rename {source/project => project}/wsgi.py (100%)

diff --git a/source/apps/__init__.py b/apps/__init__.py
similarity index 100%
rename from source/apps/__init__.py
rename to apps/__init__.py
diff --git a/source/apps/ctf/__init__.py b/apps/ctf/__init__.py
similarity index 100%
rename from source/apps/ctf/__init__.py
rename to apps/ctf/__init__.py
diff --git a/source/apps/ctf/admin.py b/apps/ctf/admin.py
similarity index 90%
rename from source/apps/ctf/admin.py
rename to apps/ctf/admin.py
index 5a74873..f897f54 100644
--- a/source/apps/ctf/admin.py
+++ b/apps/ctf/admin.py
@@ -1,6 +1,6 @@
 from django.contrib import admin
 
-from source.apps.ctf import models
+from apps.ctf import models
 
 
 @admin.register(models.Team)
diff --git a/source/apps/ctf/apps.py b/apps/ctf/apps.py
similarity index 80%
rename from source/apps/ctf/apps.py
rename to apps/ctf/apps.py
index a24299d..72da749 100644
--- a/source/apps/ctf/apps.py
+++ b/apps/ctf/apps.py
@@ -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'
diff --git a/source/apps/ctf/forms.py b/apps/ctf/forms.py
similarity index 54%
rename from source/apps/ctf/forms.py
rename to apps/ctf/forms.py
index b059663..c185eb5 100644
--- a/source/apps/ctf/forms.py
+++ b/apps/ctf/forms.py
@@ -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)
 
diff --git a/source/apps/ctf/migrations/__init__.py b/apps/ctf/migrations/__init__.py
similarity index 100%
rename from source/apps/ctf/migrations/__init__.py
rename to apps/ctf/migrations/__init__.py
diff --git a/source/apps/ctf/models.py b/apps/ctf/models.py
similarity index 100%
rename from source/apps/ctf/models.py
rename to apps/ctf/models.py
diff --git a/source/apps/ctf/templates/ctf/base/base.html b/apps/ctf/templates/ctf/base/base.html
similarity index 100%
rename from source/apps/ctf/templates/ctf/base/base.html
rename to apps/ctf/templates/ctf/base/base.html
diff --git a/source/apps/ctf/templates/ctf/flag_list.html b/apps/ctf/templates/ctf/flag_list.html
similarity index 100%
rename from source/apps/ctf/templates/ctf/flag_list.html
rename to apps/ctf/templates/ctf/flag_list.html
diff --git a/source/apps/ctf/templates/ctf/flag_submit.html b/apps/ctf/templates/ctf/flag_submit.html
similarity index 100%
rename from source/apps/ctf/templates/ctf/flag_submit.html
rename to apps/ctf/templates/ctf/flag_submit.html
diff --git a/source/apps/ctf/templates/ctf/homepage.html b/apps/ctf/templates/ctf/homepage.html
similarity index 100%
rename from source/apps/ctf/templates/ctf/homepage.html
rename to apps/ctf/templates/ctf/homepage.html
diff --git a/apps/ctf/templates/ctf/login.html b/apps/ctf/templates/ctf/login.html
new file mode 100644
index 0000000..3868e97
--- /dev/null
+++ b/apps/ctf/templates/ctf/login.html
@@ -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 %}
\ No newline at end of file
diff --git a/source/apps/ctf/templates/ctf/team_create.html b/apps/ctf/templates/ctf/team_create.html
similarity index 100%
rename from source/apps/ctf/templates/ctf/team_create.html
rename to apps/ctf/templates/ctf/team_create.html
diff --git a/source/apps/ctf/templates/ctf/team_list.html b/apps/ctf/templates/ctf/team_list.html
similarity index 79%
rename from source/apps/ctf/templates/ctf/team_list.html
rename to apps/ctf/templates/ctf/team_list.html
index c03f003..72f064f 100644
--- a/source/apps/ctf/templates/ctf/team_list.html
+++ b/apps/ctf/templates/ctf/team_list.html
@@ -9,5 +9,5 @@
     {% endfor %}
     </ul>
 
-    <a href="{% url "team_create" %}">Create</a>
+    <a href="{% url "ctf:team_create" %}">Create</a>
 {% endblock %}
diff --git a/source/apps/ctf/tests.py b/apps/ctf/tests.py
similarity index 100%
rename from source/apps/ctf/tests.py
rename to apps/ctf/tests.py
diff --git a/source/apps/ctf/urls.py b/apps/ctf/urls.py
similarity index 77%
rename from source/apps/ctf/urls.py
rename to apps/ctf/urls.py
index e7e276a..07e6614 100644
--- a/source/apps/ctf/urls.py
+++ b/apps/ctf/urls.py
@@ -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'),
 ]
diff --git a/source/apps/ctf/views.py b/apps/ctf/views.py
similarity index 62%
rename from source/apps/ctf/views.py
rename to apps/ctf/views.py
index b4971f5..c1e506b 100644
--- a/source/apps/ctf/views.py
+++ b/apps/ctf/views.py
@@ -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()
diff --git a/source/manage.py b/manage.py
similarity index 96%
rename from source/manage.py
rename to manage.py
index 0c58a03..ead61b6 100755
--- a/source/manage.py
+++ b/manage.py
@@ -1,5 +1,5 @@
-#!/usr/bin/env python
 """Django's command-line utility for administrative tasks."""
+
 import os
 import sys
 
diff --git a/source/project/__init__.py b/project/__init__.py
similarity index 100%
rename from source/project/__init__.py
rename to project/__init__.py
diff --git a/source/project/asgi.py b/project/asgi.py
similarity index 100%
rename from source/project/asgi.py
rename to project/asgi.py
diff --git a/source/project/settings.py b/project/settings.py
similarity index 93%
rename from source/project/settings.py
rename to project/settings.py
index beb5fee..9081d77 100644
--- a/source/project/settings.py
+++ b/project/settings.py
@@ -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
diff --git a/source/project/urls.py b/project/urls.py
similarity index 93%
rename from source/project/urls.py
rename to project/urls.py
index 13cdfb0..65616ee 100644
--- a/source/project/urls.py
+++ b/project/urls.py
@@ -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'))
diff --git a/source/project/wsgi.py b/project/wsgi.py
similarity index 100%
rename from source/project/wsgi.py
rename to project/wsgi.py