Created
June 17, 2012 00:25
-
-
Save mvattuone/2942972 to your computer and use it in GitHub Desktop.
Photo Upload - MV
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
views.py | |
from django.shortcuts import render_to_response | |
from django.template import RequestContext | |
from django.views.decorators.csrf import csrf_exempt | |
from django.http import HttpResponse | |
from django.core.files.storage import default_storage | |
from django.core.files.base import ContentFile | |
from django.conf import settings | |
from django.shortcuts import render | |
import json | |
import md5 | |
from photo_upload.models import * | |
from photo_upload.forms import * | |
def index(request): | |
textbox = TextBox.objects.all() | |
context = { | |
"form": PhotoForm(), | |
"photos": Photo.objects.filter(approved=True), | |
"textbox": textbox, | |
} | |
if request.method == 'POST': | |
form = PhotoForm(request.POST, request.FILES) | |
if form.is_valid(): | |
new_upload = form.save() | |
else: | |
context['form'] = form | |
print form.errors | |
return render_to_response("index.html",context,context_instance=RequestContext(request)) | |
def photo(request): | |
form = PhotoForm(request.POST or None) | |
if form.is_valid(): | |
new_photo = form.save() | |
response_data = { | |
'form': form | |
} | |
return render(request, 'photo.html', dictionary=response_data) | |
@csrf_exempt | |
def upload_raw_photo(request): | |
if request.method == 'POST': | |
raw_photo = RawPhoto() | |
raw_content_file = ContentFile(request.raw_post_data) | |
file_name = hashlib.md5(imagefile.getvalue()).hexdigest()+'.jpg' | |
raw_photo.photo.save() | |
data = { | |
'success': True, | |
'file_name': file_name, | |
'file_url': raw_photo.photo.url, | |
'raw_photo_pk': raw_photo.pk | |
} | |
else: | |
data = {'success': False} | |
return HttpResponse(json.dumps(data)) | |
forms.py | |
from django.forms import ModelForm | |
from django import forms | |
from photo_upload.models import * | |
class PhotoForm(ModelForm): | |
class Meta: | |
model = Photo | |
exclude = {'approved',} | |
Models.py | |
from django.db import models | |
class TextBox(models.Model): | |
title = models.CharField(max_length=50) | |
description = models.CharField(max_length=460) | |
def __unicode__(self): | |
return '%s, %s' % (self.title, self.description,) | |
def get_title(self): | |
return '%s' % self.title | |
def get_description(self): | |
return '%s' % self.description | |
class RawPhoto(models.Model): | |
photo = models.ImageField(upload_to='raws/') | |
class Photo(models.Model): | |
name = models.CharField(max_length=50) | |
zip_code = models.CharField(max_length=5) | |
email = models.EmailField(max_length=75) | |
raw_photo = models.ForeignKey(RawPhoto, related_name='raw_photo') | |
# captioned_photo = models.ImageField(upload_to='captioned/') | |
approved = models.BooleanField() | |
def __unicode__(self): | |
return "%s, %s, %s" % (self.name, self.zip_code, self.email,) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment