diff --git a/main/settings.py b/main/settings.py index 8ba8d821..faa68540 100644 --- a/main/settings.py +++ b/main/settings.py @@ -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 = { diff --git a/scripts/seed.bat b/scripts/seed.bat index d9fa2eaa..85d9b0f6 100644 --- a/scripts/seed.bat +++ b/scripts/seed.bat @@ -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 diff --git a/scripts/seed.sh b/scripts/seed.sh index 3d726988..552832bb 100755 --- a/scripts/seed.sh +++ b/scripts/seed.sh @@ -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 diff --git a/showcase/admin.py b/showcase/admin.py index 38cee47d..c8839c0b 100644 --- a/showcase/admin.py +++ b/showcase/admin.py @@ -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) diff --git a/showcase/fixtures/showcase_site_settings.json b/showcase/fixtures/showcase_site_settings.json new file mode 100644 index 00000000..145e09c8 --- /dev/null +++ b/showcase/fixtures/showcase_site_settings.json @@ -0,0 +1,13 @@ +[ + { + "model": "showcase.showcasesitesettings", + "pk": 1, + "fields": { + "order_by_category": "-created", + "spotlight_number": 5, + "projects_per_page": 5, + "hackathons": [], + "featured_hackathons": [] + } + } +] diff --git a/showcase/lists.py b/showcase/lists.py new file mode 100644 index 00000000..9ec92a77 --- /dev/null +++ b/showcase/lists.py @@ -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'), +] diff --git a/showcase/migrations/0002_showcasesitesettings.py b/showcase/migrations/0002_showcasesitesettings.py new file mode 100644 index 00000000..af1d8403 --- /dev/null +++ b/showcase/migrations/0002_showcasesitesettings.py @@ -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', + }, + ), + ] diff --git a/showcase/models.py b/showcase/models.py index ac5d639f..8fe03e0c 100644 --- a/showcase/models.py +++ b/showcase/models.py @@ -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): @@ -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' diff --git a/showcase/views.py b/showcase/views.py index 687ea5e5..a1814375 100644 --- a/showcase/views.py +++ b/showcase/views.py @@ -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)