Commit a8ef0f03 authored by olavhdi's avatar olavhdi
Browse files

single table for a facility, now validates imagesize, wordcount, and max 5 images

parent 14ce43e3
......@@ -3,7 +3,6 @@
from django.contrib import admin
# Register your models here.
from .models import Facility, FacilityImage
from .models import Facility
admin.site.register(Facility)
admin.site.register(FacilityImage)
# Generated by Django 3.1 on 2021-02-25 11:56
# Generated by Django 3.1 on 2021-02-25 13:57
import django.core.validators
from django.db import migrations, models
import django.db.models.deletion
import facilities.models
import facilities.validators
class Migration(migrations.Migration):
......@@ -18,22 +18,19 @@ class Migration(migrations.Migration):
name='Facility',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=100)),
('name', models.CharField(max_length=50, unique=True)),
('date', models.DateTimeField()),
('description', models.TextField()),
('description', models.CharField(max_length=400)),
('latitude', models.DecimalField(decimal_places=5, max_digits=7, validators=[django.core.validators.MaxValueValidator(90), django.core.validators.MinValueValidator(-90)])),
('longitude', models.DecimalField(decimal_places=5, max_digits=8, validators=[django.core.validators.MaxValueValidator(180), django.core.validators.MinValueValidator(-180)])),
('image1', models.ImageField(upload_to=facilities.models.facility_directory_path, validators=[facilities.validators.image_size])),
('image2', models.ImageField(upload_to=facilities.models.facility_directory_path, validators=[facilities.validators.image_size])),
('image3', models.ImageField(upload_to=facilities.models.facility_directory_path, validators=[facilities.validators.image_size])),
('image4', models.ImageField(upload_to=facilities.models.facility_directory_path, validators=[facilities.validators.image_size])),
('image5', models.ImageField(upload_to=facilities.models.facility_directory_path, validators=[facilities.validators.image_size])),
],
options={
'ordering': ['-date'],
},
),
migrations.CreateModel(
name='FacilityImage',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('file', models.ImageField(upload_to=facilities.models.facility_directory_path)),
('facility', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='files', to='facilities.facility')),
],
),
]
# Generated by Django 3.1 on 2021-02-25 14:08
from django.db import migrations, models
import facilities.models
import facilities.validators
class Migration(migrations.Migration):
dependencies = [
('facilities', '0001_initial'),
]
operations = [
migrations.AlterField(
model_name='facility',
name='date',
field=models.DateField(),
),
migrations.AlterField(
model_name='facility',
name='description',
field=models.TextField(validators=[facilities.validators.max_words]),
),
migrations.AlterField(
model_name='facility',
name='image2',
field=models.ImageField(blank=True, upload_to=facilities.models.facility_directory_path, validators=[facilities.validators.image_size]),
),
migrations.AlterField(
model_name='facility',
name='image3',
field=models.ImageField(blank=True, upload_to=facilities.models.facility_directory_path, validators=[facilities.validators.image_size]),
),
migrations.AlterField(
model_name='facility',
name='image4',
field=models.ImageField(blank=True, upload_to=facilities.models.facility_directory_path, validators=[facilities.validators.image_size]),
),
migrations.AlterField(
model_name='facility',
name='image5',
field=models.ImageField(blank=True, upload_to=facilities.models.facility_directory_path, validators=[facilities.validators.image_size]),
),
]
......@@ -2,11 +2,23 @@
which contain descriptions, images and location.
"""
import os
from django.core.validators import MaxValueValidator, MinValueValidator
from django.db import models
def facility_directory_path(instance, filename):
"""Return path for which facility files should be uploaded on the web server
Args:
instance (FacilityImage): FacilityImage instance
filename (str): Name of the file
Returns:
str: Path where facility file is stored
"""
return f"facilities/{instance.name}/{filename}"
class Facility(models.Model):
"""Django model for a facility that admins can add.
......@@ -19,11 +31,17 @@ class Facility(models.Model):
description: Description of the facility
latitude: Latitude of the facility
longitude: Longitude of the facility
image1: Image of the facility
image2: Image of the facility
image3: Image of the facility
image4: Image of the facility
image5: Image of the facility
"""
from .validators import image_size, max_words
name = models.CharField(max_length=50, unique=True)
date = models.DateTimeField()
description = models.CharField(max_length=400)
date = models.DateField()
description = models.TextField(validators=[max_words])
latitude = models.DecimalField(
decimal_places=5, max_digits=7, validators=[MaxValueValidator(90), MinValueValidator(-90)])
......@@ -31,35 +49,14 @@ class Facility(models.Model):
longitude = models.DecimalField(
decimal_places=5, max_digits=8, validators=[MaxValueValidator(180), MinValueValidator(-180)])
image1 = models.ImageField(validators=[image_size], upload_to=facility_directory_path)
image2 = models.ImageField(validators=[image_size], upload_to=facility_directory_path, blank=True)
image3 = models.ImageField(validators=[image_size], upload_to=facility_directory_path, blank=True)
image4 = models.ImageField(validators=[image_size], upload_to=facility_directory_path, blank=True)
image5 = models.ImageField(validators=[image_size], upload_to=facility_directory_path, blank=True)
class Meta:
ordering = ["-date"]
def __str__(self):
return self.name
# Create your models here.
def facility_directory_path(instance, filename):
"""Return path for which facility files should be uploaded on the web server
Args:
instance (FacilityImage): FacilityImage instance
filename (str): Name of the file
Returns:
str: Path where facility file is stored
"""
return f"facilities/{instance.facility.id}/{filename}"
class FacilityImage(models.Model):
"""Django model for image associated with a facility.
Attributes:
facility: The facility for which this file has been uploaded
file: The actual file that's being uploaded
"""
facility = models.ForeignKey(Facility, on_delete=models.CASCADE, related_name="files", blank=False)
file = models.ImageField(upload_to=facility_directory_path)
return self.name
\ No newline at end of file
......@@ -7,7 +7,4 @@ urlpatterns = [
path("api/facilities/", views.FacilityList.as_view(), name="facility-list"),
path("api/facilities/<int:pk>/", views.FacilitySingle.as_view(), name="facility-detail"),
path("api/facilities/<str:name>/", views.FacilitySingle.as_view(), name="facility-detail"),
path(
"api/facilities/<int:fk>/", views.FacilityImageList.as_view(), name="facility-image-list"
),
]
from django.core.exceptions import ValidationError
def image_size(image):
if image.file.size > 3 * 1024 * 1024:
raise ValidationError('Image size needs to be less than 3MB')
# Restricts the amount of words to 500
def max_words(string):
if len(string.split(" ")) > 500:
raise ValidationError('Description can contain a maximum of 500 words')
......@@ -4,7 +4,7 @@ from rest_framework import generics, mixins
from rest_framework import permissions
from rest_framework.decorators import api_view
from rest_framework.reverse import reverse
from facilities.models import Facility, FacilityImage
from facilities.models import Facility
from rest_framework.response import Response
......@@ -14,7 +14,6 @@ def api_root(request, format=None):
{
"users": reverse("user-list", request=request, format=format),
"facilities": reverse("facility-list", request=request, format=format),
"facility-files": reverse("facility-file-list", request=request, format=format),
}
)
......@@ -63,21 +62,4 @@ class FacilitySingle(
permission_classes = [permissions.IsAuthenticated]
def get(self, request, *args, **kwargs):
return self.retrieve(request, *args, **kwargs)
class FacilityImageList(
mixins.ListModelMixin,
mixins.RetrieveModelMixin,
generics.GenericAPIView,
):
"""Class defining the web response for the creation of a FacilityImage, or a list of FacilityImages.
HTTP methods: GET
"""
queryset = FacilityImage.objects.all()
permission_classes = [permissions.IsAuthenticated]
def get(self, request, *args, **kwargs):
return self.list(request, *args, **kwargs)
return self.retrieve(request, *args, **kwargs)
\ No newline at end of file
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