Implement petition app
This commit is contained in:
@@ -2,3 +2,4 @@ import models
|
||||
from django.contrib import admin
|
||||
|
||||
admin.site.register(models.Post)
|
||||
admin.site.register(models.Comment)
|
||||
|
0
petition/__init__.py
Normal file
0
petition/__init__.py
Normal file
5
petition/admin.py
Normal file
5
petition/admin.py
Normal file
@@ -0,0 +1,5 @@
|
||||
import models
|
||||
from django.contrib import admin
|
||||
|
||||
admin.site.register(models.Petition)
|
||||
admin.site.register(models.Comment)
|
12
petition/forms.py
Normal file
12
petition/forms.py
Normal file
@@ -0,0 +1,12 @@
|
||||
from django import forms
|
||||
import models
|
||||
|
||||
class PetitionForm(forms.ModelForm):
|
||||
class Meta:
|
||||
model = models.Petition
|
||||
exclude = ('author', 'created', 'updated', 'closed')
|
||||
|
||||
class CommentForm(forms.ModelForm):
|
||||
class Meta:
|
||||
model = models.Comment
|
||||
exclude = ('author', 'created', 'updated', 'petition')
|
25
petition/models.py
Normal file
25
petition/models.py
Normal file
@@ -0,0 +1,25 @@
|
||||
from django.db import models
|
||||
from django.contrib.auth.models import User
|
||||
|
||||
class Petition(models.Model):
|
||||
author = models.ForeignKey(User, related_name='petitions')
|
||||
created = models.DateTimeField(editable=False, auto_now_add=True)
|
||||
updated = models.DateTimeField(editable=False, auto_now=True)
|
||||
body = models.TextField()
|
||||
closed = models.BooleanField(default=False)
|
||||
|
||||
def __unicode__(self):
|
||||
return self.body
|
||||
|
||||
class Comment(models.Model):
|
||||
author = models.ForeignKey(User, related_name='petition_comments')
|
||||
petition = models.ForeignKey(Petition)
|
||||
created = models.DateTimeField(editable=False, auto_now_add=True)
|
||||
updated = models.DateTimeField(editable=False, auto_now=True)
|
||||
body = models.TextField()
|
||||
|
||||
class Meta:
|
||||
ordering = ['-created']
|
||||
|
||||
def __unicode__(self):
|
||||
return self.body
|
16
petition/tests.py
Normal file
16
petition/tests.py
Normal file
@@ -0,0 +1,16 @@
|
||||
"""
|
||||
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)
|
7
petition/urls.py
Normal file
7
petition/urls.py
Normal file
@@ -0,0 +1,7 @@
|
||||
from django.conf.urls.defaults import patterns, include, url
|
||||
|
||||
urlpatterns = patterns('petition',
|
||||
url('^create$', 'views.create'),
|
||||
url('^(?P<id>[0-9]+)', 'views.view'),
|
||||
url('^comment/(?P<id>[0-9]+)', 'views.comment'),
|
||||
)
|
41
petition/views.py
Normal file
41
petition/views.py
Normal file
@@ -0,0 +1,41 @@
|
||||
import models
|
||||
import forms
|
||||
from django.shortcuts import render_to_response
|
||||
from django.template import RequestContext
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.http import HttpResponseRedirect
|
||||
from django.core.urlresolvers import reverse
|
||||
|
||||
@login_required
|
||||
def create(request):
|
||||
if request.method == 'POST':
|
||||
form = forms.PetitionForm(request.POST)
|
||||
else:
|
||||
form = forms.PetitionForm()
|
||||
if form.is_valid():
|
||||
petition = models.Petition()
|
||||
petition.author = request.user
|
||||
petition.body = form.cleaned_data['body']
|
||||
petition.save()
|
||||
return HttpResponseRedirect(reverse('petition.views.view', kwargs={"id":petition.id}))
|
||||
return render_to_response('petition/create.html', {'form':form}, context_instance = RequestContext(request))
|
||||
|
||||
@login_required
|
||||
def view(request, id):
|
||||
petition = models.Petition.objects.get(id__exact=id)
|
||||
commentForm = forms.CommentForm()
|
||||
return render_to_response('petition/view.html', {'petition':petition, 'form': commentForm}, context_instance = RequestContext(request))
|
||||
|
||||
def comment(request, id):
|
||||
if request.method == 'POST':
|
||||
form = forms.CommentForm(request.POST)
|
||||
else:
|
||||
form = forms.CommentForm()
|
||||
if form.is_valid():
|
||||
petition = models.Petition.objects.get(id__exact=id)
|
||||
comment = models.Comment()
|
||||
comment.author = request.user
|
||||
comment.body = form.cleaned_data['body']
|
||||
comment.petition = petition
|
||||
comment.save()
|
||||
return HttpResponseRedirect(reverse('petition.views.view', kwargs={"id":petition.id})+"#c"+str(comment.id))
|
@@ -131,7 +131,8 @@ INSTALLED_APPS = (
|
||||
'news',
|
||||
'django.contrib.comments',
|
||||
'django.contrib.flatpages',
|
||||
'minecraft'
|
||||
'minecraft',
|
||||
'petition'
|
||||
# Uncomment the next line to enable admin documentation:
|
||||
# 'django.contrib.admindocs',
|
||||
)
|
||||
|
@@ -65,6 +65,7 @@
|
||||
<ul>
|
||||
<li><a href="/">Home</a></li>
|
||||
<li><a href="{%url forums.views.index%}">Forums</a></li>
|
||||
<li><a href="{% url petition.views.create %}">Create Petition</a></li>
|
||||
<li><a href="http://{{minecraftHost}}/map/">Live Map</a></li>
|
||||
{% get_flatpages '/' as pages %}
|
||||
{% for page in pages %}
|
||||
|
18
templates/petition/create.html
Normal file
18
templates/petition/create.html
Normal file
@@ -0,0 +1,18 @@
|
||||
{% extends 'base.html' %}
|
||||
|
||||
{% block title %}New Petition{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<p>Creating a petition notifies the administration. Possible uses:</p>
|
||||
<ul>
|
||||
<li>Abusive player</li>
|
||||
<li>Server bug</li>
|
||||
<li>Feature request</li>
|
||||
</ul>
|
||||
<form method="POST" action="{% url petition.views.create %}">
|
||||
{% csrf_token %}
|
||||
{{form.as_p}}
|
||||
<input type="submit" value="Submit"/>
|
||||
</form>
|
||||
|
||||
{% endblock %}
|
20
templates/petition/view.html
Normal file
20
templates/petition/view.html
Normal file
@@ -0,0 +1,20 @@
|
||||
{% extends 'base.html' %}
|
||||
{% load markup %}
|
||||
|
||||
{% block title %}Petition #{{petition.id}}{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<p>Created by {{petition.author}}</p>
|
||||
{{petition.body|markdown:"safe"}}
|
||||
|
||||
{% for comment in petition.comment_set.all %}
|
||||
<p>By {{comment.author}}</p>
|
||||
{{comment.body|markdown:"safe"}}
|
||||
{% endfor %}
|
||||
|
||||
<form method="POST" action="{% url petition.views.comment id=petition.id %}">
|
||||
{% csrf_token %}
|
||||
{{form.as_p}}
|
||||
<input type="submit" value="Submit"/>
|
||||
</form>
|
||||
{% endblock %}
|
1
urls.py
1
urls.py
@@ -6,6 +6,7 @@ admin.autodiscover()
|
||||
|
||||
urlpatterns = patterns('',
|
||||
url(r'^$', 'news.views.index', name='home'),
|
||||
url(r'^petitions/', include('petition.urls')),
|
||||
url(r'^comments/', include('django.contrib.comments.urls')),
|
||||
url(r'^news/', include('news.urls')),
|
||||
url(r'^profiles/', include('profiles.urls')),
|
||||
|
Reference in New Issue
Block a user