Skip to content
This repository has been archived by the owner on Jan 3, 2024. It is now read-only.

Commit

Permalink
feat(templates): add the initial project templates
Browse files Browse the repository at this point in the history
  • Loading branch information
bitterteriyaki committed Sep 26, 2023
1 parent 3c32e77 commit 8ad63cf
Show file tree
Hide file tree
Showing 9 changed files with 131 additions and 13 deletions.
Empty file removed apps/templates/.keep
Empty file.
16 changes: 15 additions & 1 deletion poetry.lock

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

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ python = "^3.11"
django = "^4.2.5"
psycopg2 = "^2.9.7"
django-environ = "^0.11.2"
django-guardian = "^2.4.0"

[tool.poetry.group.dev.dependencies]
pre-commit = "^3.4.0"
Expand Down
7 changes: 4 additions & 3 deletions server/settings/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
from os.path import join
from pathlib import Path

from environ import Env

BASE_DIR = Path(__file__).parent.parent.parent
APPS_DIR = BASE_DIR / "apps"

# Load environment variables from config/.env file. See
# https://django-environ.readthedocs.io/en/latest/

env = Env()
env.read_env(join(BASE_DIR, "config", ".env"))
READ_DOTENV = env.bool("DJANGO_READ_DOTENV", default=False)

if READ_DOTENV:
env.read_env(BASE_DIR / "config" / ".env")
39 changes: 31 additions & 8 deletions server/settings/base.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from os.path import join
from typing import List

from server.settings import APPS_DIR, BASE_DIR, env
from server.settings import BASE_DIR, env

######################
# General Settings #
Expand All @@ -24,11 +24,6 @@

LOCALE_PATHS = [join(BASE_DIR, "locale")]

# Default primary key field type
# https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"

#################
# Middlewares #
#################
Expand All @@ -49,6 +44,11 @@

DATABASES = {"default": env.db()}

# Default primary key field type
# https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"

##########
# URLs #
##########
Expand All @@ -70,7 +70,9 @@
"django.contrib.staticfiles",
]

THIRD_PARTY_APPS: List[str] = []
THIRD_PARTY_APPS = [
"guardian",
]

LOCAL_APPS: List[str] = []

Expand Down Expand Up @@ -100,7 +102,7 @@
TEMPLATES = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [APPS_DIR / "templates"],
"DIRS": [BASE_DIR / "templates"],
"APP_DIRS": True,
"OPTIONS": {
"context_processors": [
Expand All @@ -118,3 +120,24 @@
##################

STATIC_URL = "/static/"

####################
# Authentication #
####################

AUTHENTICATION_BACKENDS = [
"django.contrib.auth.backends.ModelBackend",
"guardian.backends.ObjectPermissionBackend",
]

LOGIN_REDIRECT_URL = "home"

LOGOUT_REDIRECT_URL = "home"

LOGIN_URL = "login"

#####################
# Django Guardian #
#####################

ANONYMOUS_USER_NAME = None
7 changes: 6 additions & 1 deletion server/urls.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
from django.contrib import admin
from django.urls import path
from django.urls import include, path
from django.views.generic import TemplateView

home_view = TemplateView.as_view(template_name="pages/home.html")

urlpatterns = [
path("admin/", admin.site.urls),
path("", include("django.contrib.auth.urls")),
path("", home_view, name="home"),
]
23 changes: 23 additions & 0 deletions templates/base.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{% load static i18n %}
{% get_current_language as LANGUAGE_CODE %}
<!DOCTYPE html>
<html lang="{{ LANGUAGE_CODE }}">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{% block title %}{% endblock title %} | Virtual Judge</title>
</head>
<body>
<header>
<nav>
<ul>
<li><a href="{% url 'login' %}">Login</a></li>
</ul>
</nav>
</header>

<main>
{% block content %}{% endblock content %}
</main>
</body>
</html>
13 changes: 13 additions & 0 deletions templates/pages/home.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{% extends "base.html" %}

{% block title %}Home{% endblock title %}

{% block content %}
<h1>Home</h1>

{% if user.is_authenticated %}
<p>Welcome, {{ user.username }}!</p>
{% else %}
<p>Welcome, new user!</p>
{% endif %}
{% endblock content %}
38 changes: 38 additions & 0 deletions templates/registration/login.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{% extends "base.html" %}

{% block title %}Login{% endblock title %}

{% block content %}
{% if form.errors %}
<p>Your username and password didn't match. Please try again.</p>
{% endif %}

{% if next %}
{% if user.is_authenticated %}
<p>Your account doesn't have access to this page. To proceed,
please login with an account that has access.</p>
{% else %}
<p>Please login to see this page.</p>
{% endif %}
{% endif %}

<form method="post" action="{% url 'login' %}">
{% csrf_token %}
<table>
<tr>
<td>{{ form.username.label_tag }}</td>
<td>{{ form.username }}</td>
</tr>
<tr>
<td>{{ form.password.label_tag }}</td>
<td>{{ form.password }}</td>
</tr>
</table>

<input type="submit" value="login">
<input type="hidden" name="next" value="{{ next }}">
</form>

{# Assumes you set up the password_reset view in your URLconf #}
<p><a href="{% url 'password_reset' %}">Lost password?</a></p>
{% endblock %}

0 comments on commit 8ad63cf

Please sign in to comment.