first image model and upload form.
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/metadatacomposer/forms.py Tue May 21 14:54:41 2013 +0200
@@ -0,0 +1,9 @@
+# forms.py
+from django import forms
+from metadatacomposer.models import Image
+
+class ImageUploadModelForm(forms.ModelForm):
+
+ class Meta:
+ model = Image
+ fields = ['image_file', 'title', 'description']
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/metadatacomposer/migrations/0001_initial.py Tue May 21 14:54:41 2013 +0200
@@ -0,0 +1,44 @@
+# -*- coding: utf-8 -*-
+import datetime
+from south.db import db
+from south.v2 import SchemaMigration
+from django.db import models
+
+
+class Migration(SchemaMigration):
+
+ def forwards(self, orm):
+ # Adding model 'Image'
+ db.create_table('metadatacomposer_image', (
+ ('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),
+ ('image_file', self.gf('django.db.models.fields.files.ImageField')(max_length=2048)),
+ ('width', self.gf('django.db.models.fields.IntegerField')()),
+ ('height', self.gf('django.db.models.fields.IntegerField')()),
+ ('title', self.gf('django.db.models.fields.CharField')(max_length=1024, null=True, blank=True)),
+ ('description', self.gf('django.db.models.fields.TextField')(null=True, blank=True)),
+ ('creation_date', self.gf('django.db.models.fields.DateTimeField')(auto_now_add=True, blank=True)),
+ ('modification_date', self.gf('django.db.models.fields.DateTimeField')(auto_now=True, blank=True)),
+ ))
+ db.send_create_signal('metadatacomposer', ['Image'])
+
+
+ def backwards(self, orm):
+ # Deleting model 'Image'
+ db.delete_table('metadatacomposer_image')
+
+
+ models = {
+ 'metadatacomposer.image': {
+ 'Meta': {'object_name': 'Image'},
+ 'creation_date': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
+ 'description': ('django.db.models.fields.TextField', [], {'null': 'True', 'blank': 'True'}),
+ 'height': ('django.db.models.fields.IntegerField', [], {}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'image_file': ('django.db.models.fields.files.ImageField', [], {'max_length': '2048'}),
+ 'modification_date': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}),
+ 'title': ('django.db.models.fields.CharField', [], {'max_length': '1024', 'null': 'True', 'blank': 'True'}),
+ 'width': ('django.db.models.fields.IntegerField', [], {})
+ }
+ }
+
+ complete_apps = ['metadatacomposer']
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/metadatacomposer/models.py Tue May 21 14:54:41 2013 +0200
@@ -0,0 +1,24 @@
+from django.db import models
+import os
+import time
+
+
+def image_file_name(instance, filename):
+ now = time.time()
+ milliseconds = '%03d' % int((now - int(now)) * 1000)
+ localtime = time.localtime()
+ _, ext = os.path.splitext(filename)
+ filename = time.strftime('%Y%m%d%H%M%S', localtime) + milliseconds
+ return 'metadatacomposer/images/' + filename + ext
+
+
+class Image(models.Model):
+
+ image_file = models.ImageField(width_field="width", height_field="height", upload_to=image_file_name, max_length=2048)
+ width = models.IntegerField(null=False, blank=False)
+ height = models.IntegerField(null=False, blank=False)
+ title = models.CharField(null=True, blank=True, max_length=1024)
+ description = models.TextField(null=True, blank=True)
+ creation_date = models.DateTimeField(auto_now_add=True)
+ modification_date = models.DateTimeField(auto_now=True)
+
\ No newline at end of file
--- a/src/metadatacomposer/templates/metadatacomposer_home.html Thu May 16 18:11:00 2013 +0200
+++ b/src/metadatacomposer/templates/metadatacomposer_home.html Tue May 21 14:54:41 2013 +0200
@@ -46,7 +46,7 @@
<!-- block containers -->
<article class="container">
<div class="row article-title">
- <h3 class="span8"><i class="icon-file"></i>Derniers projets</h3>
+ <h3 class="span8"><i class="icon-file"></i> Derniers projets</h3>
<div class="span4 wrap-btn-add"><a class="btn btn-success" href="#">Ajouter un projet <i class="icon-plus-sign"></i></a></div>
</div>
<div class="row">
@@ -64,30 +64,12 @@
<div class="span4 wrap-btn-add"><a class="btn btn-success" href="#">Ajouter une image <i class="icon-plus-sign"></i></a></div>
</div>
<div class="row">
- <div class="span2">
- <a href=""><img src="{% static 'metadatacomposer/img/140x140.gif' %}" alt=""></a>
- <h4><a href="">Titre de l'image</a></h4>
- </div>
+ {% for i in images %}
<div class="span2">
- <a href=""><img src="{% static 'metadatacomposer/img/140x140.gif' %}" alt=""></a>
- <h4><a href="">Titre de l'image</a></h4>
- </div>
- <div class="span2">
- <a href=""><img src="{% static 'metadatacomposer/img/140x140.gif' %}" alt=""></a>
- <h4><a href="">Titre de l'image</a></h4>
+ <a href="">{% thumbnail i.image_file "140x140" crop="center" format="PNG" as im %}<img src="{{ im.url }}" width="{{ im.width }}" height="{{ im.height }}" />{% empty %}<img src="{% static 'metadatacomposer/img/140x140.gif' %}" width="140px" height="140px" />{% endthumbnail %}
+ <h4>{{ i.title }}</h4></a>
</div>
- <div class="span2">
- <a href=""><img src="{% static 'metadatacomposer/img/140x140.gif' %}" alt=""></a>
- <h4><a href="">Titre de l'image</a></h4>
- </div>
- <div class="span2">
- <a href=""><img src="{% static 'metadatacomposer/img/140x140.gif' %}" alt=""></a>
- <h4><a href="">Titre de l'image</a></h4>
- </div>
- <div class="span2">
- <a href=""><img src="{% static 'metadatacomposer/img/140x140.gif' %}" alt=""></a>
- <h4><a href="">Titre de l'image</a></h4>
- </div>
+ {% endfor %}
</div>
</article>
<article class="container">
--- a/src/metadatacomposer/templates/metadatacomposer_modal_image.html Thu May 16 18:11:00 2013 +0200
+++ b/src/metadatacomposer/templates/metadatacomposer_modal_image.html Tue May 21 14:54:41 2013 +0200
@@ -1,4 +1,4 @@
-{% load static %}
+{% load static %}
<div class="row">
<div class="span3">
<ul class="modal-menu">
@@ -8,20 +8,23 @@
</div>
<div class="span8 offset1 popup-content">
<h4>Informations et source de l'image</h4>
- <form class="form-horizontal" action="#" enctype="multipart/form-data" method="post">
+ <form class="form-horizontal" action="{% url composer_modal_image branding=branding %}" enctype="multipart/form-data" method="post">{% csrf_token %}
+ {% for error in form.errors.name %}
+ <span class="error">{{ error }}</span>
+ {% endfor %}
<div class="control-group">
<label class="control-label" for="media-title">Titre :</label>
<div class="controls">
- <input type="text" name="title-media" id="media-title" placeholder="Titre de l'image..">
+ <input type="text" name="title" id="id_title" placeholder="Titre de l'image.." maxlength="1024"/>
</div>
</div>
<div class="control-group">
<label class="control-label" for="media-description">Description :</label>
<div class="controls">
- <textarea name="" id="media-description"></textarea>
+ <textarea name="description" id="id_description"></textarea>
</div>
</div>
- <div class="control-group">
+ <!--div class="control-group">
<label class="control-label" for="media-type-select">Type de la source :</label>
<div class="controls">
<select name="" id="media-type-select">
@@ -36,11 +39,11 @@
<input type="text" name="title-media" id="media-url-link" placeholder="Titre de l'image..">
<a href="#" class="btn btn-previsualisation" class="btn"><i class="icon-eye-open"></i> Prévisualiser</a>
</div>
- </div>
+ </div-->
<div class="control-group input-image-upload">
<label class="control-label" for="media-url-link">Fichier de l'image :</label>
<div class="controls">
- <input type="file" name="title-media" id="media-file" placeholder="Titre de l'image..">
+ <input type="file" name="image_file" id="id_image_file"/>
<a href="#" class="btn btn-previsualisation" class="btn"><i class="icon-eye-open"></i> Prévisualiser</a>
</div>
</div>
--- a/src/metadatacomposer/views.py Thu May 16 18:11:00 2013 +0200
+++ b/src/metadatacomposer/views.py Tue May 21 14:54:41 2013 +0200
@@ -1,13 +1,13 @@
from django.conf import settings
-from django.contrib.auth.models import User
from django.contrib.auth.decorators import login_required
-from django.core.exceptions import ObjectDoesNotExist
from django.core.paginator import Paginator, InvalidPage, EmptyPage
-from django.http import HttpResponseNotFound, HttpResponse
from django.shortcuts import redirect
from django.utils.decorators import method_decorator
from django.views.generic.base import View, TemplateResponseMixin
+from django.views.generic.edit import FormView
from ldt.ldt_utils.models import Project, Content
+from metadatacomposer.forms import ImageUploadModelForm
+from metadatacomposer.models import Image
import logging #@UnresolvedImport
@@ -39,10 +39,12 @@
self.branding = branding
projects = Project.safe_objects.filter(owner=request.user).order_by('-modification_date')[:6]
+ images = Image.objects.order_by('-modification_date')[:6]
+ logger.debug(images)
contents = Content.safe_objects.order_by('-update_date')[:6]
context = self.get_context_dict(request)
- context.update({"projects":projects, "contents": contents})
+ context.update({"projects":projects, "images": images, "contents": contents})
return self.render_to_response(context)
@@ -173,6 +175,33 @@
def get(self, request, branding="iri", **kwargs):
self.branding = branding
+
+ # Add form
+ form = ImageUploadModelForm()
+
context = self.get_context_dict(request)
+ context.update({"form":form})
return self.render_to_response(context)
-
\ No newline at end of file
+
+ def post(self, request, branding="iri", **kwargs):
+ self.branding = branding
+
+ # Check form
+ form = ImageUploadModelForm(request.POST, request.FILES)
+ logger.debug("COUCOU 1")
+ logger.debug(form)
+ logger.debug(form.is_valid())
+ if form.is_valid():
+ logger.debug("COUCOU 2")
+ # If an image id was in the form, we update the existing image
+ if 'image_pk' in request.POST:
+ form = ImageUploadModelForm(request.POST, request.FILES, instance=Image.objects.get(pk=request.POST['image_pk']))
+ form.save()
+ else:
+ logger.debug("COUCOU 3")
+ context = self.get_context_dict(request)
+ context.update({"form":form})
+ return redirect(request.META['HTTP_REFERER'])
+
+
+