Skip to content

Add communities #4404

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added backend/community/__init__.py
Empty file.
58 changes: 58 additions & 0 deletions backend/community/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
from django.contrib import admin
from django.http.request import HttpRequest

from community.models import Community, CommunityMember, Link


class LinkInline(admin.TabularInline):
model = Link


class CommunityMemberInline(admin.TabularInline):
model = CommunityMember
autocomplete_fields = ("user",)


@admin.register(Community)
class CommunityAdmin(admin.ModelAdmin):
list_display = ("name", "hostname", "description")
inlines = [LinkInline, CommunityMemberInline]
fieldsets = (
(
"Community",
{
"fields": (
"name",
"hostname",
"description",
),
},
),
(
"Landing Page",
{
"fields": (
"landing_page_primary_color",
"landing_page_secondary_color",
"landing_page_hover_color",
"landing_page_custom_logo_svg",
),
},
),
)

def has_change_permission(self, request: HttpRequest, obj=None) -> bool:
if not obj:
return False

if request.user.is_superuser:
return True

if (
obj.members.all()
.filter(user=request.user, role=CommunityMember.Role.ADMIN)
.exists()
):
return True

return False
6 changes: 6 additions & 0 deletions backend/community/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class CommunityConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "community"
66 changes: 66 additions & 0 deletions backend/community/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Generated by Django 5.1.4 on 2025-04-20 21:07

import colorfield.fields
import django.db.models.deletion
import django.utils.timezone
import model_utils.fields
from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
migrations.CreateModel(
name='Community',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('created', model_utils.fields.AutoCreatedField(default=django.utils.timezone.now, editable=False, verbose_name='created')),
('modified', model_utils.fields.AutoLastModifiedField(default=django.utils.timezone.now, editable=False, verbose_name='modified')),
('name', models.CharField(max_length=300)),
('hostname', models.CharField(max_length=256)),
('description', models.TextField()),
('landing_page_primary_color', colorfield.fields.ColorField(blank=True, default=None, help_text='Used for the background of the landing page. Depending on the contrast, it will be used to decide if the text should be white or black.', image_field=None, max_length=25, null=True, samples=None)),
('landing_page_secondary_color', colorfield.fields.ColorField(blank=True, default=None, help_text='Used for the logo color, borders and, if not specified, the hover effect of the links.', image_field=None, max_length=25, null=True, samples=None)),
('landing_page_hover_color', colorfield.fields.ColorField(blank=True, default=None, help_text='Optional. Used when hovering the links. If empty, it will use the secondary color.', image_field=None, max_length=25, null=True, samples=None)),
('landing_page_custom_logo_svg', models.TextField(blank=True, help_text='Optional. If empty, it will use the Python Italia logo with the community name below. Copy your SVG code here.', null=True)),
],
options={
'verbose_name_plural': 'Communities',
},
),
migrations.CreateModel(
name='CommunityMember',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('created', model_utils.fields.AutoCreatedField(default=django.utils.timezone.now, editable=False, verbose_name='created')),
('modified', model_utils.fields.AutoLastModifiedField(default=django.utils.timezone.now, editable=False, verbose_name='modified')),
('role', models.CharField(choices=[('ADMIN', 'Admin')], default='ADMIN', max_length=300)),
('community', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='members', to='community.community')),
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='community_memberships', to=settings.AUTH_USER_MODEL)),
],
options={
'abstract': False,
},
),
migrations.CreateModel(
name='Link',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('created', model_utils.fields.AutoCreatedField(default=django.utils.timezone.now, editable=False, verbose_name='created')),
('modified', model_utils.fields.AutoLastModifiedField(default=django.utils.timezone.now, editable=False, verbose_name='modified')),
('url', models.URLField()),
('label', models.CharField(max_length=300)),
('community', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='links', to='community.community')),
],
options={
'abstract': False,
},
),
]
Empty file.
101 changes: 101 additions & 0 deletions backend/community/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import logging
from django.conf import settings
from django.db import models
import httpx
from model_utils.models import TimeStampedModel
from colorfield.fields import ColorField
from django.db import transaction


logger = logging.getLogger(__name__)


class Community(TimeStampedModel):
name = models.CharField(max_length=300)
hostname = models.CharField(max_length=256)
description = models.TextField()

landing_page_primary_color = ColorField(
blank=True,
null=True,
help_text=(
"Used for the background of the landing page. "
"Depending on the contrast, it will be used to decide if "
"the text should be white or black."
),
)
landing_page_secondary_color = ColorField(
blank=True,
null=True,
help_text=(
"Used for the logo color, borders "
"and, if not specified, the hover effect of the links."
),
)
landing_page_hover_color = ColorField(
blank=True,
null=True,
help_text="Optional. Used when hovering the links. If empty, it will use the secondary color.",
)
landing_page_custom_logo_svg = models.TextField(
blank=True,
null=True,
help_text=(
"Optional. If empty, it will use the Python Italia logo with the community name below. "
"Copy your SVG code here."
),
)

def save(self, *args, **kwargs):
super().save(*args, **kwargs)
transaction.on_commit(self.revalidate_landing_page)

def revalidate_landing_page(self):
# this is very bad :)
try:
for _ in range(3):
response = httpx.post(
f"https://{self.hostname}/api/revalidate/",
json={
"path": f"/{self.hostname}",
"secret": settings.REVALIDATE_SECRET,
},
)

if response.status_code == 200:
break

response.raise_for_status()
except Exception as e:
logger.exception("Error while revalidating landing page", exc_info=e)
pass

def __str__(self) -> str:
return self.name

class Meta:
verbose_name_plural = "Communities"


class CommunityMember(TimeStampedModel):
class Role(models.TextChoices):
ADMIN = "ADMIN"

user = models.ForeignKey(
"users.User", on_delete=models.CASCADE, related_name="community_memberships"
)
community = models.ForeignKey(
Community, on_delete=models.CASCADE, related_name="members"
)
role = models.CharField(choices=Role.choices, default=Role.ADMIN, max_length=300)


class Link(TimeStampedModel):
community = models.ForeignKey(
Community, on_delete=models.CASCADE, related_name="links"
)
url = models.URLField()
label = models.CharField(max_length=300)

def __str__(self) -> str:
return f"{self.label} - {self.url}"
1 change: 1 addition & 0 deletions backend/community/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Create your views here.
2 changes: 2 additions & 0 deletions backend/pycon/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
"modelcluster",
"taggit",
"wagtail_headless_preview",
"colorfield",
# --
"schedule.apps.ScheduleConfig",
"custom_admin",
Expand Down Expand Up @@ -129,6 +130,7 @@
"billing.apps.BillingConfig",
"privacy_policy.apps.PrivacyPolicyConfig",
"visa.apps.VisaConfig",
"community.apps.CommunityConfig",
]

MIDDLEWARE = [
Expand Down
1 change: 1 addition & 0 deletions backend/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ dependencies = [
"weasyprint>=63.1",
"opencv-python-headless>=4.10.0.84",
"psycopg[c]>=3.2.3",
"django-colorfield>=0.13.0",
]
name = "backend"
version = "0.1.0"
Expand Down
17 changes: 17 additions & 0 deletions backend/uv.lock

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

Loading