Commit dd6939a5 authored by Jasper St. Pierre's avatar Jasper St. Pierre

Add screenshot uploading capability.

Use sorl-thumbnail and ImageField to create a quick image upload capability.
parent 6cf3d98f
......@@ -31,6 +31,7 @@ For now, we require:
* django-autoslug_
* django-extensions_
* django-tagging_
* sorl-thumbnail_
I develop with PostgreSQL_ at home, but Django should be able to use
SQLite_, MySQL_, and others.
......@@ -39,6 +40,7 @@ SQLite_, MySQL_, and others.
.. _django-autoslug: http://packages.python.org/django-autoslug/
.. _django-extensions: http://packages.python.org/django-extensions/
.. _django-tagging: http://code.google.com/p/django-tagging/
.. _sorl-thumbnail: http://thumbnail.sorl.net/
.. _PostgreSQL: http://www.postgresql.org/
.. _SQLite: http://www.sqlite.org/
.. _MySQL: http://www.mysql.com/
......@@ -2,5 +2,6 @@ Django==1.3
django-autoslug==1.4.1
django-extensions==0.6
django-tagging==0.3.1
sorl-thumbnail==11.05.2
psycopg2==2.4.2
wsgiref==0.1.2
{% extends "base.html" %}
{% load thumbnail %}
{% block body %}
<div class="extension" data-uuid="{{ extension.uuid }}" data-manifest="{{ manifest }}">
<div class="button"></div>
......@@ -11,12 +12,31 @@
<ul>
{% if user == extension.creator %}
<li><a href="{% url upload-file pk=extension.pk %}">Upload a new version</a></li>
<li><a href="{% url upload-screenshot pk=extension.pk %}">Upload screenshot</a></li>
{% endif %}
{% if user.is_superuser %}
<li><a class="featured" href="#" data-uuid="{{ extension.uuid }}"></a></li>
{% endif %}
</ul>
<hr>
<h4> Screenshots </h4>
{% if extension.screenshot_set.count %}
<ul>
{% for screenshot in extension.screenshot_set.all %}
<li>
<p>
{% thumbnail screenshot.image "300x200" as th %}
<a href="{{ screenshot.image.url }}"><img src="{{ th.url }}" /></a>
{% endthumbnail %}
</p>
<p>{{ screenshot.title }}</p>
</li>
{% endfor %}
</ul>
{% else %}
This extension has no screenshots.
{% endif %}
<hr>
{% load comments %}
......
{% extends "base.html" %}
{% block body %}
<form enctype="multipart/form-data" action=""
method="POST">
{% csrf_token %}
{{ form }}
<input type="submit" value="Submit">
</form>
{% endblock %}
......@@ -4,7 +4,8 @@ from django.conf.urls.defaults import patterns, url
from extensions.models import Extension
from tagging.views import tagged_object_list
from browse.views import detail, manifest, download, list_ext, browse_tag, modify_tag
from browse.views import detail, manifest, download, list_ext, browse_tag
from browse.views import modify_tag, upload_screenshot
urlpatterns = patterns('',
url(r'^$', list_ext, name='ext-index'),
......@@ -21,4 +22,6 @@ urlpatterns = patterns('',
url(r'^extension/(?P<pk>\d+)/(?P<slug>.+)', detail, name='ext-detail'),
url(r'^extension/(?P<pk>\d+)', detail, dict(slug=None), name='ext-detail'),
url(r'^extension/upload-screenshot/(?P<pk>\d+)', upload_screenshot, name='upload-screenshot'),
)
......@@ -8,8 +8,9 @@ from django.shortcuts import get_object_or_404, render, redirect
from django.core.urlresolvers import reverse
from django.http import HttpResponse, HttpResponseForbidden
from django.contrib.auth.decorators import login_required
from django import forms
from extensions.models import Extension
from extensions.models import Extension, Screenshot
def get_manifest_url(request, ver):
manifest_url = reverse('ext-manifest', kwargs=dict(uuid=ver.extension.uuid))
......@@ -60,6 +61,30 @@ def download(request, uuid):
url = reverse('ext-url', kwargs=dict(filepath=version.source.url))
return redirect(request.build_absolute_uri(url))
class UploadScreenshotForm(forms.ModelForm):
class Meta:
model = Screenshot
exclude = 'extension',
@login_required
def upload_screenshot(request, pk):
extension = get_object_or_404(Extension, pk=pk)
if request.user != extension.creator:
return HttpResponseForbidden()
if request.method == 'POST':
form = UploadScreenshotForm(request.POST, request.FILES)
if form.is_valid():
screenshot = form.save(commit=False)
screenshot.extension = extension
screenshot.save()
return redirect(reverse('ext-detail', kwargs=dict(pk=extension.pk)))
else:
form = UploadScreenshotForm(initial=dict(extension="FOO"))
return render(request, 'upload-screenshot.html', dict(form=form))
def browse_tag(request, tag):
tag_inst = get_tag(tag)
if tag_inst is None:
......
from django.contrib import admin
from extensions.models import Extension, ExtensionVersion
from sorl.thumbnail.admin import AdminImageMixin
from extensions.models import Extension, ExtensionVersion, Screenshot
class ExtensionVersionAdmin(admin.TabularInline):
model = ExtensionVersion
list_display = 'version', 'source'
class ScreenshotAdmin(AdminImageMixin, admin.TabularInline):
model = Screenshot
list_display = 'title', 'image'
class ExtensionAdmin(admin.ModelAdmin):
list_display = 'is_published', 'name', 'num_versions', 'creator'
list_display_links = 'name',
......@@ -14,6 +20,6 @@ class ExtensionAdmin(admin.ModelAdmin):
return ext.extensionversion_set.count()
num_versions.short_description = "#V"
inlines = [ExtensionVersionAdmin]
inlines = [ExtensionVersionAdmin, ScreenshotAdmin]
admin.site.register(Extension, ExtensionAdmin)
......@@ -8,6 +8,7 @@ import autoslug
import tagging
from django.contrib import auth
from django.db import models
from sorl import thumbnail
# Create your models here.
......@@ -123,3 +124,12 @@ class ExtensionVersion(models.Model):
extension, version = cls.from_metadata_json(metadata, extension)
zipfile.close()
return extension, version
class Screenshot(models.Model):
extension = models.ForeignKey(Extension)
title = models.TextField()
def make_filename(self, filename):
return os.path.join(self.extension.uuid, filename)
image = thumbnail.ImageField(upload_to=make_filename)
......@@ -126,6 +126,7 @@ INSTALLED_APPS = (
'django.contrib.staticfiles',
'django_extensions',
'tagging',
'sorl.thumbnail',
'extensions',
'browse',
......
Markdown is supported
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