latest changes
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
from django import forms
|
||||
from .models import Card, Column, Profile, Project, Tag, Membership
|
||||
from django.contrib.auth import get_user_model
|
||||
from zoneinfo import ZoneInfo, ZoneInfoNotFoundError
|
||||
|
||||
|
||||
class ProjectForm(forms.ModelForm):
|
||||
@@ -62,3 +63,11 @@ class ProfileForm(forms.ModelForm):
|
||||
class Meta:
|
||||
model = Profile
|
||||
fields = ['display_name', 'avatar_url', 'timezone', 'language', 'theme']
|
||||
|
||||
def clean_timezone(self):
|
||||
value = self.cleaned_data['timezone'].strip()
|
||||
try:
|
||||
ZoneInfo(value)
|
||||
except (ZoneInfoNotFoundError, ValueError):
|
||||
raise forms.ValidationError('Enter a valid IANA timezone, such as Europe/Amsterdam.')
|
||||
return value
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
from zoneinfo import ZoneInfo, ZoneInfoNotFoundError
|
||||
|
||||
from django.utils import timezone
|
||||
|
||||
from .models import Profile
|
||||
|
||||
|
||||
class ProfileTimezoneMiddleware:
|
||||
def __init__(self, get_response):
|
||||
self.get_response = get_response
|
||||
|
||||
def __call__(self, request):
|
||||
if request.user.is_authenticated:
|
||||
name = Profile.objects.filter(user=request.user).values_list('timezone', flat=True).first()
|
||||
if name:
|
||||
try:
|
||||
timezone.activate(ZoneInfo(name))
|
||||
except (ZoneInfoNotFoundError, ValueError):
|
||||
timezone.deactivate()
|
||||
else:
|
||||
timezone.deactivate()
|
||||
else:
|
||||
timezone.deactivate()
|
||||
try:
|
||||
return self.get_response(request)
|
||||
finally:
|
||||
timezone.deactivate()
|
||||
@@ -1,12 +1,13 @@
|
||||
{% extends 'core/base.html' %}
|
||||
{% block content %}
|
||||
<style>.htmx-indicator{display:none}.htmx-request.htmx-indicator,.htmx-request .htmx-indicator{display:inline}</style>
|
||||
<div class="d-flex justify-content-between align-items-center mb-3">
|
||||
<div><a href="{% url 'projects:list' %}">All projects</a><h1>{{ project.name }}</h1><p>{{ project.description }}</p>{% if project.git_repository_url %}<a href="{{ project.git_repository_url }}" rel="noopener noreferrer">Repository</a>{% endif %}<p>{{ done_count }} of {{ total_count }} cards done</p></div>
|
||||
{% if can_edit %}<div><a class="btn btn-outline-secondary" href="{% url 'projects:edit' project.id %}">Edit project</a> {% if project.archived %}<form class="d-inline" method="post" action="{% url 'projects:restore' project.id %}">{% csrf_token %}<button class="btn btn-outline-success">Restore project</button></form>{% else %}<form class="d-inline" method="post" action="{% url 'projects:archive' project.id %}">{% csrf_token %}<button class="btn btn-outline-danger">Archive project</button></form> <a class="btn btn-success" href="{% url 'projects:card_create' project.id %}">New card</a>{% endif %}</div>{% endif %}
|
||||
</div>
|
||||
<div class="d-flex gap-3 flex-wrap mb-3">{% for label,count in priority_counts %}<span class="badge text-bg-light border">{{ label }}: {{ count }} open</span>{% endfor %}</div>
|
||||
<form class="row g-2 mb-3" method="get" role="search">
|
||||
<div class="col"><input class="form-control" name="q" value="{{ query }}" placeholder="Search cards and specifications" aria-label="Search cards" hx-get="{% url 'projects:board' project.id %}" hx-trigger="input changed delay:300ms" hx-target="#board-results" hx-include="closest form"></div>
|
||||
<div class="col"><input class="form-control" name="q" value="{{ query }}" placeholder="Search cards and specifications" aria-label="Search cards" hx-get="{% url 'projects:board' project.id %}" hx-trigger="input changed delay:300ms" hx-target="#board-results" hx-include="closest form" hx-indicator="#board-search-loading"></div>
|
||||
<div class="col-auto"><select class="form-select" name="priority" aria-label="Priority" hx-get="{% url 'projects:board' project.id %}" hx-trigger="change" hx-target="#board-results" hx-include="closest form"><option value="">All priorities</option><option value="high" {% if selected_priority == 'high' %}selected{% endif %}>High</option><option value="medium" {% if selected_priority == 'medium' %}selected{% endif %}>Medium</option><option value="low" {% if selected_priority == 'low' %}selected{% endif %}>Low</option></select></div>
|
||||
<div class="col-auto"><select class="form-select" name="card_type" aria-label="Card type" hx-get="{% url 'projects:board' project.id %}" hx-trigger="change" hx-target="#board-results" hx-include="closest form"><option value="">All types</option>{% for value,label in card_types %}<option value="{{ value }}" {% if value == selected_type %}selected{% endif %}>{{ label }}</option>{% endfor %}</select></div>
|
||||
<div class="col-auto"><select class="form-select" name="column" aria-label="Column" hx-get="{% url 'projects:board' project.id %}" hx-trigger="change" hx-target="#board-results" hx-include="closest form"><option value="">All columns</option>{% for choice in columns %}<option value="{{ choice.id }}" {% if choice.id|stringformat:'s' == selected_column %}selected{% endif %}>{{ choice.name }}</option>{% endfor %}</select></div>
|
||||
@@ -15,6 +16,7 @@
|
||||
<div class="col-auto"><label><input type="checkbox" name="archived" value="1" {% if show_archived %}checked{% endif %}> Archived</label></div>
|
||||
<div class="col-auto"><a class="btn btn-outline-secondary" href="{% url 'projects:board' project.id %}">Clear</a></div>
|
||||
</form>
|
||||
<span id="board-search-loading" class="htmx-indicator" role="status">Searching…</span>
|
||||
<div id="board-results">{% include 'projects/_board.html' %}</div>
|
||||
{% if can_edit %}<a class="btn btn-outline-secondary mt-3" href="{% url 'projects:column_create' project.id %}">Add column</a>
|
||||
<a class="btn btn-outline-secondary mt-3" href="{% url 'projects:tag_create' project.id %}">Add tag</a>{% endif %}
|
||||
|
||||
@@ -1 +1 @@
|
||||
{% extends 'core/base.html' %}{% block content %}<style>.htmx-indicator{display:none}.htmx-request.htmx-indicator,.htmx-request .htmx-indicator{display:inline}</style><div class="d-flex justify-content-between align-items-center mb-4"><div><h1>Projects</h1><p>Boards and specifications across your workspace.</p></div><a class="btn btn-success" href="{% url 'projects:create' %}">New project</a></div><p><a href="{% url 'projects:list' %}">Active</a> · <a href="{% url 'projects:list' %}?archived=1">Archived</a></p><form method="get" role="search" class="mb-3"><label for="project-search" class="form-label">Search projects and cards</label><input id="project-search" class="form-control" name="q" value="{{ query }}" hx-get="{% url 'projects:list' %}{% if show_archived %}?archived=1{% endif %}" hx-trigger="input changed delay:300ms" hx-target="#project-results" hx-indicator="#search-loading"><span id="search-loading" class="htmx-indicator">Searching…</span></form><div id="project-results">{% include 'projects/_project_list.html' %}</div><script src="https://unpkg.com/htmx.org@2.0.4"></script>{% endblock %}
|
||||
{% extends 'core/base.html' %}{% block content %}<style>.htmx-indicator{display:none}.htmx-request.htmx-indicator,.htmx-request .htmx-indicator{display:inline}</style><div class="d-flex justify-content-between align-items-center mb-4"><div><h1>Projects</h1><p>Boards and specifications across your workspace.</p></div><a class="btn btn-success" href="{% url 'projects:create' %}">New project</a></div><p><a href="{% url 'projects:list' %}">Active</a> · <a href="{% url 'projects:list' %}?archived=1">Archived</a></p><form method="get" role="search" class="mb-3"><label for="project-search" class="form-label">Search projects and cards</label><input id="project-search" class="form-control" name="q" value="{{ query }}" hx-get="{% url 'projects:list' %}{% if show_archived %}?archived=1{% endif %}" hx-trigger="input changed delay:300ms" hx-target="#project-results" hx-indicator="#search-loading"><span id="search-loading" class="htmx-indicator" role="status">Searching…</span> <a class="btn btn-sm btn-outline-secondary" href="{% url 'projects:list' %}{% if show_archived %}?archived=1{% endif %}">Clear</a></form><div id="project-results">{% include 'projects/_project_list.html' %}</div><script src="https://unpkg.com/htmx.org@2.0.4"></script>{% endblock %}
|
||||
|
||||
@@ -1 +1 @@
|
||||
<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Sign in · Common Ground</title><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/css/bootstrap.min.css"></head><body class="bg-light"><main class="container" style="max-width:28rem;margin-top:12vh"><h1>Common Ground</h1><p>Sign in to your dashboard.</p><form method="post" class="card card-body">{% csrf_token %}{{ form.as_p }}<input type="hidden" name="next" value="{{ next }}"><button class="btn btn-success">Sign in</button></form>{% if oidc_enabled %}<a class="btn btn-outline-primary mt-3" href="{% url 'oidc_authentication_init' %}">Sign in with your organization</a>{% endif %}</main></body></html>
|
||||
{% load static %}<!doctype html><html lang="en" data-theme="system"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Sign in · Common Ground</title><script>document.documentElement.dataset.theme=localStorage.getItem('dashboard-theme')||'system';</script><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/css/bootstrap.min.css"><link rel="stylesheet" href="{% static 'core/site.css' %}"><link rel="stylesheet" href="{% static 'core/theme-bridge.css' %}"></head><body><main class="container" style="max-width:28rem;margin-top:12vh"><h1>Common Ground</h1><p>Sign in to your dashboard.</p><form method="post" class="card card-body">{% csrf_token %}{{ form.as_p }}<input type="hidden" name="next" value="{{ next }}"><button class="btn btn-success">Sign in</button></form>{% if oidc_enabled %}<a class="btn btn-outline-primary mt-3" href="{% url 'oidc_authentication_init' %}">Sign in with your organization</a>{% endif %}</main></body></html>
|
||||
|
||||
@@ -10,7 +10,8 @@
|
||||
<h2 class="h5 mt-4">Connected sign-in identities</h2>
|
||||
{% for identity in identities %}<p>{{ identity.issuer }} · {{ identity.subject }}</p>{% empty %}<p>No external sign-in identity connected.</p>{% endfor %}
|
||||
<h2 class="h5 mt-4">MCP connections</h2>
|
||||
<p>Endpoint: <code>{{ mcp_url }}</code>. Authorize a registered client with <code>projects:read</code> and optionally <code>projects:write</code>. Client registration requires a signed-in account and a CSRF token for browser requests.</p>
|
||||
<p>Endpoint: <a href="{{ mcp_url }}"><code>{{ mcp_url }}</code></a>. Authorize a registered client with <code>projects:read</code> and optionally <code>projects:write</code>. Client registration requires a signed-in account and a CSRF token for browser requests.</p>
|
||||
{% if not mcp_configured %}<p class="alert alert-warning" role="status">This address differs from the configured OAuth resource URL. Set <code>MCP_PUBLIC_URL</code> to this HTTPS endpoint for both services before connecting a client.</p>{% endif %}
|
||||
{% for grant in grants %}
|
||||
<div class="card card-body mb-2" style="max-width:40rem"><strong>{{ grant.application.name|default:'Connected client' }}</strong><span>Permissions: {{ grant.scope }}</span><small>Expires {{ grant.expires }}</small><form method="post" action="{% url 'projects:revoke_connection' grant.id %}">{% csrf_token %}<button class="btn btn-sm btn-outline-danger mt-2">Revoke</button></form></div>
|
||||
{% empty %}<p>No active MCP clients.</p>{% endfor %}
|
||||
|
||||
@@ -52,7 +52,9 @@ class OAuthMcpTests(TestCase):
|
||||
self.assertIsNone(_verified(secret))
|
||||
|
||||
def test_discovery_and_project_access(self):
|
||||
self.assertEqual(self.client.get('/.well-known/oauth-authorization-server').status_code, 200)
|
||||
discovery = self.client.get('/.well-known/oauth-authorization-server')
|
||||
self.assertEqual(discovery.status_code, 200)
|
||||
self.assertIn('none', discovery.json()['token_endpoint_auth_methods_supported'])
|
||||
project = create_project(self.user, name='Private')
|
||||
self.assertEqual(self.client.get(f'/projects/{project.pk}/').status_code, 302)
|
||||
|
||||
|
||||
+42
-2
@@ -1,8 +1,12 @@
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.test import TestCase
|
||||
from django.test import TestCase, override_settings
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.urls import reverse
|
||||
from .models import Card, CardRevision, Membership
|
||||
from django.http import HttpResponse
|
||||
from django.utils import timezone
|
||||
from .models import Card, CardRevision, Membership, Profile
|
||||
from .forms import ProfileForm
|
||||
from .middleware import ProfileTimezoneMiddleware
|
||||
from .services import RevisionConflict, card_for, create_card, create_project, move_card, update_card, update_column
|
||||
|
||||
|
||||
@@ -80,6 +84,42 @@ class ProjectWorkflowTests(TestCase):
|
||||
self.client.force_login(self.owner)
|
||||
self.assertEqual(self.client.get('/').status_code, 200)
|
||||
|
||||
def test_profile_timezone_applies_to_requests(self):
|
||||
Profile.objects.create(user=self.owner, timezone='America/New_York')
|
||||
observed = []
|
||||
|
||||
def capture(request):
|
||||
observed.append(timezone.get_current_timezone_name())
|
||||
return HttpResponse('ok')
|
||||
|
||||
request = type('Request', (), {'user': self.owner})()
|
||||
response = ProfileTimezoneMiddleware(capture)(request)
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(observed, ['America/New_York'])
|
||||
self.assertEqual(timezone.get_current_timezone_name(), 'Europe/Amsterdam')
|
||||
self.assertFalse(ProfileForm(data={'timezone': 'Not/A_Timezone'}).is_valid())
|
||||
|
||||
def test_profile_mcp_url_uses_validated_https_origin(self):
|
||||
self.client.force_login(self.owner)
|
||||
public = 'https://projects.jorendegraaf.eu/mcp'
|
||||
headers = {'HTTP_HOST': 'projects.jorendegraaf.eu', 'HTTP_X_FORWARDED_PROTO': 'https'}
|
||||
with override_settings(MCP_PUBLIC_URL='https://other.example/mcp'):
|
||||
response = self.client.get(reverse('projects:profile'), **headers)
|
||||
self.assertEqual(response.context['mcp_url'], public)
|
||||
self.assertContains(response, 'This address differs from the configured OAuth resource URL.')
|
||||
with override_settings(MCP_PUBLIC_URL=public, OAUTH_ISSUER_URL='https://projects.jorendegraaf.eu/'):
|
||||
response = self.client.get(reverse('projects:profile'), **headers)
|
||||
self.assertEqual(response.context['mcp_url'], public)
|
||||
self.assertNotContains(response, 'This address differs from the configured OAuth resource URL.')
|
||||
metadata = self.client.get('/.well-known/oauth-authorization-server', **headers).json()
|
||||
self.assertEqual(metadata['issuer'], 'https://projects.jorendegraaf.eu/')
|
||||
|
||||
with override_settings(MCP_PUBLIC_URL='http://127.0.0.1:8766/mcp'):
|
||||
local = self.client.get(reverse('projects:profile'), HTTP_HOST='127.0.0.1:8765')
|
||||
self.assertEqual(local.context['mcp_url'], 'http://127.0.0.1:8766/mcp')
|
||||
rejected = self.client.get(reverse('projects:profile'), HTTP_HOST='untrusted.example')
|
||||
self.assertEqual(rejected.status_code, 400)
|
||||
|
||||
def test_login_throttles_failed_attempts(self):
|
||||
for _ in range(5):
|
||||
self.assertEqual(self.client.post('/accounts/login/', {'username': 'owner', 'password': 'wrong'}).status_code, 200)
|
||||
|
||||
+6
-1
@@ -4,6 +4,7 @@ from django.contrib.auth.views import LoginView
|
||||
from django.core.cache import cache
|
||||
from django.http import HttpResponse
|
||||
import hashlib
|
||||
from urllib.parse import urlsplit
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.db.models import Q, Count
|
||||
from django.core.paginator import Paginator
|
||||
@@ -300,7 +301,11 @@ def profile(request):
|
||||
request.user.save(update_fields=['email'])
|
||||
return redirect('projects:profile')
|
||||
grants = get_access_token_model().objects.filter(user=request.user, expires__gt=timezone.now()).select_related('application').order_by('-created')
|
||||
return render(request, 'projects/profile.html', {'form': form, 'grants': grants, 'identities': request.user.oauth_identities.all(), 'mcp_url': settings.MCP_PUBLIC_URL, 'oidc_enabled': settings.OIDC_ENABLED, 'title': 'Profile', 'section': 'profile'})
|
||||
visiting_url = request.build_absolute_uri('/mcp')
|
||||
# Local development uses a separate MCP port; public deployments route /mcp through HTTPS.
|
||||
mcp_url = settings.MCP_PUBLIC_URL if urlsplit(visiting_url).hostname in {'localhost', '127.0.0.1', '::1'} else visiting_url
|
||||
mcp_configured = mcp_url.rstrip('/') == settings.MCP_PUBLIC_URL.rstrip('/')
|
||||
return render(request, 'projects/profile.html', {'form': form, 'grants': grants, 'identities': request.user.oauth_identities.all(), 'mcp_url': mcp_url, 'mcp_configured': mcp_configured, 'oidc_enabled': settings.OIDC_ENABLED, 'title': 'Profile', 'section': 'profile'})
|
||||
|
||||
|
||||
@login_required
|
||||
|
||||
Reference in New Issue
Block a user