Skip to content

Commit

Permalink
Be able to work with a set of data in development
Browse files Browse the repository at this point in the history
When working on the site, it is helpful to have a large sample of data
to work from, so provide a full copy of my own tshirt wearings.
  • Loading branch information
norm committed Aug 2, 2021
1 parent 4008e17 commit a9d046c
Show file tree
Hide file tree
Showing 7 changed files with 3,145 additions and 1 deletion.
30 changes: 30 additions & 0 deletions README.markdown
Expand Up @@ -5,6 +5,36 @@ A place to collection my tshirt wearing history.



## Generated static sites

Running in development:

# bring up the stack
export HASWORN_ENV=dev
./start

# if this is a new checkout, or volumes have been removed
./manage collectstatic --noinput
./manage migrate
DJANGO_SUPERUSER_PASSWORD=norm ./manage createsuperuser \
--username norm --email norm@example.com --noinput

To test a generated hasworn site:

./manage import_csv sample/norm.csv
./manage generate_site norm
( cd baked/norm && python -m http.server 8080 )


When done developing:

# shut down temporarily
./compose down

# shut down and remove volumes to start afresh (or when development stops)
./compose down --volumes --remove-orphans


## Scripts

Scripts to interact with docker-compose, using the right configuration and
Expand Down
Empty file.
Empty file.
32 changes: 32 additions & 0 deletions hasworn/wearers/management/commands/import_csv.py
@@ -0,0 +1,32 @@
import csv
from django.core.management.base import BaseCommand

from hasworn.wearers.models import Wearer
from hasworn.clothing.models import Clothing, Wearing, Worn


class Command(BaseCommand):
help = 'Import wearings from a CSV file'

def add_arguments(self, parser):
parser.add_argument('csv_file')

def handle(self, *args, **options):
with open(options['csv_file']) as handle:
reader = csv.DictReader(handle)
for row in reader:
print(row)
wearer = Wearer.objects.get(username = row['wearer'])
clothing, _ = Clothing.objects.get_or_create(
name = row['name'],
slug = row['slug'],
created_by = wearer,
)
worn, _ = Worn.objects.get_or_create(
clothing = clothing,
wearer = wearer,
)
wearing = Wearing.objects.get_or_create(
worn = worn,
day = row['date'],
)
9 changes: 8 additions & 1 deletion manage
@@ -1,3 +1,10 @@
#!/usr/bin/env -S bash -euo pipefail

./compose run app python manage.py "$@"
declare -a env_vars

[ -n "${DJANGO_SUPERUSER_PASSWORD:-}" ] \
&& env_vars+=(-e DJANGO_SUPERUSER_PASSWORD=$DJANGO_SUPERUSER_PASSWORD)

./compose run \
"${env_vars[@]}" \
app python manage.py "$@"
3,049 changes: 3,049 additions & 0 deletions sample/norm.csv

Large diffs are not rendered by default.

26 changes: 26 additions & 0 deletions sample/scrape.py
@@ -0,0 +1,26 @@
import csv
import os
import requests
from bs4 import BeautifulSoup
from dateutil.parser import parse

with open('wearings.csv', 'w') as handle:
writer = csv.DictWriter(
handle,
fieldnames=['date', 'wearer', 'slug', 'name'],
)
writer.writeheader()

for year in range(2012, 2022):
req = requests.get('http://norm.hasworn.com/%s/' % year)
soup = BeautifulSoup(req.content, 'html.parser')
for wearing in soup.select('ul.month li'):
slug = os.path.basename(wearing.select('a')[0]['href'])
shirt, when = wearing.select('b')
date = parse(when.text)
writer.writerow({
'date': date.date(),
'wearer': 'norm',
'name': shirt.text,
'slug': slug,
})

0 comments on commit a9d046c

Please sign in to comment.