Simple Ohlhafv challenge machine

This commit is contained in:
Aarni
2017-09-20 19:27:36 +03:00
parent f738e76726
commit 7a435bcbc6
6 changed files with 111 additions and 0 deletions
+11
View File
@@ -0,0 +1,11 @@
from django import forms
from django.utils.translation import ugettext_lazy as _
from webapp.models import OhlhafvChallenge
class OhlhafvForm(forms.ModelForm):
class Meta:
model = OhlhafvChallenge
fields = ['challenger', 'challenger_email', 'victim', 'victim_email', 'series', 'message']
+8
View File
@@ -0,0 +1,8 @@
import django_tables2 as tables
from django.utils.translation import ugettext as _
from webapp.models import OhlhafvChallenge
class OhlhafvTable(tables.Table):
class Meta:
model = OhlhafvChallenge
+21
View File
@@ -0,0 +1,21 @@
{% extends "base.html" %}
{% load bootstrap3 %}
{% load i18n %}
{% block content %}
<div>
<h3>{% trans "Ohlhafv" %}</h3>
<div id="input_form">
<form name="ohlhafvForm" action="/ohlhafv/submit/" method="post" class="form">{% csrf_token %}
{% bootstrap_form form %}
{% buttons %}
<button type="submit" class="btn btn-primary">
{% trans "Challenge" %}
</button>
{% endbuttons %}
</form>
</div>
</div>
{% endblock content %}
+20
View File
@@ -0,0 +1,20 @@
{% extends "base.html" %}
{% load static %}
{% load i18n %}
{% load django_tables2 %}
{% block content %}
<div>
<div>
<h2>{% trans "All challenges" %}</h2>
</div>
<div class="ohlhafv_count">
<span>{% trans "Total challenges:" %} {{ challenge_count }}</span>
</div>
{{ table|safe }}
</div>
{% endblock content %}
+8
View File
@@ -5,6 +5,9 @@ from webapp.views import admin_index
from webapp.views import login_view
from webapp.views import logout_view
from webapp.views import about_view
from webapp.views import ohlhafv_view
from webapp.views import ohlhafv_submit
from webapp.views import ohlhafv_list
urlpatterns = [
# main
@@ -17,4 +20,9 @@ urlpatterns = [
# git revision
url(r'^about', about_view),
#ohlhafv
url(r'^ohlhafv$', ohlhafv_view),
url(r'^ohlhafv/submit', ohlhafv_submit),
url(r'^ohlhafv/list', ohlhafv_list),
]
+43
View File
@@ -2,9 +2,13 @@ from django.shortcuts import render, redirect
from django.contrib.auth import login, logout, authenticate
from django.views.decorators.http import require_http_methods
from django.views.decorators.csrf import ensure_csrf_cookie
from django.http import HttpResponse, HttpResponseRedirect
from django.contrib.auth.decorators import permission_required
from django.conf import settings
import logging
from webapp.models import OhlhafvChallenge
from webapp.forms import OhlhafvForm
from webapp.tables import OhlhafvTable
@require_http_methods(["GET"])
@@ -48,3 +52,42 @@ def logout_view(request, *args, **kwargs):
@require_http_methods(["GET"])
def about_view(request, *args, **kwargs):
return render(request, "about.html", {})
@require_http_methods(["GET"])
def ohlhafv_view(request, *args, **kwargs):
form = OhlhafvForm()
return render(request, 'ohlhafv.html', {'form': form})
@ensure_csrf_cookie
@require_http_methods(["POST"])
def ohlhafv_submit(request, *args, **kwargs):
form = OhlhafvForm(request.POST)
if form.is_valid():
form.save()
#return HttpResponseRedirect('/list/')
else:
pass
#return render(request, 'error.html', {'error': form.errors})
return HttpResponseRedirect('/ohlhafv/list/')
@ensure_csrf_cookie
@require_http_methods(["GET"])
def ohlhafv_list(request, *args, **kwargs):
challenges = OhlhafvChallenge.objects.all()
table = OhlhafvTable(challenges,
request=request,
exclude=['id', 'challenger_email', 'victim_email'],
attrs={'class': 'table table-bordered table-hover'})
table.paginate(page=request.GET.get('page', 1), per_page=25)
table_html = table.as_html(request)
context = {
'table': table_html,
'challenge_count': len(challenges),
}
return render(request, 'ohlhafv_list.html', context)