Skip to content
This repository has been archived by the owner on Feb 12, 2022. It is now read-only.

Commit

Permalink
set fixed name for the schedule image
Browse files Browse the repository at this point in the history
  • Loading branch information
ckleemann committed Aug 16, 2016
1 parent 5cef352 commit 2fb2d4e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
21 changes: 21 additions & 0 deletions website/migrations/0003_auto_20160816_1505.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10 on 2016-08-16 13:05
from __future__ import unicode_literals

from django.db import migrations, models
import website.models


class Migration(migrations.Migration):

dependencies = [
('website', '0002_schedule'),
]

operations = [
migrations.AlterField(
model_name='schedule',
name='image',
field=models.ImageField(storage=website.models.OverwriteStorage(), upload_to=website.models.Schedule.fixedname_upload_to, verbose_name='Stundenplan Bild'),
),
]
27 changes: 25 additions & 2 deletions website/models.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
from django.db import models
from django.core.exceptions import ValidationError
from django.core.files.storage import FileSystemStorage
from django.db import models
from django.db.models.signals import pre_delete
from django.dispatch import receiver
from django.template.defaultfilters import date as _date
from django.utils.translation import ugettext_lazy as _

import os

class Settings(models.Model):
"""Configuration for Website App."""
Expand Down Expand Up @@ -38,6 +42,12 @@ def instance():
except Settings.DoesNotExist:
return None

class OverwriteStorage(FileSystemStorage):
"""Delete the old file so that the name is available"""
def get_available_name(self, name, max_length):
self.delete(name)
return super().get_available_name(name, max_length)

class Schedule(models.Model):
"""A Schedule for a Degree during the Ophase"""
class Meta:
Expand All @@ -50,9 +60,22 @@ class Meta:
('DSS', _('Distributed Software Systems')),
)

def fixedname_upload_to(instance, filename):
"""returns the path and name where the image should upload to"""
path = 'website/schedule/'
name = instance.degree.lower()
x, file_extension = os.path.splitext(filename)
return '{}{}{}'.format(path, name, file_extension)

degree = models.CharField(max_length=3, choices=DEGREE_CHOICES, verbose_name=_('Abschluss'), unique=True)
image = models.ImageField(upload_to='website/schedule/', verbose_name=_('Stundenplan Bild'))
image = models.ImageField(verbose_name=_('Stundenplan Bild'),
upload_to=fixedname_upload_to, storage=OverwriteStorage())
stand = models.DateField(verbose_name=_('Stand des Stundenplans'))

def __str__(self):
return '{} {}'.format(self.get_degree_display(), _date(self.stand, 'SHORT_DATE_FORMAT'))

#Register an signal receiver so the image is deletet when the model is deleted
@receiver(pre_delete, sender=Schedule)
def schedule_delete(sender, instance, **kwargs):
instance.image.delete(False)

0 comments on commit 2fb2d4e

Please sign in to comment.