Skip to content
Browse files

Views and url routing for images and audio.

  • Loading branch information...
1 parent f9224e6 commit 48c500f0333d05440d21c6b17eb0baa1d800973b @lukeman lukeman committed Dec 1, 2010
Showing with 72 additions and 1 deletion.
  1. +19 −0 mimesis/urls.py
  2. +53 −1 mimesis/views.py
View
19 mimesis/urls.py
@@ -0,0 +1,19 @@
+from django.conf.urls.defaults import patterns, url
+
+
+urlpatterns = patterns("upload.views",
+
+ # Images
+ url(r"^/image/add/", "add_image", name="add_image"),
+ url(r"^/image/add-for/(?P<app_label>[\w\-]+)/(?P<module_name>[\w\-]+)/(?P<obj_pk>\d+)/$",
+ "add_image", name="add_image_for"),
+ url(r"^/image/(?P<pk>\d+)/associate/(?P<app_label>[\w\-]+)/(?P<module_name>[\w\-]+)/(?P<obj_pk>\d+)/$",
+ "associate_image", name="associate_image"),
+
+ # Audio
+ # url(r"^/audio/add/", "add_audio", name="add_audio"),
+ # url(r"^/audio/add-for/(?P<app_label>[\w\-]+)/(?P<module_name>[\w\-]+)/(?P<obj_pk>\d+)/$",
+ # "add_audio", name="add_audio_for"),
+ # url(r"^/audio/(?P<pk>\d+)/associate/(?P<app_label>[\w\-]+)/(?P<module_name>[\w\-]+)/(?P<obj_pk>\d+)/$",
+ # "associate_audio", name="associate_audio"),
+)
View
54 mimesis/views.py
@@ -1 +1,53 @@
-# Create your views here.
+from django.db.models import get_model
+from django.http import HttpResponseRedirect
+from django.shortcuts import render_to_response, get_object_or_404
+from django.template.context import RequestContext
+from django.views.decorators.http import require_POST
+
+from django.contrib.auth.decorators import login_required
+
+from mimesis.forms import ImageForm, ImageAssociationForm
+from mimesis.forms import AudioForm, AudioAssociationForm
+from mimesis.models import Image, Audio, Video
+
+
+@login_required
+@require_POST
+def add_image(request, template_name="mimesis/image_add.html",
+ app_label=None, model_name=None, obj_pk=None):
+
+ if obj_pk:
+ model = get_model(app_label, model_name)
+ obj = get_object_or_404(model, pk=obj_pk)
+
+ next = request.POST.get("next", "/")
+
+ form = ImageForm(request.POST, request.FILES)
+ if form.is_valid():
+ image = form.save(request)
+ return HttpResponseRedirect(next)
+ else:
+ return render_to_response(template_name, {
+ "form": form,
+ }, RequestContext(request))
+
+
+@login_required
+@require_POST
+def associate_image(request, pk, app_label, model_name, obj_pk):
+
+ next = request.POST.get("next", "/")
+
+ model = get_model(app_label)
+ obj = get_object_or_404(model, pk=obj_pk)
+ image = get_object_or_404(Image, pk=pk)
+
+ form = ImageAssociationForm(request.POST)
+ if form.is_valid():
+ form.save(request, obj, image)
+ return HttpResponseRedirect(next)
+ else:
+ return render_to_response("/", {
+ "form": form,
+ }, RequestContext(request))
+

0 comments on commit 48c500f

Please sign in to comment.
Something went wrong with that request. Please try again.