Unverified Commit 1ac25e82 authored by Joost Rijneveld's avatar Joost Rijneveld
Browse files

Create document models

parent 946484ab
from django.contrib import admin
from documents.models import AssociationDocument
from documents.models import GenericDocument
from documents.models import GeneralMeeting, GeneralMeetingDocument
class GeneralMeetingDocInline(admin.StackedInline):
model = GeneralMeetingDocument
@admin.register(GeneralMeeting)
class GeneralMeetingAdmin(admin.ModelAdmin):
inlines = (GeneralMeetingDocInline, )
admin.site.register(GenericDocument)
admin.site.register(AssociationDocument)
from django.apps import AppConfig
class DocumentsConfig(AppConfig):
name = 'documents'
# -*- coding: utf-8 -*-
# Generated by Django 1.10b1 on 2016-07-06 20:37
from __future__ import unicode_literals
from django.db import migrations, models
import django.db.models.deletion
import documents.models
import utils.validators
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='AssociationDocument',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=200)),
('year', models.IntegerField()),
('file', models.FileField(upload_to=documents.models.associationdocs_path, validators=[utils.validators.validate_file_extension])),
('filetype', models.CharField(choices=[('policy_document', 'Policy document'), ('annual_report', 'Annual report'), ('financial_report', 'Financial report')], max_length=16)),
],
),
migrations.CreateModel(
name='GeneralMeeting',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('minutes', models.FileField(upload_to=documents.models.minutes_path, validators=[utils.validators.validate_file_extension])),
('datetime', models.DateTimeField()),
('location', models.CharField(max_length=200)),
],
),
migrations.CreateModel(
name='GeneralMeetingDocument',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('file', models.FileField(upload_to=documents.models.general_meetingdocs_path, validators=[utils.validators.validate_file_extension])),
('meeting', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='documents.GeneralMeeting')),
],
),
migrations.CreateModel(
name='GenericDocument',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=200)),
('file', models.FileField(upload_to='documents/general/', validators=[utils.validators.validate_file_extension])),
('members_only', models.BooleanField(default=False)),
],
),
]
from django.db import models
from django.utils.translation import ugettext_lazy as _
import os
from utils.validators import validate_file_extension
def associationdocs_path(instance, filename):
filename = instance.filetype + instance.year
return 'documents/associationdocs/{0}'.format(filename)
def minutes_path(instance, filename):
_, ext = os.path.splitext(filename)
return 'documents/{}/minutes{}'.format(instance.datetime.date(), ext)
def general_meetingdocs_path(instance, filename):
_, ext = os.path.splitext(filename)
return 'documents/{}/files/'.format(instance.datetime.date(), filename)
class AssociationDocument(models.Model):
name = models.CharField(max_length=200)
year = models.IntegerField()
file = models.FileField(
upload_to=associationdocs_path,
validators=[validate_file_extension],
)
FILETYPES = (
('policy_document', _("Policy document")),
('annual_report', _("Annual report")),
('financial_report', _("Financial report")),
)
filetype = models.CharField(
max_length=16,
choices=FILETYPES,
)
class GenericDocument(models.Model):
name = models.CharField(max_length=200)
file = models.FileField(
upload_to='documents/general/',
validators=[validate_file_extension],
)
members_only = models.BooleanField(default=False)
def __str__(self):
return self.name
class GeneralMeeting(models.Model):
minutes = models.FileField(
upload_to=minutes_path,
validators=[validate_file_extension],
)
datetime = models.DateTimeField()
location = models.CharField(max_length=200)
class GeneralMeetingDocument(models.Model):
meeting = models.ForeignKey(GeneralMeeting, on_delete=models.CASCADE)
file = models.FileField(
upload_to=general_meetingdocs_path,
validators=[validate_file_extension],
)
def __str__(self):
return self.file.name
# Create your views here.
......@@ -41,6 +41,7 @@ INSTALLED_APPS = [
# Our apps
'thaliawebsite', # include for admin settings
'members',
'documents',
]
MIDDLEWARE = [
......
from django.core.exceptions import ValidationError
import os
def validate_file_extension(file, exts=['.txt', '.pdf', '.jpg', '.png']):
_, ext = os.path.splitext(file.name)
if not ext.lower() in exts:
raise ValidationError("File extension not allowed.")
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment