Skip to content

Commit

Permalink
#A19. As Admin, I would like to change which hackathons are included …
Browse files Browse the repository at this point in the history
…in the showcase and be able to select a specific order (#182)
  • Loading branch information
stefdworschak authored May 14, 2021
1 parent 22ce717 commit 419fe03
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 12 deletions.
3 changes: 0 additions & 3 deletions main/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,6 @@
}
}

# Number of showcases displayed in the carousel
SHOWCASE_SPOTLIGHT_NUMBER = int(os.environ.get('SHOWCASE_SPOTLIGHT_NUMBER')
or 0)
SUPPORT_EMAIL = os.environ.get("SUPPORT_EMAIL")

LOGGING = {
Expand Down
1 change: 1 addition & 0 deletions scripts/seed.bat
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ python manage.py loaddata profiles
python manage.py loaddata emailaddresses
python manage.py loaddata hackathons
python manage.py loaddata showcase
python manage.py loaddata showcase_site_settings
1 change: 1 addition & 0 deletions scripts/seed.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ python3 manage.py loaddata profiles
python3 manage.py loaddata emailaddresses
python3 manage.py loaddata hackathons
python3 manage.py loaddata showcase
python3 manage.py loaddata showcase_site_settings
3 changes: 2 additions & 1 deletion showcase/admin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.contrib import admin

from .models import Showcase
from .models import Showcase, ShowcaseSiteSettings


admin.site.register(Showcase)
admin.site.register(ShowcaseSiteSettings)
13 changes: 13 additions & 0 deletions showcase/fixtures/showcase_site_settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[
{
"model": "showcase.showcasesitesettings",
"pk": 1,
"fields": {
"order_by_category": "-created",
"spotlight_number": 5,
"projects_per_page": 5,
"hackathons": [],
"featured_hackathons": []
}
}
]
9 changes: 9 additions & 0 deletions showcase/lists.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
ORDER_BY_CATEGORY_CHOICES = [
('?', 'Random Order'),
('-created', 'Showcase Created Date in Descending Order'),
('created', 'Showcase Created Date in Ascending Order'),
('-updated', 'Showcase Updated Date in Descending Order'),
('updated', 'Showcase Updated Date in Ascending Order'),
('-display_name', 'Alphabetical by Project Name in Descending Order'),
('display_name', 'Alphabetical by Project Name in Ascending Order'),
]
29 changes: 29 additions & 0 deletions showcase/migrations/0002_showcasesitesettings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Generated by Django 3.1.8 on 2021-05-12 14:32

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('hackathon', '0042_hackteam_communication_channel'),
('showcase', '0001_initial'),
]

operations = [
migrations.CreateModel(
name='ShowcaseSiteSettings',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('order_by_category', models.CharField(choices=[('?', 'Random Order'), ('-created', 'Showcase Created Date in Descending Order'), ('created', 'Showcase Created Date in Ascending Order'), ('-updated', 'Showcase Updated Date in Descending Order'), ('updated', 'Showcase Updated Date in Ascending Order'), ('-display_name', 'Alphabetical by Project Name in Descending Order'), ('display_name', 'Alphabetical by Project Name in Ascending Order')], default='random', max_length=255)),
('spotlight_number', models.IntegerField(default=5)),
('projects_per_page', models.IntegerField(default=5)),
('featured_hackathons', models.ManyToManyField(related_name='showcase_featured_hackathons', to='hackathon.Hackathon')),
('hackathons', models.ManyToManyField(related_name='showcase_hackathons', to='hackathon.Hackathon')),
],
options={
'verbose_name': 'Project Showcase Site Settings',
'verbose_name_plural': 'Project Showcase Site Settings',
},
),
]
40 changes: 39 additions & 1 deletion showcase/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from django.db import models

from .lists import ORDER_BY_CATEGORY_CHOICES
from accounts.models import CustomUser as User
from hackathon.models import HackProject
from hackathon.models import HackProject, Hackathon


class Showcase(models.Model):
Expand Down Expand Up @@ -47,3 +48,40 @@ def get_project(self):
return self.hack_project
except:
return None


class SingletonModel(models.Model):
""" Singleton model for Showcases """
class Meta:
abstract = True

def save(self, *args, **kwargs):
self.pk = 1
super(SingletonModel, self).save(*args, **kwargs)

def delete(self, *args, **kwargs):
pass

@classmethod
def load(cls):
obj, created = cls.objects.get_or_create(pk=1)
return obj


class ShowcaseSiteSettings(SingletonModel):
""" Model to set how the showcase should be constructed"""
hackathons = models.ManyToManyField(Hackathon,
related_name="showcase_hackathons")
featured_hackathons = models.ManyToManyField(
Hackathon, related_name="showcase_featured_hackathons")
order_by_category = models.CharField(default="random", max_length=255,
choices=ORDER_BY_CATEGORY_CHOICES)
spotlight_number = models.IntegerField(default=5)
projects_per_page = models.IntegerField(default=5)

def __str__(self):
return "Project Showcase Settings"

class Meta:
verbose_name = 'Project Showcase Site Settings'
verbose_name_plural = 'Project Showcase Site Settings'
31 changes: 24 additions & 7 deletions showcase/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,39 @@
from django.shortcuts import render, reverse, redirect, get_object_or_404

from .forms import ShowcaseForm
from .models import Showcase
from .models import Showcase, ShowcaseSiteSettings
from hackathon.models import HackTeam, HackProject
from images.helpers import image_to_base64str

SHOWCASE_SPOTLIGHT_NUMBER = settings.SHOWCASE_SPOTLIGHT_NUMBER


def view_showcases(request):
""" Shows the project showcase page """
showcase_settings = ShowcaseSiteSettings.objects.first()
if not showcase_settings:
return render(request, 'showcase.html', {
'top_results': None,
'all_showcases': None,
})

showcase_hackathons = showcase_settings.hackathons.all()
featured_hackathons = showcase_settings.featured_hackathons.all()
showcase_hackathons_teams = [team.id
for hackathon in showcase_hackathons
for team in hackathon.teams.all()]
showcase_featured_hackathons_teams = [
team.id for featured_hackathon in featured_hackathons
for team in featured_hackathon.teams.all()]

all_showcases = Showcase.objects.filter(
is_public=True).order_by('display_name')
hack_project__hackteam__in=showcase_hackathons_teams,
is_public=True
).order_by(showcase_settings.order_by_category)
top_results = Showcase.objects.filter(
is_public=True).order_by('?')[
:SHOWCASE_SPOTLIGHT_NUMBER]
hack_project__hackteam__in=showcase_featured_hackathons_teams,
is_public=True
).order_by('?')[:showcase_settings.spotlight_number]

paginator = Paginator(all_showcases, 5)
paginator = Paginator(all_showcases, showcase_settings.projects_per_page)
page = request.GET.get('page')
paginated_showcases = paginator.get_page(page)

Expand Down

0 comments on commit 419fe03

Please sign in to comment.