-
-
Save jacobb/2942999 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
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() | |
form = PhotoForm(request.POST or None) | |
if form.is_valid(): | |
new_photo = form.save() | |
response_data = { | |
'form': form | |
} | |
form = PhotoForm(request.POST or None, request.FILES or None) | |
if form.is_valid(): | |
new_upload = form.save() | |
else: | |
print form.errors | |
context = { | |
"photos": Photo.objects.filter(approved=True), | |
'form': form, | |
"textbox": textbox, | |
} | |
return render(request, "index.html", dictionary=context) | |
@csrf_exempt | |
def upload_raw_photo(request): | |
if request.method == 'POST': | |
raw_photo = RawPhoto() | |
raw_content_file = ContentFile(request.raw_post_data) | |
file_name = "%s.jpg" % hashlib.md5(request.raw_post_data).hexdigest() | |
raw_photo.photo.save(file_name, raw_content_file) | |
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 import forms | |
from photo_upload.models import * | |
class PhotoForm(forms.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