Skip to content
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

Add admin skyward expedition #128

Merged
merged 4 commits into from
Feb 24, 2024
Merged
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
8 changes: 8 additions & 0 deletions corpus/skyward_expedition/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,11 @@ class AnnouncementForm(CorpusModelForm):
class Meta:
model = Announcement
fields = ["content", "url_link", "url_link_text", "announcement_mailing"]

def clean(self):
data = self.cleaned_data
if data.get("url_link") and not data.get("url_link_text"):
raise forms.ValidationError(
"Both URL Link and corresponding text are required."
)
return data
20 changes: 20 additions & 0 deletions corpus/skyward_expedition/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from accounts.models import User
from django.db import models

from corpus.utils import send_email


# Create your models here.

Expand Down Expand Up @@ -50,3 +52,21 @@ class Announcement(models.Model):
content = models.TextField(blank=False, null=False)
url_link = models.URLField(blank=True, null=True)
url_link_text = models.CharField(max_length=200, blank=True, null=True)

def send_email(self, mail_option):
email_ids = None

if mail_option == 2:
email_ids = list(
Team.objects.values_list("team_leader__user__email", flat=True)
)
elif mail_option == 3:
email_ids = list(SEUser.objects.values_list("user__email", flat=True))

if email_ids is not None:
send_email(
"Announcement | Skyward Expedition",
"emails/skyward_expedition/announcement.html",
{"announcement": self},
bcc=email_ids,
)
34 changes: 34 additions & 0 deletions corpus/skyward_expedition/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,38 @@
views.delete_invite,
name="skyward_expedition_delete_invite",
),
path("admin/", views.admin, name="skyward_expedition_admin"),
path(
"admin/members/",
views.member_dashboard,
name="skyward_expedition_member_dashboard",
),
path(
"admin/teams/", views.teams_dashboard, name="skyward_expedition_teams_dashboard"
),
path(
"admin/teams/<int:team_id>",
views.team_details,
name="skyward_expedition_team_details",
),
path(
"admin/announcements",
views.announcements_dashboard,
name="skyward_expedition_announcements_dashboard",
),
path(
"admin/announcements/new/",
views.new_announcement,
name="skyward_expedition_new_announcement",
),
path(
"admin/announcements/<int:announcement_id>/edit/",
views.edit_announcement,
name="skyward_expedition_edit_announcement",
),
path(
"admin/announcements/<int:announcement_id>/delete/",
views.delete_announcement,
name="skyward_expedition_delete_announcement",
),
]
113 changes: 112 additions & 1 deletion corpus/skyward_expedition/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@
from django.contrib.auth.decorators import login_required
from django.shortcuts import redirect
from django.shortcuts import render
from skyward_expedition.forms import AnnouncementForm
from skyward_expedition.forms import InviteForm
from skyward_expedition.forms import SEForm
from skyward_expedition.forms import TeamCreationForm
from skyward_expedition.models import Announcement
from skyward_expedition.models import Invite
from skyward_expedition.models import SEUser
from skyward_expedition.models import Team

from corpus.decorators import ensure_group_membership
from corpus.decorators import module_enabled


Expand All @@ -26,7 +29,7 @@ def home(request):
if se_user is not None:
args["dashboard"] = True

if request.user.groups.filter(name__in="se_admin").exists():
if request.user.groups.filter(name="skyward_expedition_admin").exists():
args["admin"] = True

return render(request, "skyward_expedition/home.html", args)
Expand Down Expand Up @@ -249,3 +252,111 @@ def delete_invite(request, pk):

messages.success(request, "Invite deleted!")
return redirect("skyward_expedition_dashboard")


@login_required
@ensure_group_membership(group_names=["skyward_expedition_admin"])
def admin(request):
return render(request, "skyward_expedition/admin/index.html")


@login_required
@ensure_group_membership(group_names=["skyward_expedition_admin"])
def member_dashboard(request):
members = SEUser.objects.all()

members_count = members.count()
nitk_count = members.filter(nitk_participant=True).count()
ieee_count = members.filter(ieee_member=True).count()

args = {
"members": members,
"members_count": members_count,
"nitk_count": nitk_count,
"ieee_count": ieee_count,
}

return render(request, "skyward_expedition/admin/member_dashboard.html", args)


@login_required
@ensure_group_membership(group_names=["skyward_expedition_admin"])
def teams_dashboard(request):
teams = Team.objects.all()

teams_count = teams.count()

args = {"teams": teams, "teams_count": teams_count}

return render(request, "skyward_expedition/admin/teams_dashboard.html", args)


@login_required
@ensure_group_membership(group_names=["skyward_expedition_admin"])
def team_details(request, team_id):
team = Team.objects.get(id=team_id)
members = SEUser.objects.filter(team=team)

args = {"team": team, "members": members}

return render(request, "skyward_expedition/admin/team_details.html", args)


@login_required
@ensure_group_membership(group_names=["skyward_expedition_admin"])
def announcements_dashboard(request):
announcements = Announcement.objects.all().order_by("-pk")

args = {"announcements": announcements}

return render(
request, "skyward_expedition/admin/announcements_dashboard.html", args
)


@login_required
@ensure_group_membership(group_names=["skyward_expedition_admin"])
def new_announcement(request):
form = AnnouncementForm()

if request.method == "POST":
form = AnnouncementForm(request.POST)
if form.is_valid():
announcement = form.save()
mail_option = int(form.cleaned_data.get("announcement_mail", "1"))
announcement.send_email(mail_option)
messages.success(request, "Announcement added successfully!")
return redirect("skyward_expedition_announcements_dashboard")

args = {"form": form}

return render(request, "skyward_expedition/admin/new_announcement.html", args)


@login_required
@ensure_group_membership(group_names=["skyward_expedition_admin"])
def edit_announcement(request, announcement_id):
announcement = Announcement.objects.get(pk=announcement_id)
form = AnnouncementForm(instance=announcement)

if request.method == "POST":
form = AnnouncementForm(request.POST, instance=announcement)
if form.is_valid():
form.save()

messages.success(request, "Announcement updated!")
return redirect("skyward_expedition_announcements_dashboard")

args = {"announcement": announcement, "form": form}

return render(request, "skyward_expedition/admin/edit_announcement.html", args)


@login_required
@ensure_group_membership(group_names=["skyward_expedition_admin"])
def delete_announcement(request, announcement_id):
announcement = Announcement.objects.get(pk=announcement_id)
announcement.delete()

messages.success(request, "Announcement deleted!")
return redirect("skyward_expedition_announcements_dashboard")
3 changes: 2 additions & 1 deletion corpus/tailwind.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
module.exports = {
content: [
"./templates/*.html",
"./templates/**/*.html"
"./templates/**/*.html",
"./templates/**/**/*.html"
],
theme: {
extend: {},
Expand Down
15 changes: 15 additions & 0 deletions corpus/templates/emails/skyward_expedition/announcement.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{% extends 'emails/base.html' %}

{% block title %}
Announcement | Skyward Expedition
{% endblock %}

{% block content %}
<h1>Announcement - Skyward Expedition</h1>
<p>{{ announcement.content | linebreaks }}</p>
{% if announcement.url_link %}
<p>
<a href="{{ announcement.url_link }}">{{ announcement.url_link_text }}</a>
</p>
{% endif %}
{% endblock %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{% extends 'skyward_expedition/base.html' %}

{% block title %}
Announcements Dashboard
{{ block.super }}
{% endblock %}

{% block content %}
<div class="flex flex-col mx-20">
<div class="flex flex-row justify-center items-center my-20">
<h1 class="text-4xl font-bold text-center">Announcements Dashboard</h1>
</div>
<div class="flex flex-row mb-20">
<a href="{% url 'skyward_expedition_admin' %}" class="btn btn-accent m-5">Go to Admin Dashboard</a>
<a href="{% url 'skyward_expedition_new_announcement' %}" class="btn btn-accent m-5">Create Announcement</a>
</div>
<div class="flex flex-col my-10 overflow-x-auto">
<table class="table">
<thead>
<tr>
<td>Content</td>
<td>URL</td>
<td>Actions</td>
</tr>
</thead>
<tbody>
{% for announcement in announcements %}
<tr>
<td>{{ announcement.content | linebreaks }}</td>
<td>
{% if announcement.url_link %}
<a href="{{ announcement.url_link }}" class="btn rounded-full btn-primary">
{{ announcement.url_link_text }}
</a>
{% else %}
-
{% endif %}
</td>
<td>
<a href="{% url 'skyward_expedition_edit_announcement' announcement_id=announcement.id %}" class="btn btn-info mx-1">
Edit
</a>
<a href="{% url 'skyward_expedition_delete_announcement' announcement_id=announcement.id %}" class="btn btn-error mx-1">Delete</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
{% endblock %}
86 changes: 86 additions & 0 deletions corpus/templates/skyward_expedition/admin/edit_announcement.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
{% extends 'skyward_expedition/base.html' %}

{% block title %}
Edit Announcement
{{ block.super }}
{% endblock %}

{% block content %}
<div class="flex flex-col mx-20">
<div class="flex flex-row justify-center items-center my-20">
<h1 class="text-4xl font-bold text-center">Edit Announcement</h1>
</div>
<div class="flex flex-row mb-20">
<a href="{% url 'skyward_expedition_announcements_dashboard' %}" class="btn btn-accent">
Go to Announcements Dashboard
</a>
</div>
<div class="flex flex-col my-5">
<div class="alert alert-warning">Editing an announcement will not send an email.</div>
</div>
<div class="flex flex-col mb-10 overflow-x-auto">
<form method="post"
action="{% url 'skyward_expedition_edit_announcement' announcement_id=announcement.id %}">
{% csrf_token %}

{% if form.errors %}
<div class="w-full my-2">
<div class="alert alert-error">
<span>{{ form.non_field_errors }}</span>
</div>
</div>
{% endif %}

<div class="w-full my-2">
<label for="{{ form.content.id_for_label }}">Content</label>
{{ form.content }}
{% if form.content.errors %}
<div class="my-1">
<div role="alert" class="alert alert-error">
{{ form.content.errors }}
</div>
</div>
{% endif %}
</div>

<div class="w-full my-2">
<label for="{{ form.url_link.id_for_label }}">URL Link</label>
{{ form.url_link }}
{% if form.url_link.errors %}
<div class="my-1">
<div role="alert" class="alert alert-error">
{{ form.url_link.errors }}
</div>
</div>
{% endif %}
</div>

<div class="w-full my-2">
<label for="{{ form.url_link_text.id_for_label }}">URL Link Text</label>
{{ form.url_link_text }}
{% if form.url_link_text.errors %}
<div class="my-1">
<div role="alert" class="alert alert-error">
{{ form.url_link_text.errors }}
</div>
</div>
{% endif %}
</div>

<div class="w-full my-2">
<label for="{{ form.announcement_mailing.id_for_label }}">Email options</label>
{{ form.announcement_mailing }}
{% if form.announcement_mailing.errors %}
<div class="my-1">
<div role="alert" class="alert alert-error">
{{ form.announcement_mailing.errors }}
</div>
</div>
{% endif %}
</div>

<button type="submit" class="btn btn-primary text-white btn-block">Update Announcement</button>
</form>
</div>
</div>
{% endblock %}
Loading
Loading