Private
Public Access
Scaffold Django project and add first-run auth flow
Sets up the Evection project structure: split settings supporting both SQLite and PostgreSQL via DATABASE_URL, Celery+Redis wiring, and the planned app layout (accounts, feeds, articles, rules, search, reader, pwa, core). Implements multi-user auth with a FreshRSS-style first-run setup wizard, login/logout, and admin-only user management, covered by automated tests. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
986206fe40
commit
c635cd9330
84 files changed
+897
No files matched your search
Whitespace-only changes.
Whitespace-only changes.
@@ -0,0 +1,9 @@
|
||||
from django.contrib import admin
|
||||
from django.contrib.auth.admin import UserAdmin as DjangoUserAdmin
|
||||
|
||||
from .models import User
|
||||
|
||||
|
||||
@admin.register(User)
|
||||
class UserAdmin(DjangoUserAdmin):
|
||||
fieldsets = DjangoUserAdmin.fieldsets + (("Evection", {"fields": ("timezone",)}),)
|
||||
@@ -0,0 +1,6 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class AccountsConfig(AppConfig):
|
||||
default_auto_field = "django.db.models.BigAutoField"
|
||||
name = "apps.accounts"
|
||||
@@ -0,0 +1,30 @@
|
||||
from django import forms
|
||||
from django.contrib.auth.forms import UsernameField
|
||||
|
||||
from .models import User
|
||||
|
||||
|
||||
class SetupForm(forms.ModelForm):
|
||||
password = forms.CharField(widget=forms.PasswordInput, min_length=8)
|
||||
password_confirm = forms.CharField(widget=forms.PasswordInput, min_length=8)
|
||||
|
||||
class Meta:
|
||||
model = User
|
||||
fields = ["username", "email"]
|
||||
field_classes = {"username": UsernameField}
|
||||
|
||||
def clean(self):
|
||||
cleaned_data = super().clean()
|
||||
if cleaned_data.get("password") != cleaned_data.get("password_confirm"):
|
||||
raise forms.ValidationError("Passwords do not match.")
|
||||
return cleaned_data
|
||||
|
||||
|
||||
class CreateUserForm(forms.ModelForm):
|
||||
password = forms.CharField(widget=forms.PasswordInput, min_length=8)
|
||||
|
||||
class Meta:
|
||||
model = User
|
||||
fields = ["username", "email", "is_staff"]
|
||||
field_classes = {"username": UsernameField}
|
||||
labels = {"is_staff": "Grant admin access"}
|
||||
@@ -0,0 +1,33 @@
|
||||
from django.conf import settings
|
||||
from django.shortcuts import redirect
|
||||
from django.urls import reverse
|
||||
|
||||
from .models import User
|
||||
|
||||
_SETUP_EXEMPT_PREFIXES = (settings.STATIC_URL,)
|
||||
|
||||
|
||||
class SetupWizardMiddleware:
|
||||
"""Force every request to the first-run setup wizard until an account exists.
|
||||
|
||||
Once any User row exists, /setup/ becomes unreachable again (see the
|
||||
setup view), mirroring FreshRSS's one-time install wizard. The "a user
|
||||
exists" check only ever flips false -> true for the lifetime of an
|
||||
instance, so it's cached per-process after the first positive result to
|
||||
avoid a query on every request.
|
||||
"""
|
||||
|
||||
_a_user_exists = False
|
||||
|
||||
def __init__(self, get_response):
|
||||
self.get_response = get_response
|
||||
|
||||
def __call__(self, request):
|
||||
path = request.path
|
||||
if not path.startswith(_SETUP_EXEMPT_PREFIXES):
|
||||
if not SetupWizardMiddleware._a_user_exists:
|
||||
SetupWizardMiddleware._a_user_exists = User.objects.exists()
|
||||
setup_url = reverse("accounts:setup")
|
||||
if path != setup_url and not SetupWizardMiddleware._a_user_exists:
|
||||
return redirect(setup_url)
|
||||
return self.get_response(request)
|
||||
@@ -0,0 +1,119 @@
|
||||
# Generated by Django 5.2.18 on 2026-10-30 00:01
|
||||
|
||||
import django.contrib.auth.models
|
||||
import django.contrib.auth.validators
|
||||
import django.utils.timezone
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
("auth", "0012_alter_user_first_name_max_length"),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name="User",
|
||||
fields=[
|
||||
(
|
||||
"id",
|
||||
models.BigAutoField(
|
||||
auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
|
||||
),
|
||||
),
|
||||
("password", models.CharField(max_length=128, verbose_name="password")),
|
||||
(
|
||||
"last_login",
|
||||
models.DateTimeField(blank=True, null=True, verbose_name="last login"),
|
||||
),
|
||||
(
|
||||
"is_superuser",
|
||||
models.BooleanField(
|
||||
default=False,
|
||||
help_text="Designates that this user has all permissions without explicitly assigning them.",
|
||||
verbose_name="superuser status",
|
||||
),
|
||||
),
|
||||
(
|
||||
"username",
|
||||
models.CharField(
|
||||
error_messages={"unique": "A user with that username already exists."},
|
||||
help_text="Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.",
|
||||
max_length=150,
|
||||
unique=True,
|
||||
validators=[django.contrib.auth.validators.UnicodeUsernameValidator()],
|
||||
verbose_name="username",
|
||||
),
|
||||
),
|
||||
(
|
||||
"first_name",
|
||||
models.CharField(blank=True, max_length=150, verbose_name="first name"),
|
||||
),
|
||||
(
|
||||
"last_name",
|
||||
models.CharField(blank=True, max_length=150, verbose_name="last name"),
|
||||
),
|
||||
(
|
||||
"email",
|
||||
models.EmailField(blank=True, max_length=254, verbose_name="email address"),
|
||||
),
|
||||
(
|
||||
"is_staff",
|
||||
models.BooleanField(
|
||||
default=False,
|
||||
help_text="Designates whether the user can log into this admin site.",
|
||||
verbose_name="staff status",
|
||||
),
|
||||
),
|
||||
(
|
||||
"is_active",
|
||||
models.BooleanField(
|
||||
default=True,
|
||||
help_text="Designates whether this user should be treated as active. Unselect this instead of deleting accounts.",
|
||||
verbose_name="active",
|
||||
),
|
||||
),
|
||||
(
|
||||
"date_joined",
|
||||
models.DateTimeField(
|
||||
default=django.utils.timezone.now, verbose_name="date joined"
|
||||
),
|
||||
),
|
||||
("timezone", models.CharField(default="UTC", max_length=64)),
|
||||
("created_at", models.DateTimeField(auto_now_add=True)),
|
||||
(
|
||||
"groups",
|
||||
models.ManyToManyField(
|
||||
blank=True,
|
||||
help_text="The groups this user belongs to. A user will get all permissions granted to each of their groups.",
|
||||
related_name="user_set",
|
||||
related_query_name="user",
|
||||
to="auth.group",
|
||||
verbose_name="groups",
|
||||
),
|
||||
),
|
||||
(
|
||||
"user_permissions",
|
||||
models.ManyToManyField(
|
||||
blank=True,
|
||||
help_text="Specific permissions for this user.",
|
||||
related_name="user_set",
|
||||
related_query_name="user",
|
||||
to="auth.permission",
|
||||
verbose_name="user permissions",
|
||||
),
|
||||
),
|
||||
],
|
||||
options={
|
||||
"verbose_name": "user",
|
||||
"verbose_name_plural": "users",
|
||||
"abstract": False,
|
||||
},
|
||||
managers=[
|
||||
("objects", django.contrib.auth.models.UserManager()),
|
||||
],
|
||||
),
|
||||
]
|
||||
Whitespace-only changes.
@@ -0,0 +1,7 @@
|
||||
from django.contrib.auth.models import AbstractUser
|
||||
from django.db import models
|
||||
|
||||
|
||||
class User(AbstractUser):
|
||||
timezone = models.CharField(max_length=64, default="UTC")
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
@@ -0,0 +1,92 @@
|
||||
import pytest
|
||||
from django.urls import reverse
|
||||
|
||||
from .middleware import SetupWizardMiddleware
|
||||
from .models import User
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def _reset_setup_wizard_cache():
|
||||
SetupWizardMiddleware._a_user_exists = False
|
||||
yield
|
||||
SetupWizardMiddleware._a_user_exists = False
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_root_redirects_to_setup_when_no_users_exist(client):
|
||||
response = client.get("/", follow=False)
|
||||
assert response.status_code == 302
|
||||
assert response.url == reverse("accounts:setup")
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_setup_creates_admin_and_logs_in(client):
|
||||
response = client.post(
|
||||
reverse("accounts:setup"),
|
||||
{
|
||||
"username": "keith",
|
||||
"email": "keith@example.com",
|
||||
"password": "correct-horse-battery",
|
||||
"password_confirm": "correct-horse-battery",
|
||||
},
|
||||
)
|
||||
assert response.status_code == 302
|
||||
assert response.url == reverse("reader:stream")
|
||||
|
||||
user = User.objects.get(username="keith")
|
||||
assert user.is_staff
|
||||
assert user.is_superuser
|
||||
|
||||
stream_response = client.get(reverse("reader:stream"))
|
||||
assert stream_response.status_code == 200
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_setup_is_unreachable_once_a_user_exists(client):
|
||||
User.objects.create_user("existing", password="irrelevant")
|
||||
|
||||
response = client.get(reverse("accounts:setup"))
|
||||
assert response.status_code == 302
|
||||
assert response.url == reverse("accounts:login")
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_setup_rejects_mismatched_passwords(client):
|
||||
response = client.post(
|
||||
reverse("accounts:setup"),
|
||||
{
|
||||
"username": "keith",
|
||||
"email": "keith@example.com",
|
||||
"password": "correct-horse-battery",
|
||||
"password_confirm": "does-not-match",
|
||||
},
|
||||
)
|
||||
assert response.status_code == 200
|
||||
assert not User.objects.exists()
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_non_admin_cannot_manage_users(client):
|
||||
User.objects.create_user("regular", password="pw-regular-123")
|
||||
client.login(username="regular", password="pw-regular-123")
|
||||
|
||||
response = client.get(reverse("accounts:manage_users"))
|
||||
assert response.status_code == 302 # user_passes_test redirects non-staff
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_admin_can_create_user(client):
|
||||
admin = User.objects.create_user("admin", password="pw-admin-123", is_staff=True)
|
||||
client.login(username="admin", password="pw-admin-123")
|
||||
|
||||
response = client.post(
|
||||
reverse("accounts:create_user"),
|
||||
{
|
||||
"username": "newperson",
|
||||
"email": "newperson@example.com",
|
||||
"password": "pw-newperson-123",
|
||||
},
|
||||
)
|
||||
assert response.status_code == 302
|
||||
assert User.objects.filter(username="newperson").exists()
|
||||
assert admin.pk # sanity: fixture user untouched
|
||||
@@ -0,0 +1,18 @@
|
||||
from django.contrib.auth import views as auth_views
|
||||
from django.urls import path
|
||||
|
||||
from . import views
|
||||
|
||||
app_name = "accounts"
|
||||
|
||||
urlpatterns = [
|
||||
path("setup/", views.setup, name="setup"),
|
||||
path(
|
||||
"login/",
|
||||
auth_views.LoginView.as_view(template_name="accounts/login.html"),
|
||||
name="login",
|
||||
),
|
||||
path("logout/", auth_views.LogoutView.as_view(), name="logout"),
|
||||
path("users/", views.manage_users, name="manage_users"),
|
||||
path("users/create/", views.create_user, name="create_user"),
|
||||
]
|
||||
@@ -0,0 +1,47 @@
|
||||
from django.contrib import messages
|
||||
from django.contrib.auth import login
|
||||
from django.contrib.auth.decorators import user_passes_test
|
||||
from django.shortcuts import redirect, render
|
||||
|
||||
from .forms import CreateUserForm, SetupForm
|
||||
from .middleware import SetupWizardMiddleware
|
||||
from .models import User
|
||||
|
||||
is_admin = user_passes_test(lambda u: u.is_authenticated and u.is_staff)
|
||||
|
||||
|
||||
def setup(request):
|
||||
if User.objects.exists():
|
||||
return redirect("accounts:login")
|
||||
|
||||
form = SetupForm(request.POST or None)
|
||||
if request.method == "POST" and form.is_valid():
|
||||
user = form.save(commit=False)
|
||||
user.set_password(form.cleaned_data["password"])
|
||||
user.is_staff = True
|
||||
user.is_superuser = True
|
||||
user.save()
|
||||
SetupWizardMiddleware._a_user_exists = True
|
||||
login(request, user)
|
||||
messages.success(request, "Welcome to Evection! Your admin account is ready.")
|
||||
return redirect("reader:stream")
|
||||
|
||||
return render(request, "accounts/setup.html", {"form": form})
|
||||
|
||||
|
||||
@is_admin
|
||||
def manage_users(request):
|
||||
users = User.objects.order_by("username")
|
||||
return render(request, "accounts/manage_users.html", {"users": users})
|
||||
|
||||
|
||||
@is_admin
|
||||
def create_user(request):
|
||||
form = CreateUserForm(request.POST or None)
|
||||
if request.method == "POST" and form.is_valid():
|
||||
user = form.save(commit=False)
|
||||
user.set_password(form.cleaned_data["password"])
|
||||
user.save()
|
||||
messages.success(request, f"Account '{user.username}' created.")
|
||||
return redirect("accounts:manage_users")
|
||||
return render(request, "accounts/create_user.html", {"form": form})
|
||||
Whitespace-only changes.
@@ -0,0 +1 @@
|
||||
# Register your models here.
|
||||
@@ -0,0 +1,6 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class ArticlesConfig(AppConfig):
|
||||
default_auto_field = "django.db.models.BigAutoField"
|
||||
name = "apps.articles"
|
||||
Whitespace-only changes.
@@ -0,0 +1 @@
|
||||
# Create your models here.
|
||||
@@ -0,0 +1 @@
|
||||
# Create your tests here.
|
||||
@@ -0,0 +1 @@
|
||||
# Create your views here.
|
||||
Whitespace-only changes.
@@ -0,0 +1 @@
|
||||
# Register your models here.
|
||||
@@ -0,0 +1,6 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class CoreConfig(AppConfig):
|
||||
default_auto_field = "django.db.models.BigAutoField"
|
||||
name = "apps.core"
|
||||
Whitespace-only changes.
@@ -0,0 +1 @@
|
||||
# Create your models here.
|
||||
@@ -0,0 +1 @@
|
||||
# Create your tests here.
|
||||
@@ -0,0 +1 @@
|
||||
# Create your views here.
|
||||
Whitespace-only changes.
@@ -0,0 +1 @@
|
||||
# Register your models here.
|
||||
@@ -0,0 +1,6 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class FeedsConfig(AppConfig):
|
||||
default_auto_field = "django.db.models.BigAutoField"
|
||||
name = "apps.feeds"
|
||||
Whitespace-only changes.
@@ -0,0 +1 @@
|
||||
# Create your models here.
|
||||
@@ -0,0 +1 @@
|
||||
# Create your tests here.
|
||||
@@ -0,0 +1 @@
|
||||
# Create your views here.
|
||||
Whitespace-only changes.
@@ -0,0 +1 @@
|
||||
# Register your models here.
|
||||
@@ -0,0 +1,6 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class PwaConfig(AppConfig):
|
||||
default_auto_field = "django.db.models.BigAutoField"
|
||||
name = "apps.pwa"
|
||||
Whitespace-only changes.
@@ -0,0 +1 @@
|
||||
# Create your models here.
|
||||
@@ -0,0 +1 @@
|
||||
# Create your tests here.
|
||||
@@ -0,0 +1 @@
|
||||
# Create your views here.
|
||||
Whitespace-only changes.
@@ -0,0 +1 @@
|
||||
# Register your models here.
|
||||
@@ -0,0 +1,6 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class ReaderConfig(AppConfig):
|
||||
default_auto_field = "django.db.models.BigAutoField"
|
||||
name = "apps.reader"
|
||||
Whitespace-only changes.
@@ -0,0 +1 @@
|
||||
# Create your models here.
|
||||
@@ -0,0 +1 @@
|
||||
# Create your tests here.
|
||||
@@ -0,0 +1,9 @@
|
||||
from django.urls import path
|
||||
|
||||
from . import views
|
||||
|
||||
app_name = "reader"
|
||||
|
||||
urlpatterns = [
|
||||
path("", views.stream, name="stream"),
|
||||
]
|
||||
@@ -0,0 +1,7 @@
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.shortcuts import render
|
||||
|
||||
|
||||
@login_required
|
||||
def stream(request):
|
||||
return render(request, "reader/stream.html")
|
||||
Whitespace-only changes.
@@ -0,0 +1 @@
|
||||
# Register your models here.
|
||||
@@ -0,0 +1,6 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class RulesConfig(AppConfig):
|
||||
default_auto_field = "django.db.models.BigAutoField"
|
||||
name = "apps.rules"
|
||||
Whitespace-only changes.
@@ -0,0 +1 @@
|
||||
# Create your models here.
|
||||
@@ -0,0 +1 @@
|
||||
# Create your tests here.
|
||||
@@ -0,0 +1 @@
|
||||
# Create your views here.
|
||||
Whitespace-only changes.
@@ -0,0 +1 @@
|
||||
# Register your models here.
|
||||
@@ -0,0 +1,6 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class SearchConfig(AppConfig):
|
||||
default_auto_field = "django.db.models.BigAutoField"
|
||||
name = "apps.search"
|
||||
Whitespace-only changes.
@@ -0,0 +1 @@
|
||||
# Create your models here.
|
||||
@@ -0,0 +1 @@
|
||||
# Create your tests here.
|
||||
@@ -0,0 +1 @@
|
||||
# Create your views here.
|
||||
Reference in new issue
Block a user