diff --git a/.gitignore b/.gitignore index 3f9e2ef..006567c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .pyc +*.swp media/ db.sqlite3 Ernestophone/settings.py diff --git a/Ernestophone/settings-example.py b/Ernestophone/settings-example.py index a403cec..481ec5d 100644 --- a/Ernestophone/settings-example.py +++ b/Ernestophone/settings-example.py @@ -13,10 +13,10 @@ import os BASE_DIR = os.path.dirname(os.path.dirname(__file__)) -MEDIA_ROOT = os.path.join(BASE_DIR, 'media') +EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend' + MEDIA_URL = '/media/' -STATIC_ROOT = os.path.join(BASE_DIR, 'static') STATIC_URL = '/static/' # Quick-start development settings - unsuitable for production diff --git a/calendrier/forms.py b/calendrier/forms.py index daed0b4..b69e21d 100644 --- a/calendrier/forms.py +++ b/calendrier/forms.py @@ -5,6 +5,7 @@ from gestion.models import ErnestoUser class ModifEventForm(forms.ModelForm): class Meta: model = Event + exclude = ['slug'] widgets = { 'description': forms.Textarea(attrs={"placeholder":"facultatif, balises html supportées"}), 'date': forms.TextInput(attrs={"placeholder": 'jj/mm/aaaa'}), @@ -20,6 +21,7 @@ class EventForm(forms.ModelForm): message = forms.CharField(max_length=2000, widget=forms.Textarea(attrs={"placeholder":"Remplir ici pour remplacer le mail automatique"}), required=False) class Meta: model = Event + exclude = ['slug'] widgets = { 'nomcourt': forms.TextInput(attrs={"placeholder": '9 caractères max'}), 'description': forms.Textarea(attrs={"placeholder":"facultatif, balises html supportées"}), diff --git a/partitions/models.py b/partitions/models.py index 1ee78c5..2f79474 100644 --- a/partitions/models.py +++ b/partitions/models.py @@ -31,7 +31,4 @@ class PartitionSet(models.Model): def __str__(self): return("%s - %s [%s]" % (self.nom, self.auteur, self.get_category_display())) - - def get_category(self): - return diff --git a/partitions/templatetags/__init__.py b/partitions/templatetags/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/partitions/templatetags/urls_extra.py b/partitions/templatetags/urls_extra.py deleted file mode 100644 index 0da412d..0000000 --- a/partitions/templatetags/urls_extra.py +++ /dev/null @@ -1,9 +0,0 @@ -from django import template - -register = template.Library() - -@register.filter -def cuturl(texte): - tex = texte.split('/') - n = len(tex) - return tex[n-1] diff --git a/partitions/urls.py b/partitions/urls.py index 092c8d3..bf5fe2a 100644 --- a/partitions/urls.py +++ b/partitions/urls.py @@ -6,8 +6,8 @@ urlpatterns = patterns('partitions.views', url(r'^(?P[^/]+)/(?P[^/]+)/upload/?$', 'upload'), url(r'^(?P[^/]+)/(?P[^/]+)/delete/?$', 'delete_morc'), url(r'^(?P[^/]+)/(?P[^/]+)/conf/?$', 'conf_delete_morc'), - url(r'^(?P[^/]+)/(?P[^/]+)/see/(?P[^/]+)/?$', 'see'), - url(r'^(?P[^/]+)/(?P[^/]+)/(?P[^/]+)/$', 'download'), + url(r'^(?P[^/]+)/(?P[^/]+)/see/(?P\d+)/?$', 'see'), + url(r'^(?P[^/]+)/(?P[^/]+)/(?P\d+)/$', 'download'), url(r'^(?P[^/]+)/(?P[^/]+)/(?P[^/]+)/delete/?$', 'delete'), url(r'^(?P[^/]+)/(?P[^/]+)/(?P[^/]+)/conf/?$', 'conf_delete'), url(r'^new/?$', 'ajouter_morceau'), diff --git a/partitions/views.py b/partitions/views.py index a1950b7..84162fe 100644 --- a/partitions/views.py +++ b/partitions/views.py @@ -3,6 +3,7 @@ from django.shortcuts import render, redirect, HttpResponse, render_to_response, from django.contrib.auth.decorators import login_required from django.utils.encoding import smart_str from django.forms.models import modelform_factory +from django.core.files import File import os @@ -46,21 +47,24 @@ def upload(request, nom, auteur): return render(request, 'partitions/upload.html', locals()) @login_required -def see(request, nom, auteur, file_name): - if ".pdf" in file_name: - abspath=open('/var/www_new/media/partitions/' + file_name, 'rb') - response=HttpResponse(content=abspath.read()) - response["Content-Type"]="application/pdf" - response["Content-Disposition"]="inline; filename= %s" % (nom +'-'+ auteur +'-' + smart_str(file_name)) - abspath.close() +def see(request, nom, auteur, partition_id): + partition = get_object_or_404(Partition, id=partition_id) + _, extension = os.path.splitext(partition.part.path) + if ".pdf" == extension: + with open(partition.part.path, 'rb') as f: + myfile = File(f) + response = HttpResponse(content=myfile.read()) + response["Content-Type"] = "application/pdf" + response["Content-Disposition"] = "inline; filename=%s-%s-%s" % ( + nom, auteur, smart_str(os.path.basename(myfile.name))) return response - elif ".mp3" in file_name: - abspath=open('/var/www_new/media/partitions/' + file_name, 'rb') - response = HttpResponse() - response.write(abspath.read()) - response["Content-Type"]="audio/mp3" - response["Content-Length"]= os.path.getsize('/var/www_new/media/partitions/' + file_name) - abspath.close() + elif ".mp3" == extension: + with open(partition.part.path, 'rb') as f: + myfile = File(f) + response = HttpResponse() + response.write(myfile.read()) + response["Content-Type"] = "audio/mp3" + response["Content-Length"] = myfile.size return response else: partitions = PartitionSet.objects.get(nom=nom, auteur=auteur) @@ -68,15 +72,15 @@ def see(request, nom, auteur, file_name): return render(request, 'partitions/listepart.html', locals()) @login_required -def download(request, nom, auteur, file_name): - abspath=open('/var/www_new/media/partitions/' + file_name, 'rb') - response = HttpResponse(content=abspath.read()) - typ = file_name.split(".") - n = len(typ) - typ = typ[n-1] - response['Content-Type'] = 'application/' + typ - response['Content-Disposition'] = 'attachment; filename= %s ' % (nom +'-'+ auteur + '-' + smart_str(file_name)) - abspath.close() +def download(request, nom, auteur, partition_id): + partition = get_object_or_404(Partition, id=partition_id) + with open(partition.part.path, 'rb') as f: + myfile = File(f) + response = HttpResponse(content=myfile.read()) + typ = os.path.splitext(myfile.name)[1][1:] + response['Content-Type'] = 'application/%s' % (typ, ) + response['Content-Disposition'] = 'attachment; filename=%s-%s-%s' % ( + nom, auteur, smart_str(os.path.basename(myfile.name))) return response @login_required diff --git a/templates/gestion/base.html b/templates/gestion/base.html index e950679..2c1baa2 100644 --- a/templates/gestion/base.html +++ b/templates/gestion/base.html @@ -1,5 +1,4 @@ {% load static %} -{% load urls_extra %} diff --git a/templates/partitions/listepart.html b/templates/partitions/listepart.html index fdf0775..f3c3f54 100644 --- a/templates/partitions/listepart.html +++ b/templates/partitions/listepart.html @@ -1,5 +1,4 @@ {% extends "base.html" %} -{% load urls_extra %} {% block titre %}{{ p }}{% endblock %} @@ -30,15 +29,15 @@