Skip to content

Commit

Permalink
Add a CSV of all wearings
Browse files Browse the repository at this point in the history
This allows users of the site to reuse their own data in some manner,
and to take backups of their data.
  • Loading branch information
norm committed Aug 2, 2021
1 parent 94d16d6 commit f80740e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
4 changes: 3 additions & 1 deletion hasworn/wearers/models.py
Expand Up @@ -4,7 +4,7 @@
from django.db import models
from django.utils.translation import ugettext_lazy as _

from .pages import WearerPage, WearerWornPage, WearerYear, WearerTypeIndex
from .pages import WearerPage, WearerWornPage, WearerYear, WearerTypeIndex, WearerCSV


class Wearer(AbstractUser):
Expand Down Expand Up @@ -94,6 +94,7 @@ def generate_wearer_site_wearing(self, wearing):
WearerTypeIndex(wearer=self).create()
WearerYear(wearer=self, year=wearing.day.year).create()
WearerPage(wearer=self).create()
WearerCSV(wearer=self).create()

def generate_wearer_site(self):
WearerPage(wearer=self).create()
Expand All @@ -102,3 +103,4 @@ def generate_wearer_site(self):
for year in self.years_active():
WearerYear(wearer=self, year=year).create()
WearerTypeIndex(wearer=self).create()
WearerCSV(wearer=self).create()
30 changes: 30 additions & 0 deletions hasworn/wearers/pages.py
@@ -1,5 +1,8 @@
import csv
from datetime import date
from django.conf import settings
from hasworn.pages import ModelPage, StaticPage
import os


class WearerPage(StaticPage):
Expand Down Expand Up @@ -45,3 +48,30 @@ def get_context(self, **kwargs):
context = super().get_context(**kwargs)
context['wearings'] = self.wearer.most_worn()
return context


class WearerCSV(StaticPage):
def get_filename(self):
return '%s/index.csv' % self.wearer.username

def create_page(self):
filename = self.get_filename()
full_filename = os.path.join(settings.GENERATED_SITES_DIR, filename)
with open(full_filename, 'w') as handle:
print('->', full_filename)
writer = csv.DictWriter(
handle,
fieldnames=['day','type','slug','name'],
)
writer.writeheader()

wearings = self.wearer.wearings.select_related(
'worn__clothing'
).order_by('day')
for wearing in wearings:
writer.writerow({
'day': wearing.day,
'type': wearing.clothing.type,
'slug': wearing.clothing.slug,
'name': wearing.clothing.name,
})
9 changes: 7 additions & 2 deletions nginx/prod/static.conf.template
@@ -1,3 +1,7 @@
types {
text/csv csv;
}

server {
listen 8080;
server_name ~^([a-z][a-z0-9-]+)\.hasworn\.com$ ;
Expand All @@ -14,7 +18,8 @@ server {
server_name ~^([a-z][a-z0-9-]+)\.hasworn\.com$ ;
root /baked/$1;

location / {
try_files $uri/index.html $uri.html =404;
location = /index.csv {}
location / {
try_files $uri.html $uri/index.html =404;
}
}

0 comments on commit f80740e

Please sign in to comment.