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

2021 Release #2

Open
wants to merge 17 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pre-commit = "*"

[packages]
uwsgi = "*"
django = "==2.2.10"
django = "==3.2.9"
cssselect = "==1.0.3"
dateutils = "==0.6.6"
markdown = "==3.0.1"
Expand Down
501 changes: 287 additions & 214 deletions Pipfile.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ First clone the repository from BitBucket and switch to the new directory::

Next, create a virtual environment and install all of the requirements::

(iticket)$ pip shell
(iticket)$ pipenv install --dev
(gingerhouse)$ pip shell
(gingerhouse)$ pipenv install --dev

Now, create a local settings file and set your DJANGO_SETTINGS_MODULE to use it:::

Expand Down
9 changes: 8 additions & 1 deletion gingerhouse/houses/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,22 @@


from gingerhouse.houses.models import Category, GingerHouse, Vote
from gingerhouse.photos.models import Photo


class CategoryAdmin(admin.ModelAdmin):
list_display = ("name",)


class PhotoAdminInline(admin.TabularInline):
model = Photo
extra = 3


class GingerHouseAdmin(admin.ModelAdmin):
list_display = ("name", "vote_count")

inlines = [PhotoAdminInline]

def vote_count(self, obj):
return obj.vote_set.all().count()

Expand Down
25 changes: 25 additions & 0 deletions gingerhouse/houses/migrations/0003_remove_image_field.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Generated by Django 3.2.9 on 2021-12-01 23:15

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('houses', '0002_auto_20201205_1227'),
]

operations = [
migrations.AlterModelOptions(
name='category',
options={'verbose_name_plural': 'Categories'},
),
migrations.AlterModelOptions(
name='gingerhouse',
options={'ordering': ['-category__name']},
),
migrations.RemoveField(
model_name='gingerhouse',
name='image',
),
]
20 changes: 8 additions & 12 deletions gingerhouse/houses/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
class Category(models.Model):
"""Categories are used to limit voting and present entries in groupings"""
name = models.CharField(max_length=255)

class Meta:
verbose_name_plural = "Categories"

Expand All @@ -21,21 +21,18 @@ class GingerHouse(models.Model):
category = models.ForeignKey('Category', on_delete=models.CASCADE)
display_address = models.TextField(blank=True)
model_address = models.TextField(blank=True)
image = models.ImageField(
verbose_name="Ginger house image",
upload_to='uploads/',
max_length=255,
blank=True,
null=True,
help_text="Ginger house image. Horizontal format, cropped to 812x680 px.",
)


class Meta:
ordering = ["-category__name", ]

def __str__(self):
return self.name


@property
def primary_image(self):
image = self.photo_set.filter(is_primary=True).first()
return image

def get_absolute_url(self):
return reverse("houses:detail", kwargs={"pk": self.id})

Expand All @@ -46,4 +43,3 @@ class Vote(models.Model):

def __str__(self):
return "{} - {} - {}".format(self.email, self.ginger_house.category.name, self.ginger_house.name)

14 changes: 12 additions & 2 deletions gingerhouse/houses/views.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
from django.db.models import OuterRef, Subquery, ImageField
from django.shortcuts import redirect, reverse
from django.views.generic import ListView, DetailView, FormView

from gingerhouse.houses.forms import VoteForm
from gingerhouse.houses.models import GingerHouse, Vote
from gingerhouse.photos.models import Photo


class HousesListView(ListView):
model = GingerHouse
template_name = 'houses/index.html'

def get_queryset(self):
qs = super().get_queryset()
qs = qs.annotate(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️

image=Subquery(Photo.objects.filter(house=OuterRef("id"), is_primary=True)[:1].values("image"),
output_field=ImageField())
)
return qs


class HouseDetailView(DetailView):
model = GingerHouse
template_name = 'houses/detail.html'
context_object_name = "house"

def get_context_data(self, **kwargs):
ctx = super(HouseDetailView, self).get_context_data(**kwargs)
ctx["form"] = VoteForm(ginger_house=ctx["object"])
Expand All @@ -24,7 +34,7 @@ def get_context_data(self, **kwargs):
class VoteView(FormView):
template_name = 'houses/detail.html'
form_class = VoteForm

def get_ginger_house(self):
ginger_house_id = self.kwargs.get("ginger_house_id")
ginger_house = GingerHouse.objects.get(pk=ginger_house_id)
Expand Down
Empty file added gingerhouse/photos/__init__.py
Empty file.
26 changes: 26 additions & 0 deletions gingerhouse/photos/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Generated by Django 3.2.9 on 2021-12-01 23:16

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

initial = True

dependencies = [
('houses', '0003_remove_image_field'),
]

operations = [
migrations.CreateModel(
name='Image',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('image', models.ImageField(blank=True, help_text='Ginger house image. Horizontal format, cropped to 812x680 px.', max_length=255, null=True, upload_to='uploads/', verbose_name='Ginger house image')),
('is_primary', models.BooleanField(default=False)),
('description', models.TextField(blank=True)),
('house', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='houses.gingerhouse')),
],
),
]
18 changes: 18 additions & 0 deletions gingerhouse/photos/migrations/0002_rename_image_photo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 3.2.9 on 2021-12-01 23:46

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('houses', '0003_remove_image_field'),
('photos', '0001_initial'),
]

operations = [
migrations.RenameModel(
old_name='Image',
new_name='Photo',
),
]
Empty file.
21 changes: 21 additions & 0 deletions gingerhouse/photos/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from django.db import models


class Photo(models.Model):
house = models.ForeignKey("houses.GingerHouse", null=True, blank=False, on_delete=models.SET_NULL)
image = models.ImageField(
verbose_name="Ginger house image",
upload_to='uploads/',
max_length=255,
blank=True,
null=True,
help_text="Ginger house image. Horizontal format, cropped to 812x680 px.",
)
is_primary = models.BooleanField(default=False)
description = models.TextField(blank=True)

class Meta:
ordering = ("house__id", "is_primary")

def __str__(self):
return self.description
6 changes: 4 additions & 2 deletions gingerhouse/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
)

# Application definition

INSTALLED_APPS = [
'django.contrib.auth',
'django.contrib.contenttypes',
Expand All @@ -33,7 +32,8 @@
'django.contrib.humanize',
'django.contrib.sitemaps',
# local apps
'gingerhouse.houses'
'gingerhouse.houses',
'gingerhouse.photos',
]

MIDDLEWARE = [
Expand Down Expand Up @@ -205,3 +205,5 @@
SECURE_BROWSER_XSS_FILTER = True
CSRF_COOKIE_HTTPONLY = True
X_FRAME_OPTIONS = 'DENY'

DEFAULT_AUTO_FIELD = 'django.db.models.AutoField'
2 changes: 1 addition & 1 deletion gingerhouse/templates/base.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% load static from staticfiles %}
{% load static %}
<!DOCTYPE html>
<html lang="en" class="no-js">
<head>
Expand Down
4 changes: 2 additions & 2 deletions gingerhouse/templates/houses/detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ <h3 class="model_address">{{ house.model_address }}</h3>
</div>
<div class="card-body">
<img class="card-img-top image" alt="{{ house.name }} image"
src="{% if house.image %}{{ house.image.url }}{% else %}/static/img/placeholder-house.jpg{% endif %}"
src="{% if house.primary_image %}{{ house.primary_image.image.url }}{% else %}/static/img/placeholder-house.jpg{% endif %}"
/>
</div>
<div class="card-footer">
Expand Down Expand Up @@ -79,4 +79,4 @@ <h3 class="model_address">{{ house.model_address }}</h3>
</div>

{% include "includes/email-required-text.html" %}
{% endblock %}
{% endblock %}
2 changes: 1 addition & 1 deletion gingerhouse/templates/houses/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ <h3 class="model_address">{{ house.model_address }}</h3>
</div>
<div class="card-body">
<a href="{{ house.get_absolute_url }}"><img class="card-img-top image" alt="{{ house.name }} image"
src="{% if house.image %}{{ house.image.url }}{% else %}/static/img/placeholder-house.jpg{% endif %}"
src="{% if house.image %}/media/{{ house.image }}{% else %}/static/img/placeholder-house.jpg{% endif %}"
/></a>

<div class="location text-center">
Expand Down