Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

started templates, geoimg static files, further model/view work

  • Loading branch information...
commit 2c0b6adda68361ed355add22cfa226ceda6fbc22 1 parent 0066828
csjohn authored
View
3  ImgJoust/urls.py
@@ -33,4 +33,7 @@
url(r'^sandbox/', include('sandbox.urls', namespace='sandbox')),
(r'^test_api/', include(test_v1.urls)),
(r'^api/', include(v1_api.urls)),
+ url(r'^about/', 'geoimages.views.about'),
+ url(r'^upload/', 'geoimages.views.image_upload'),
@danielmoniz Collaborator

See line 32 (right above url(r'sandbox ...) ) to see how I've included all of the URLs that we put into geoimages/urls.py. This lets us keep the geoimages app specific code in the geoimages app. Eg. the /about page is probably fine to be here if we move its view code elsewhere (eg. ImgJoust/views), but the upload URL rule should definitely be in geoimages/urls.py.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ url(r'^$', 'geoimages.views.home', name='default')
)
View
7 django_templates/geoimages/about.html
@@ -0,0 +1,7 @@
+{% extends "geoimages/base.html" %}
+
+{% block title %} About ImgJoust {% endblock %}
+
+{% block content %}
+This can be an about page with our info and some exagerated crap about our "team"... lawl. Sar! De've gone up da' venelation shaft!
+{% endblock %}
View
20 django_templates/geoimages/base.html
@@ -0,0 +1,20 @@
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
+<html lang="en">
+{% load staticfiles %}
+<script type="text/javascript"
+src="http://maps.google.com/maps/api/js?sensor=false&libraries=places"></script>
+<script type='text/javascript' src="{% static "main.js" %}"></script>
+<link rel="stylesheet" href="{% static "main.css" %}" type="text/css" media="screen" />
+<head>
+ <title> ImgJoust - {% block title %}{% endblock %}</title>
+</head>
+<body>
+ <h1>ImgJoust</h1>
+ {% block content %}{% endblock %}
+ {% block footer %}
+ <p>Ye, bitches.</p>
+ {% endblock %}
+</body>
+</html>
+
+
View
9 django_templates/geoimages/home.html
@@ -0,0 +1,9 @@
+{% extends "geoimages/base.html" %}
+
+{% block title %} Welcome to ImgJoust! {% endblock %}
+
+{% block content %}
+<h1>Fill dis' bitch yo. </h1>
+<p>This will eventually be some home page, top images or something... Uhh Jengo... shoot her... or .. something...</p>
+{% endblock %}
+
View
12 django_templates/geoimages/upload.html
@@ -1,6 +1,18 @@
<h2>Upload a New Image</h2>
+{% extends 'geoimages/base.html' %}
+
+{% block title %}
+Upload an image!
+{% endblock %}
+
+
+{% block content %}
+<div id="mapCanvas" style="width:500px; height:500px;"></div>
+<input type="text" id="autocomplete" autocomplete="off">
+<div id="results"></div>
<form enctype="multipart/form-data" method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" />
</form>
+{% endblock %}
View
24 django_templates/geoimages/upload.html~
@@ -0,0 +1,24 @@
+<<<<<<< HEAD
+<h2>Upload a New Image</h2>
+=======
+{% extends 'geoimages/base.html' %}
+
+{% block title %}
+Upload an image!
+{% endblock %}
+
+
+{% block content %}
+<div id="mapCanvas" style="width:500px; height:500px;"></div>
+<input type="text" id="autocomplete" autocomplete="off">
+<div id="results"></div>
+>>>>>>> started templates, geoimg static files, further model/view work
+<form enctype="multipart/form-data" method="post">
+{% csrf_token %}
+{{ form.as_p }}
+<input type="submit" />
+</form>
+<<<<<<< HEAD
+=======
+{% endblock %}
+>>>>>>> started templates, geoimg static files, further model/view work
View
17 geoimages/models.py
@@ -1,6 +1,8 @@
from django.db import models
from django.contrib.auth.models import User
from django import forms
+from django.forms import ModelForm
@danielmoniz Collaborator

We don't need to bring in ModelForm here, as we can already access it via forms.ModelForm (imported above). Given that I've often needed to import a great many modules, we want to work to avoid clutter here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+
class UserProfile(models.Model):
user = models.OneToOneField(User, primary_key=True)
@@ -19,8 +21,12 @@ class Image(models.Model):
# fields for lat and lng that are DecimalFields.
# Also, check out http://code.google.com/p/django-coordinatesfield/ for a
# custom field somebody has already made.
- longitude = models.DecimalField(db_index=True, max_digits=9, decimal_places=6)
- latitude = models.DecimalField(db_index=True, max_digits=9, decimal_places=6)
+
+ # @TODO We need to make sure our database representation is sufficient to
+ # store all possible long/lat numbers. (enough digits & range)
+ longitude = models.DecimalField(db_index=True, max_digits=9, decimal_places=8)
+ latitude = models.DecimalField(db_index=True, max_digits=9, decimal_places=8)
@danielmoniz Collaborator

Why has this been reverted? The red text (removed by you) was created so that we can store numbers with a number of pre-decimal digits greater than 1, eg. 35.6 or 359.

@csjohn Owner
csjohn added a note

This wasn't reverted? The fields (long & late) are below - I just put in a comment and some spacing.

@danielmoniz Collaborator

Yeah, but you changed the number of decimal places to a value that doesn't work. I tried 9 and 8 as well, but found out that is means that allocates all 8 potential decimal places, ie. it only allows for 9 - 8 digits before the decimal. So the values don't work now.

@csjohn Owner
csjohn added a note

Oh weird, I don't remember doing that at all. We'll have to look into a better representation as well, coordinates (especially those provided by google maps API) can get to be quite a few more digits (i.e. -79.39999999999998).

@danielmoniz Collaborator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+
location_hash = models.CharField(max_length=64, blank=True, null=True)
name = models.CharField(max_length=32)
description = models.CharField(max_length=256, default='', blank=True)
@@ -55,3 +61,10 @@ class Vote(models.Model):
# entry is re-saved.
datetime = models.DateTimeField(auto_now=True)
"""
+
+### FORMS ###
+
+class ImageForm(ModelForm):
@danielmoniz Collaborator

Take a look at the ImageUploadForm (in the same file), which I made earlier. I added a field to it called Image, as our Image model doesn't provide a link to a real image. I've been assuming we're going to be doing that programmatically. I was also whitelisting the fields we want to use in the form, rather than blacklisting them. I'm not sure what the best approach would be.

@csjohn Owner
csjohn added a note

Yeah whitelisting is fine by me. I didn't see your changes in the same file until I had finished mine, I was doing stuff at random points throughout the day and didn't pull until late in the night.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ class Meta:
+ model = Image
+ exclude = ('author', 'created', 'num_upvotes', 'num_downvotes')
View
77 geoimages/models.py~
@@ -0,0 +1,77 @@
+from django.db import models
+from django.contrib.auth.models import User
+<<<<<<< HEAD
+from django import forms
+=======
+from django.forms import ModelForm
+>>>>>>> started templates, geoimg static files, further model/view work
+
+class UserProfile(models.Model):
+ user = models.OneToOneField(User, primary_key=True)
+ joined = models.DateTimeField(auto_now_add=True)
+ last_login = models.DateTimeField(auto_now=True)
+ bio = models.TextField(default='', blank=True)
+
+
+class Image(models.Model):
+ author = models.ForeignKey(User)
+ created = models.DateTimeField(auto_now_add=True)
+ num_upvotes = models.IntegerField(default=0)
+ num_downvotes = models.IntegerField(default=0)
+ # @TODO Figure out how best to represent location. One idea is to create a
+ # custom field that stores both lat and lng. Or, simply have two separate
+ # fields for lat and lng that are DecimalFields.
+ # Also, check out http://code.google.com/p/django-coordinatesfield/ for a
+ # custom field somebody has already made.
+<<<<<<< HEAD
+ longitude = models.DecimalField(db_index=True, max_digits=9, decimal_places=6)
+ latitude = models.DecimalField(db_index=True, max_digits=9, decimal_places=6)
+=======
+
+ # @TODO We need to make sure our database representation is sufficient to
+ # store all possible long/lat numbers. (enough digits & range)
+ longitude = models.DecimalField(db_index=True, max_digits=9, decimal_places=8)
+ latitude = models.DecimalField(db_index=True, max_digits=9, decimal_places=8)
+
+>>>>>>> started templates, geoimg static files, further model/view work
+ location_hash = models.CharField(max_length=64, blank=True, null=True)
+ name = models.CharField(max_length=32)
+ description = models.CharField(max_length=256, default='', blank=True)
+ # @TODO Filepath for images? Can simply rebuild this from data if need be.
+
+class ImageUploadForm(forms.ModelForm):
+ image = forms.ImageField()
+
+ class Meta:
+ model = Image
+ fields = ('longitude', 'latitude', 'name', 'description')
+
+class Album(models.Model):
+ name = models.CharField(max_length=32)
+ description = models.TextField(default='', blank=True)
+ creator = models.ForeignKey(User)
+ created = models.DateTimeField(auto_now_add=True)
+ images = models.ManyToManyField(Image)
+
+"""
+class Vote(models.Model):
+ NOTE: This is being written such that it can be moved to a separate app.
+
+ vote_choices = ((1, 'Up'), (0, 'Neutral'), (-1, 'Down'))
+ value = models.IntegerField(max_length=32, choices=vote_choices)
+ subject_choices = (('Image', 'Image'), ('Album', 'Album'))
+ subject = models.CharField(max_length=32, choices=subject_choices)
+ # @TODO Not too sure if this will work, or work well/easily.
+ subject_id = models.IntegerField()
+ user = models.ForeignKey(User)
+ # The datetime is modified whenever the user re-votes and this database
+ # entry is re-saved.
+ datetime = models.DateTimeField(auto_now=True)
+"""
+
+### FORMS ###
+
+class ImageForm(ModelForm):
+ class Meta:
+ model = Image
+ exclude = ('author', 'created', 'num_upvotes', 'num_downvotes')
View
16 geoimages/views.py
@@ -9,6 +9,7 @@
import ImgJoust
from geoimages.models import Image, ImageUploadForm
+from geoimages.models import ImageForm
# Utility
import os
@@ -72,3 +73,18 @@ def handle_uploaded_file(new_file, file_name):
destination.write(chunk)
destination.close()
return new_file.name
+
+
+def image_upload(request):
@danielmoniz Collaborator

Take a look at the class-based "generic view" that I made called ImageCreateView, which is in the same file. I can talk to you in person about the benefits of using generic views over writing the entire view ourselves. In this case, I let the generic view handle almost all functionality, but I override the form_valid() function to ensure that when the form is valid, I actually add some other data (such as image.author, etc.) before saving.

@csjohn Owner
csjohn added a note

This applies to several things lol - but I didn't see your additions until last night when I was commiting and rushing to bed. Haven't had a chance to see it yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ if request.method == 'POST':
+ form = ImageForm(request.POST, request.FILES)
+ #STUFF
+ else:
+ form = ImageForm()
+ return render_to_response('geoimages/upload.html', {'form':form})
+
+def home(request):
+ return render_to_response('geoimages/home.html')
+
+def about(request):
+ return render_to_response('geoimages/about.html')
View
94 geoimages/views.py~
@@ -0,0 +1,94 @@
+<<<<<<< HEAD
+from django.views.generic import CreateView
+from django.http import HttpResponse, HttpResponseRedirect
+from django.core.urlresolvers import reverse
+from django.shortcuts import render_to_response
+from django.template import RequestContext
+from django.contrib.auth.decorators import login_required
+from django.template.defaultfilters import slugify
+
+import ImgJoust
+
+from geoimages.models import Image, ImageUploadForm
+
+# Utility
+import os
+
+class ImageCreateView(CreateView):
+ """Allows a user to upload a new picture."""
+
+ template_name = "geoimages/upload.html"
+ model = Image
+ form_class = ImageUploadForm
+
+ # @TODO Add this when we have a login form.
+ #@login_required
+ def dispatch(self, request, *args, **kwargs):
+ return super(ImageCreateView, self).dispatch(request, *args, **kwargs)
+
+ def form_valid(self, form):
+ """If the form is valid, save all data for the Image.
+ Also upload the image to the fileserver.
+ """
+ if handle_uploaded_file(self.request.FILES['image'], self.request.POST['name']):
+ image = form.save(commit=False)
+ image.author = self.request.user
+ image.save()
+ else:
+ return HttpResponse('file name already exists!')
+
+ #return HttpResponse('success')
+ # @TODO Find some way of alerting the user of success.
+ return HttpResponseRedirect(reverse('geoimages:image_upload'))
+
+def handle_uploaded_file(new_file, file_name):
+ """Takes a file and a proposed filename. Uploads the file to the
+ appropriate directory and massages file_name into a usable name.
+ """
+ username = 'testman'
+
+ # Find the extension of the uploaded file
+ file_split = new_file.name.split('.')
+ if len(file_split) > 1:
+ extension = file_split[-1]
+ else:
+ extension = '.jpg'
+
+ file_name = slugify(file_name)
+ file_name += ".{}".format(extension)
+ image_name = username + "_" + file_name
+
+ image_dir = ImgJoust.settings.PROJECT_DIR + "images/"
+
+ # Make directory if it does not exist.
+ if not os.path.exists(image_dir):
+ os.makedirs(image_dir)
+
+ # Check to see if image of that name exists already
+ if os.path.isfile(image_dir + image_name):
+ return False
+
+ destination = open(image_dir + image_name, 'wb+')
+ for chunk in new_file.chunks():
+ destination.write(chunk)
+ destination.close()
+ return new_file.name
+=======
+from django.http import HttpResponse
+from django.shortcuts import render_to_response
+from geoimages.models import ImageForm
+
+def image_upload(request):
+ if request.method == 'POST':
+ form = ImageForm(request.POST, request.FILES)
+ #STUFF
+ else:
+ form = ImageForm()
+ return render_to_response('geoimages/upload.html', {'form':form})
+
+def home(request):
+ return render_to_response('geoimages/home.html')
+
+def about(request):
+ return render_to_response('geoimages/about.html')
+>>>>>>> started templates, geoimg static files, further model/view work
View
BIN  sandbox/images/testman_pokemon
Binary file not shown
@danielmoniz

Why has this been reverted? The red text (removed by you) was created so that we can store numbers with a number of pre-decimal digits greater than 1, eg. 35.6 or 359.

@danielmoniz

We don't need to bring in ModelForm here, as we can already access it via forms.ModelForm (imported above). Given that I've often needed to import a great many modules, we want to work to avoid clutter here.

@danielmoniz

Take a look at the ImageUploadForm (in the same file), which I made earlier. I added a field to it called Image, as our Image model doesn't provide a link to a real image. I've been assuming we're going to be doing that programmatically. I was also whitelisting the fields we want to use in the form, rather than blacklisting them. I'm not sure what the best approach would be.

@danielmoniz

Why are there all of these files that end in ~? I think that maybe you need to be careful about what files vim is adding to the index, eg. don't just do a blind $ git add -A without looking at everything that is being added.

@danielmoniz

Take a look at the class-based "generic view" that I made called ImageCreateView, which is in the same file. I can talk to you in person about the benefits of using generic views over writing the entire view ourselves. In this case, I let the generic view handle almost all functionality, but I override the form_valid() function to ensure that when the form is valid, I actually add some other data (such as image.author, etc.) before saving.

@danielmoniz
Collaborator

See line 32 (right above url(r'sandbox ...) ) to see how I've included all of the URLs that we put into geoimages/urls.py. This lets us keep the geoimages app specific code in the geoimages app. Eg. the /about page is probably fine to be here if we move its view code elsewhere (eg. ImgJoust/views), but the upload URL rule should definitely be in geoimages/urls.py.

@csjohn

Assumed *~ files were in .gitignore, will check.

@csjohn

This applies to several things lol - but I didn't see your additions until last night when I was commiting and rushing to bed. Haven't had a chance to see it yet.

@csjohn

This wasn't reverted? The fields (long & late) are below - I just put in a comment and some spacing.

@csjohn

Yeah whitelisting is fine by me. I didn't see your changes in the same file until I had finished mine, I was doing stuff at random points throughout the day and didn't pull until late in the night.

@danielmoniz

Yeah, but you changed the number of decimal places to a value that doesn't work. I tried 9 and 8 as well, but found out that is means that allocates all 8 potential decimal places, ie. it only allows for 9 - 8 digits before the decimal. So the values don't work now.

@csjohn

Oh weird, I don't remember doing that at all. We'll have to look into a better representation as well, coordinates (especially those provided by google maps API) can get to be quite a few more digits (i.e. -79.39999999999998).

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