Skip to content

Commit 73dec0a

Browse files
committed
make a simple event management/registration system
1 parent 2b9f907 commit 73dec0a

17 files changed

+406
-147
lines changed

src/haps/__init__.py

Whitespace-only changes.

src/haps/admin.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from django.contrib import admin
2+
from .models import EventRegistration, Event
3+
4+
5+
@admin.register(Event)
6+
class EventAdmin(admin.ModelAdmin):
7+
search_fields = ["name"]
8+
list_display = [
9+
"name",
10+
"venue",
11+
"start_time",
12+
"accept_reg",
13+
"show_on_home",
14+
"event_page",
15+
]
16+
list_filter = ["show_on_home", "accept_reg"]
17+
fields = [
18+
"name",
19+
"description",
20+
"cover_image",
21+
"venue",
22+
"start_time",
23+
"end_time",
24+
"accept_reg",
25+
"show_on_home",
26+
]
27+
28+
29+
@admin.register(EventRegistration)
30+
class EventRegistrationAdmin(admin.ModelAdmin):
31+
search_fields = ["event", "user"]
32+
list_display = ["user", "event"]
33+
list_filter = ["event__name"]

src/haps/apps.py

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from django.apps import AppConfig
2+
3+
4+
class HapsConfig(AppConfig):
5+
default_auto_field = "django.db.models.BigAutoField"
6+
name = "haps"

src/haps/migrations/0001_initial.py

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# Generated by Django 4.2.1 on 2023-12-23 19:40
2+
3+
from django.conf import settings
4+
from django.db import migrations, models
5+
import django.db.models.deletion
6+
import utils.images
7+
8+
9+
class Migration(migrations.Migration):
10+
initial = True
11+
12+
dependencies = [
13+
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
14+
]
15+
16+
operations = [
17+
migrations.CreateModel(
18+
name="Event",
19+
fields=[
20+
(
21+
"id",
22+
models.BigAutoField(
23+
auto_created=True,
24+
primary_key=True,
25+
serialize=False,
26+
verbose_name="ID",
27+
),
28+
),
29+
("name", models.CharField(max_length=256)),
30+
("slug", models.SlugField(unique=True)),
31+
("description", models.TextField(max_length=4096)),
32+
(
33+
"cover_image",
34+
models.ImageField(
35+
blank=True, null=True, upload_to=utils.images.upload_image_to
36+
),
37+
),
38+
("venue", models.CharField(max_length=128)),
39+
("start_time", models.DateField()),
40+
("end_time", models.DateField(blank=True, null=True)),
41+
(
42+
"accept_reg",
43+
models.BooleanField(verbose_name="Accepting registrations ?"),
44+
),
45+
(
46+
"show_on_home",
47+
models.BooleanField(verbose_name="Show on Home Page ?"),
48+
),
49+
],
50+
),
51+
migrations.CreateModel(
52+
name="EventRegistration",
53+
fields=[
54+
(
55+
"id",
56+
models.BigAutoField(
57+
auto_created=True,
58+
primary_key=True,
59+
serialize=False,
60+
verbose_name="ID",
61+
),
62+
),
63+
(
64+
"event",
65+
models.ForeignKey(
66+
on_delete=django.db.models.deletion.CASCADE, to="haps.event"
67+
),
68+
),
69+
(
70+
"user",
71+
models.ForeignKey(
72+
on_delete=django.db.models.deletion.CASCADE,
73+
to=settings.AUTH_USER_MODEL,
74+
),
75+
),
76+
],
77+
),
78+
]

src/haps/migrations/__init__.py

Whitespace-only changes.

src/haps/models.py

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from django.db import models
2+
from django.contrib.auth import get_user_model
3+
from django.utils.html import format_html
4+
from utils.images import upload_image_to
5+
from utils.slugs import generate_unique_slug
6+
7+
User = get_user_model()
8+
9+
10+
class Event(models.Model):
11+
name = models.CharField(max_length=256)
12+
slug = models.SlugField(unique=True, blank=True)
13+
description = models.TextField(max_length=4096)
14+
cover_image = models.ImageField(upload_to=upload_image_to, blank=True, null=True)
15+
venue = models.CharField(max_length=128)
16+
start_time = models.DateField()
17+
end_time = models.DateField(null=True, blank=True)
18+
accept_reg = models.BooleanField(verbose_name="Accepting registrations ?")
19+
show_on_home = models.BooleanField(verbose_name="Show on Home Page ?")
20+
21+
def __str__(self):
22+
return self.name + str(self.start_time)
23+
24+
def save(self, *args, **kwargs):
25+
if self.slug == "":
26+
self.slug = generate_unique_slug(self.name, Event)
27+
super().save(args, kwargs)
28+
29+
def get_absolute_url(self):
30+
return f"/events/e/{self.slug}"
31+
32+
def event_page(self):
33+
return format_html(f'<a href="{self.get_absolute_url()}" target="_blank">View Page</a>')
34+
35+
36+
class EventRegistration(models.Model):
37+
event = models.ForeignKey(Event, on_delete=models.CASCADE)
38+
user = models.ForeignKey(User, on_delete=models.CASCADE)
39+
40+
def __str__(self) -> str:
41+
return self.user.__str__() + "%" + self.event.__str__()

src/haps/templates/haps/item.html

Whitespace-only changes.

src/haps/templates/haps/list.html

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
{% extends "commons.html" %}
2+
{% block title %} Events {% endblock title %}
3+
{%block content %}
4+
5+
6+
<div class="flex justify-center flex-wrap">
7+
8+
{%for hap in haps %}
9+
10+
<div
11+
class="max-w-sm bg-white border border-gray-200 rounded-lg shadow dark:bg-gray-800 dark:border-gray-700 mb-4 mt-4 ml-4 mr-4"
12+
>
13+
<a href="#">
14+
<img class="rounded-t-lg" src="{{hap.cover_image.url}}" alt="" />
15+
</a>
16+
<div class="p-5">
17+
<a href="#">
18+
<h5
19+
class="mb-2 text-2xl font-bold tracking-tight text-gray-900 dark:text-white"
20+
>
21+
{{hap.name}}
22+
</h5>
23+
</a>
24+
<p class="mb-3 font-normal text-gray-700 dark:text-gray-400">
25+
{{hap.description}}
26+
</p>
27+
28+
29+
<p> <i class="fa-solid fa-location-dot"></i> {{hap.venue}}
30+
31+
</p>
32+
<p> <i class="fa-solid fa-calendar"></i> {{hap.start_time}}</p>
33+
<a
34+
href="{{hap.get_absolute_url}}"
35+
class="inline-flex items-center px-3 py-2 text-sm font-medium text-center text-white bg-primary-700 rounded-lg hover:bg-primary-800 focus:ring-4 focus:outline-none focus:ring-primary-300 dark:bg-primary-600 dark:hover:bg-primary-700 dark:focus:ring-primary-800 mt-4"
36+
>
37+
Read more
38+
<svg
39+
class="rtl:rotate-180 w-3.5 h-3.5 ms-2"
40+
aria-hidden="true"
41+
xmlns="http://www.w3.org/2000/svg"
42+
fill="none"
43+
viewBox="0 0 14 10"
44+
>
45+
<path
46+
stroke="currentColor"
47+
stroke-linecap="round"
48+
stroke-linejoin="round"
49+
stroke-width="2"
50+
d="M1 5h12m0 0L9 1m4 4L9 9"
51+
/>
52+
</svg>
53+
</a>
54+
</div>
55+
</div>
56+
57+
58+
{% endfor %}
59+
60+
61+
62+
</div>
63+
{% endblock %}

src/haps/tests.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.test import TestCase
2+
3+
# Create your tests here.

src/haps/urls.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from django.urls import path
2+
3+
from . import views
4+
5+
app_name = "haps"
6+
7+
urlpatterns = [
8+
path("", views.haps_list, name="events"),
9+
path("e/<slug>", views.haps_item, name="event_item"),
10+
]

src/haps/views.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from django.shortcuts import render
2+
from django.http import HttpRequest, Http404
3+
from .models import Event
4+
import logging
5+
6+
log = logging.getLogger(__name__)
7+
8+
9+
def haps_list(request: HttpRequest):
10+
haps = Event.objects.all()
11+
context = {"haps": haps}
12+
return render(request, "haps/list.html", context=context)
13+
14+
15+
def haps_item(request: HttpRequest, slug: str):
16+
event = Event.objects.get(slug=slug)
17+
context = {"event": event}
18+
19+
return render(request, "haps/item.html", context=context)

0 commit comments

Comments
 (0)