Commit da5947b8 authored by Christophe Siraut's avatar Christophe Siraut

Update project layout. Add base for poll app.

parent bf4358d5
#!/usr/bin/python
from django.core.management import execute_manager
try:
import settings # Assumed to be in the same directory.
except ImportError:
import sys
sys.stderr.write("Error: Can't find the file 'settings.py' in the directory containing %r. It appears you've customized things.\nYou'll have to run django-admin.py, passing it your settings module.\n(If the file settings.py does indeed exist, it's causing an ImportError somehow.)\n" % __file__)
sys.exit(1)
#!/usr/bin/env python
import os
import sys
if __name__ == "__main__":
execute_manager(settings)
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "nuages.settings")
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)
......@@ -6,7 +6,7 @@ import os
DEBUG = True
TEMPLATE_DEBUG = DEBUG
PROJECT_DIR = os.path.normpath(os.path.dirname(__file__))
PROJECT_DIR = os.path.normpath(os.path.dirname(os.path.dirname(__file__)))
ADMINS = (
# ('Your Name', 'your_email@domain.com'),
)
......@@ -108,7 +108,7 @@ MIDDLEWARE_CLASSES = (
#'request.middleware.RequestMiddleware',
)
ROOT_URLCONF = 'urls'
ROOT_URLCONF = 'nuages.urls'
TEMPLATE_DIRS = (
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
......@@ -128,6 +128,7 @@ INSTALLED_APPS = (
'django.contrib.admindocs',
'registration',
'account',
'poll',
'meetingpoll',
)
......
......@@ -31,7 +31,7 @@ urlpatterns = patterns('',
url(r'^(?P<poll_id>\w+)/edit/choices/$', 'meetingpoll.views.editchoices', name='choices'),
url(r'^(?P<poll_id>\w{4})/vote/$', 'meetingpoll.views.vote', name='vote'),
url(r'^(?P<poll_id>\w+)/csv/$', 'meetingpoll.views.exp_csv'),
url(r'^accounts/profile/$', 'accounts.views.profile', name='profile'),
url(r'^accounts/profile/$', 'account.views.profile', name='profile'),
url(r'^accounts/', include('registration.backends.default.urls')),
url(r'^i18n/', include('django.conf.urls.i18n')),
url(r'^(?P<poll_id>\w{4})/clear/', 'meetingpoll.userviews.clear_cookie'),
......
......@@ -2,8 +2,8 @@ import os
import sys
sys.path.append('/usr/local/lib/nuages')
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
os.environ['DJANGO_SETTINGS_MODULE'] = 'nuages.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
__version__ = '0.1.0'
from django.contrib import admin
from models import Poll, Choice, Vote
class ChoiceInline(admin.TabularInline):
model = Choice
extra = 1
class PollAdmin(admin.ModelAdmin):
model = Poll
inlines = (ChoiceInline,)
list_display = ('question', 'count_choices', 'count_total_votes')
class VoteAdmin(admin.ModelAdmin):
model = Vote
list_display = ('choice', 'user', 'poll')
admin.site.register(Poll, PollAdmin)
admin.site.register(Vote, VoteAdmin)
from django.db import models
from django.contrib.auth.models import User
class Poll(models.Model):
question = models.CharField(max_length=255)
description = models.TextField(blank=True)
def count_choices(self):
return self.choice_set.count()
def count_total_votes(self):
result = 0
for choice in self.choice_set.all():
result += choice.count_votes()
return result
def can_vote(self, user):
return not self.vote_set.filter(user=user).exists()
def __unicode__(self):
return self.question
class Choice(models.Model):
poll = models.ForeignKey(Poll)
choice = models.CharField(max_length=255)
def count_votes(self):
return self.vote_set.count()
def __unicode__(self):
return self.choice
class Meta:
ordering = ['choice']
class Vote(models.Model):
user = models.ForeignKey(User)
poll = models.ForeignKey(Poll)
choice = models.ForeignKey(Choice)
def __unicode__(self):
return u'Vote for %s' % (self.choice)
class Meta:
unique_together = (('user', 'poll'))
{% extends "base.html" %}
{% load i18n %}
{% load polls %}
{% block content %}
<h1>{{poll.question}}</h1>
{% if poll.votable %}
<form action="{% url polls:vote poll.id %}" method="post">
{% csrf_token %}
{% for choice in poll.choice_set.all %}
<label class="radio"><input type="radio" name="choice_pk" value="{{choice.id}}"> {{choice.choice}}</label>
{% endfor %}
<input type="submit" class="btn btn-primary" value="{% trans "Vote" %}">
</form>
{% else %}
<ul>
{% for choice in poll.choice_set.all %}
<li>{{choice.choice}} - {{choice.count_votes}}</li>
{% endfor %}
</ul>
{% endif %}
{% endblock %}
{% extends "base.html" %}
{% load i18n %}
{% block content %}
<h1>{% trans "Polls" %}</h1>
{% if poll_list %}
<ul>
{% for poll in poll_list.all %}
<li><a href="{% url polls:detail poll.id %}">{{poll.question}}</a></li>
{% endfor %}
</ul>
{% else %}
{% trans "There are no polls available." %}
{% endif %}
{% endblock %}
"""
This file demonstrates writing tests using the unittest module. These will pass
when you run "manage.py test".
Replace this with more appropriate tests for your application.
"""
from django.test import TestCase
class SimpleTest(TestCase):
def test_basic_addition(self):
"""
Tests that 1 + 1 always equals 2.
"""
self.assertEqual(1 + 1, 2)
from django.conf.urls.defaults import patterns, url
from django.contrib.auth.decorators import login_required
from views import PollDetailView, PollListView, PollVoteView
urlpatterns = patterns('',
url(r'^$', PollListView.as_view(), name='list'),
url(r'^(?P<pk>\d+)/$', PollDetailView.as_view(), name='detail'),
url(r'^(?P<pk>\d+)/vote/$', login_required(PollVoteView.as_view()), name='vote'),
)
from django.views.generic import DetailView, ListView, RedirectView
from django.core.urlresolvers import reverse_lazy
from django.contrib import messages
from django.utils.translation import ugettext_lazy as _
from models import Choice, Poll, Vote
class PollListView(ListView):
model = Poll
class PollDetailView(DetailView):
model = Poll
def get_context_data(self, **kwargs):
context = super(PollDetailView, self).get_context_data(**kwargs)
context['poll'].votable = self.object.can_vote(self.request.user)
return context
class PollVoteView(RedirectView):
def post(self, request, *args, **kwargs):
poll = Poll.objects.get(id=kwargs['pk'])
user = request.user
choice = Choice.objects.get(id=request.POST['choice_pk'])
Vote.objects.create(poll=poll, user=user, choice=choice)
messages.success(request, _("Thanks for your vote."))
return super(PollVoteView, self).post(request, *args, **kwargs)
def get_redirect_url(self, **kwargs):
return reverse_lazy('polls:detail', args=[kwargs['pk']])
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