Compare commits

..

3 Commits

Author SHA1 Message Date
stupidcomputer 3fa153ee59 add a model for legislation classification, making topics
now you can make topics!
2024-06-28 16:37:21 -05:00
stupidcomputer f415713bdb make some changes to environment variable handling 2024-06-28 16:37:21 -05:00
stupidcomputer 1471dab714 make explorer more production ready
allow explorer to be deployed in an production environment without using
stupid hacks and DEBUG = true
2024-06-28 16:37:21 -05:00
20 changed files with 256 additions and 9 deletions

2
.env.prod Normal file
View File

@ -0,0 +1,2 @@
SECRET_KEY=834701
DJANGO_ALLOWED_HOSTS=localhost 127.0.0.1 [::1]

1
.gitignore vendored
View File

@ -5,3 +5,4 @@ __pycache__
db.sqlite3 db.sqlite3
media media
uploads/ uploads/
staticfiles/

5
Makefile Normal file
View File

@ -0,0 +1,5 @@
prod: # execute this target on the production server in the nix-shell
rm -r franklincce/staticfiles
cd franklincce; python3 manage.py collectstatic
sed -i "s/change_me/$(shell shuf -i1-1000000 -n1)/g" .env.prod
docker-compose -f docker-compose.prod.yml up -d --build

22
docker-compose.prod.yml Normal file
View File

@ -0,0 +1,22 @@
services:
web:
build:
context: ./franklincce
dockerfile: Dockerfile.prod
command: gunicorn franklincce.wsgi:application --bind 0.0.0.0:8000
volumes:
- static_volume:/home/app/web/staticfiles
expose:
- 8000
env_file:
- ./.env.prod
nginx:
build: ./nginx
volumes:
- static_volume:/home/app/web/staticfiles
ports:
- 1337:80
depends_on:
- web
volumes:
static_volume:

View File

@ -1,6 +1,6 @@
services: services:
web: web:
build: ./app build: ./franklincce
command: python manage.py runserver 0.0.0.0:8000 command: python manage.py runserver 0.0.0.0:8000
volumes: volumes:
- ./franklincce/:/usr/src/app/ - ./franklincce/:/usr/src/app/

View File

@ -0,0 +1,55 @@
FROM python:3.11.4-slim-buster as builder
# set work directory
WORKDIR /usr/src/app
# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
# install system dependencies
RUN apt-get update && \
apt-get install -y --no-install-recommends gcc
COPY ./requirements.txt .
RUN pip wheel --no-cache-dir --no-deps --wheel-dir /usr/src/app/wheels -r requirements.txt
# pull official base image
FROM python:3.11.4-slim-buster
# create directory for the app user
RUN mkdir -p /home/app
# create the app user
RUN addgroup --system app && adduser --system --group app
# create the appropriate directories
ENV HOME=/home/app
ENV APP_HOME=/home/app/web
RUN mkdir $APP_HOME
RUN mkdir $APP_HOME/staticfiles
WORKDIR $APP_HOME
# install dependencies
RUN apt-get update && apt-get install -y --no-install-recommends netcat
COPY --from=builder /usr/src/app/wheels /wheels
COPY --from=builder /usr/src/app/requirements.txt .
RUN pip install --upgrade pip
RUN pip install --no-cache /wheels/*
# copy entrypoint.prod.sh
COPY ./entrypoint.prod.sh .
RUN sed -i 's/\r$//g' $APP_HOME/entrypoint.prod.sh
RUN chmod +x $APP_HOME/entrypoint.prod.sh
# copy project
COPY . $APP_HOME
# chown all the files to the app user
RUN chown -R app:app $APP_HOME
# change to the app user
USER app
# run entrypoint.prod.sh
ENTRYPOINT ["/home/app/web/entrypoint.prod.sh"]

3
franklincce/entrypoint.prod.sh Executable file
View File

@ -0,0 +1,3 @@
#!/bin/sh
exec "$@"

View File

@ -1,6 +1,6 @@
from django.contrib import admin from django.contrib import admin
from .models import LegislativeText, LegislationBook from explorer import models
class LegislativeTextAdmin(admin.ModelAdmin): class LegislativeTextAdmin(admin.ModelAdmin):
list_display = ('__str__', 'legislation_title', 'school') list_display = ('__str__', 'legislation_title', 'school')
@ -8,5 +8,11 @@ class LegislativeTextAdmin(admin.ModelAdmin):
class LegislationBookAdmin(admin.ModelAdmin): class LegislationBookAdmin(admin.ModelAdmin):
exclude = ("has_performed_export",) exclude = ("has_performed_export",)
admin.site.register(LegislativeText, LegislativeTextAdmin) to_register = [
admin.site.register(LegislationBook, LegislationBookAdmin) [models.LegislativeText, LegislativeTextAdmin],
[models.LegislationBook, LegislationBookAdmin],
[models.LegislationClassification]
]
for i in to_register:
admin.site.register(*i)
print(i)

View File

@ -0,0 +1,21 @@
# Generated by Django 4.2.12 on 2024-06-28 20:53
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('explorer', '0003_legislationbook_has_performed_export_and_more'),
]
operations = [
migrations.CreateModel(
name='LegislationClassification',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(help_text='Name of this classification.', max_length=256)),
('text_to_match', models.CharField(help_text='a comma seperated list of keywords to include in the classification. spaces and dashes are discluded.', max_length=256)),
],
),
]

View File

@ -0,0 +1,19 @@
# Generated by Django 4.2.12 on 2024-06-28 21:07
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('explorer', '0004_legislationclassification'),
]
operations = [
migrations.AddField(
model_name='legislationclassification',
name='obvious_change',
field=models.CharField(default='test', help_text='Name of this classification.', max_length=256),
preserve_default=False,
),
]

View File

@ -0,0 +1,17 @@
# Generated by Django 4.2.12 on 2024-06-28 21:08
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('explorer', '0005_legislationclassification_obvious_change'),
]
operations = [
migrations.RemoveField(
model_name='legislationclassification',
name='obvious_change',
),
]

View File

@ -31,8 +31,6 @@ class LegislationBook(models.Model):
has_performed_export = models.BooleanField(default=False) has_performed_export = models.BooleanField(default=False)
def save(self, **kwargs): def save(self, **kwargs):
super().save(**kwargs)
if not self.has_performed_export: if not self.has_performed_export:
self.has_performed_export = True self.has_performed_export = True
super().save(**kwargs) super().save(**kwargs)
@ -136,3 +134,13 @@ class LegislativeText(models.Model):
if self.assembly in ["RGA", "BGA", "WGA", "GEN"]: if self.assembly in ["RGA", "BGA", "WGA", "GEN"]:
return True return True
return False return False
class LegislationClassification(models.Model):
name = models.CharField(max_length=256, help_text="Name of this classification.")
text_to_match = models.CharField(
max_length=256,
help_text="a comma seperated list of keywords to include in the classification. spaces and dashes are discluded."
)
def __str__(self):
return "{}".format(self.name)

View File

@ -0,0 +1,15 @@
{% extends "explorer/base.html" %}
{% block content %}
<link rel="stylesheet" type="text/css" href="/static/tn.css" />
<div class="boxed">
<h1>All topics</h1>
<ul>
{% for topic in classifications %}
<li><a href="/explorer/topics/{{ topic.id }}">{{ topic.name }}</a></li>
{% endfor %}
</ul>
</div>
{% endblock content %}

View File

@ -0,0 +1,15 @@
{% extends "explorer/base.html" %}
{% block content %}
<link rel="stylesheet" type="text/css" href="/static/tn.css" />
<div class="boxed">
<h1>{{ result_name }}</h1>
<ul>
{% for text in legislation %}
<li><a href="/explorer/legislation/{{ text.id }}">{{ text.legislation_title }}</a></li>
{% endfor %}
</ul>
</div>
{% endblock content %}

View File

@ -8,4 +8,6 @@ urlpatterns = [
path("stats/", views.stats, name="stats"), path("stats/", views.stats, name="stats"),
path("legislation/<int:legislation_id>/", views.view_legislation, name="viewleg"), path("legislation/<int:legislation_id>/", views.view_legislation, name="viewleg"),
path("conference/<int:conference_id>/", views.view_conference, name="viewconf"), path("conference/<int:conference_id>/", views.view_conference, name="viewconf"),
path("topics/<int:classification_id>/", views.get_all_classified_by_id, name="classificationview"),
path("topics/", views.get_all_classifications, name="classificationview"),
] ]

View File

@ -1,7 +1,7 @@
from django.shortcuts import get_object_or_404, render from django.shortcuts import get_object_or_404, render
from django.http import HttpResponse from django.http import HttpResponse
from .models import LegislativeText, LegislationBook from .models import LegislativeText, LegislationBook, LegislationClassification
from random import sample from random import sample
@ -53,3 +53,30 @@ def stats(request):
"white_ga": len(LegislativeText.objects.filter(assembly="WGA")), "white_ga": len(LegislativeText.objects.filter(assembly="WGA")),
} }
return render(request, "explorer/stats.html", context) return render(request, "explorer/stats.html", context)
def get_all_classifications(request):
classifications = LegislationClassification.objects.all()
return render(request, "explorer/classifications.html", {
"classifications": classifications,
})
def get_all_classified_by_id(request, classification_id):
classification = get_object_or_404(LegislationClassification, pk=classification_id)
# this is very expensive; make a way for this to be cached please?
all_texts = LegislativeText.objects.all()
all_terms = classification.text_to_match.split(',')
all_terms = [i.lower() for i in all_terms]
matches = []
for text in all_texts:
for term in all_terms:
if term in text.text.lower():
matches.append(text)
break
return render(request, "explorer/results.html", {
"legislation": matches,
"result_name": "All legislation in topic {}".format(classification.name)
})

View File

@ -23,7 +23,10 @@ DEBUG = bool(os.environ.get("DEBUG", default=0))
# 'DJANGO_ALLOWED_HOSTS' should be a single string of hosts with a space between each. # 'DJANGO_ALLOWED_HOSTS' should be a single string of hosts with a space between each.
# For example: 'DJANGO_ALLOWED_HOSTS=localhost 127.0.0.1 [::1]' # For example: 'DJANGO_ALLOWED_HOSTS=localhost 127.0.0.1 [::1]'
try:
ALLOWED_HOSTS = os.environ.get("DJANGO_ALLOWED_HOSTS").split(" ") ALLOWED_HOSTS = os.environ.get("DJANGO_ALLOWED_HOSTS").split(" ")
except:
ALLOWED_HOSTS = ["127.0.0.1", "localhost"]
INSTALLED_APPS = [ INSTALLED_APPS = [
'explorer.apps.ExplorerConfig', 'explorer.apps.ExplorerConfig',
@ -118,4 +121,8 @@ STATIC_URL = '/static/'
# Default primary key field type # Default primary key field type
# https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field # https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field
STATIC_ROOT = BASE_DIR / "staticfiles"
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
CSRF_TRUSTED_ORIGINS = ["http://localhost:1337"]

4
nginx/Dockerfile Normal file
View File

@ -0,0 +1,4 @@
FROM nginx:1.25
RUN rm /etc/nginx/conf.d/default.conf
COPY nginx.conf /etc/nginx/conf.d

18
nginx/nginx.conf Normal file
View File

@ -0,0 +1,18 @@
upstream franklincce {
server web:8000;
}
server {
listen 80;
location / {
proxy_pass http://franklincce;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
}
location /static/ {
alias /home/app/web/staticfiles/;
}
}

View File

@ -1,4 +1,4 @@
{ pkgs ? import <nixpkgs> {} }: { pkgs ? import <nixpkgs> {} }:
pkgs.mkShell { pkgs.mkShell {
nativeBuildInputs = with pkgs.python311Packages; [ django pymupdf ] ++ [pkgs.docker-compose] ; nativeBuildInputs = with pkgs.python311Packages; [ django pymupdf ] ++ [pkgs.docker-compose pkgs.gnumake] ;
} }