From 032bc5acaa1c36acac7591d75553d47e01bc4528 Mon Sep 17 00:00:00 2001 From: Uwe Kamper Date: Wed, 23 Oct 2013 00:27:31 +0200 Subject: [PATCH] added form and view for changing gastro-PINs --- .gitignore | 2 ++ account/forms.py | 2 ++ cbmi/forms.py | 21 +++++++++++++ cbmi/local_settings.py.smile | 57 +++++++++++++++++++++++++++++++++++ cbmi/local_settings.py.uk | 57 +++++++++++++++++++++++++++++++++++ cbmi/settings.py | 5 +++ cbmi/templates/gastropin.html | 20 ++++++++++++ cbmi/urls.py | 3 +- cbmi/views.py | 24 +++++++++++++++ manage.py | 0 requirements.txt | 2 +- 11 files changed, 191 insertions(+), 2 deletions(-) create mode 100644 cbmi/forms.py create mode 100644 cbmi/local_settings.py.smile create mode 100644 cbmi/local_settings.py.uk create mode 100644 cbmi/templates/gastropin.html mode change 100644 => 100755 manage.py diff --git a/.gitignore b/.gitignore index efc1dfb..db472e5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ *.pyc .idea local_settings.py +build +*.sqlite3 diff --git a/account/forms.py b/account/forms.py index 1eec70f..1f840d2 100644 --- a/account/forms.py +++ b/account/forms.py @@ -4,3 +4,5 @@ from django import forms class LoginForm(forms.Form): username = forms.CharField(max_length=255) password = forms.CharField(max_length=255, widget=forms.PasswordInput) + + diff --git a/cbmi/forms.py b/cbmi/forms.py new file mode 100644 index 0000000..93e4003 --- /dev/null +++ b/cbmi/forms.py @@ -0,0 +1,21 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +import re + +from django import forms +from django.utils.translation import ugettext as _ + + +class GastroPinField(forms.CharField): + def validate(self, value): + """ + Check if the value is all numeric and 4 - 6 chars long. + """ + match = re.match(r'^\d{4,6}$', value) + if not match: + raise forms.ValidationError(_('PIN must be 4 to 6 digits.')) + + +class GastroPinForm(forms.Form): + gastropin = GastroPinField() \ No newline at end of file diff --git a/cbmi/local_settings.py.smile b/cbmi/local_settings.py.smile new file mode 100644 index 0000000..1ea4e09 --- /dev/null +++ b/cbmi/local_settings.py.smile @@ -0,0 +1,57 @@ +# Django settings for cbmi project. + +DEBUG = True +TEMPLATE_DEBUG = DEBUG + +ADMINS = ( + ('Brian Wiborg', 'baccenfutter@c-base.org') + # ('Your Name', 'your_email@example.com'), +) + +MANAGERS = ADMINS + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': 'cbmi.sqlite3', + 'USER': '', + 'PASSWORD': '', + 'HOST': '', + 'PORT': '', + } +} + +# Absolute filesystem path to the directory that will hold user-uploaded files. +# Example: "/home/media/media.lawrence.com/media/" +MEDIA_ROOT = '' + +# URL that handles the media served from MEDIA_ROOT. Make sure to use a +# trailing slash. +# Examples: "http://media.lawrence.com/media/", "http://example.com/media/" +MEDIA_URL = '' + +# Absolute path to the directory static files should be collected to. +# Don't put anything in this directory yourself; store your static files +# in apps' "static/" subdirectories and in STATICFILES_DIRS. +# Example: "/home/media/media.lawrence.com/static/" +STATIC_ROOT = '' + +# URL prefix for static files. +# Example: "http://media.lawrence.com/static/" +STATIC_URL = '/static/' + +# Additional locations of static files +STATICFILES_DIRS = ( + # Put strings here, like "/home/html/static" or "C:/www/django/static". + # Always use forward slashes, even on Windows. + # Don't forget to use absolute paths, not relative paths. + '/home/smile/projects/cbmi/src/static', +) + +TEMPLATE_DIRS = ( + # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates". + # Always use forward slashes, even on Windows. + # Don't forget to use absolute paths, not relative paths. +) + +AUTH_LDAP_START_TLS = False diff --git a/cbmi/local_settings.py.uk b/cbmi/local_settings.py.uk new file mode 100644 index 0000000..1ea4e09 --- /dev/null +++ b/cbmi/local_settings.py.uk @@ -0,0 +1,57 @@ +# Django settings for cbmi project. + +DEBUG = True +TEMPLATE_DEBUG = DEBUG + +ADMINS = ( + ('Brian Wiborg', 'baccenfutter@c-base.org') + # ('Your Name', 'your_email@example.com'), +) + +MANAGERS = ADMINS + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': 'cbmi.sqlite3', + 'USER': '', + 'PASSWORD': '', + 'HOST': '', + 'PORT': '', + } +} + +# Absolute filesystem path to the directory that will hold user-uploaded files. +# Example: "/home/media/media.lawrence.com/media/" +MEDIA_ROOT = '' + +# URL that handles the media served from MEDIA_ROOT. Make sure to use a +# trailing slash. +# Examples: "http://media.lawrence.com/media/", "http://example.com/media/" +MEDIA_URL = '' + +# Absolute path to the directory static files should be collected to. +# Don't put anything in this directory yourself; store your static files +# in apps' "static/" subdirectories and in STATICFILES_DIRS. +# Example: "/home/media/media.lawrence.com/static/" +STATIC_ROOT = '' + +# URL prefix for static files. +# Example: "http://media.lawrence.com/static/" +STATIC_URL = '/static/' + +# Additional locations of static files +STATICFILES_DIRS = ( + # Put strings here, like "/home/html/static" or "C:/www/django/static". + # Always use forward slashes, even on Windows. + # Don't forget to use absolute paths, not relative paths. + '/home/smile/projects/cbmi/src/static', +) + +TEMPLATE_DIRS = ( + # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates". + # Always use forward slashes, even on Windows. + # Don't forget to use absolute paths, not relative paths. +) + +AUTH_LDAP_START_TLS = False diff --git a/cbmi/settings.py b/cbmi/settings.py index 94a050a..f322292 100644 --- a/cbmi/settings.py +++ b/cbmi/settings.py @@ -199,3 +199,8 @@ LOGGING = { }, } } + +try: + from local_settings import * +except ImportError, e: + print 'Unable to load local_settings.py:', e diff --git a/cbmi/templates/gastropin.html b/cbmi/templates/gastropin.html new file mode 100644 index 0000000..b1480f1 --- /dev/null +++ b/cbmi/templates/gastropin.html @@ -0,0 +1,20 @@ +{% extends "base.html" %} + +{% block body %} +
+
+
+

Gastro-Pin

+ {% if message %} +
{{ message }}
+ {% endif %} +
+ {{ form.non_field_errors }} + {% csrf_token %} + {{ form.as_p }} + +
+
+
+
+{% endblock %} diff --git a/cbmi/urls.py b/cbmi/urls.py index b0f696d..72d5a43 100644 --- a/cbmi/urls.py +++ b/cbmi/urls.py @@ -11,5 +11,6 @@ urlpatterns = patterns('', url(r'account/', include('account.urls')), url(r'^groups/(?P[^/]+)/', 'cbmi.views.groups_list'), - url(r'^$', 'cbmi.views.landingpage') + url(r'^$', 'cbmi.views.landingpage'), + url(r'^gastropin/$', 'cbmi.views.gastropin', name='gastropin'), ) diff --git a/cbmi/views.py b/cbmi/views.py index 8d9300d..e9d306b 100644 --- a/cbmi/views.py +++ b/cbmi/views.py @@ -1,8 +1,12 @@ from django.shortcuts import render_to_response, get_object_or_404 from django.contrib.auth.decorators import login_required from django.contrib.auth.models import Group +from django.shortcuts import render +from django.utils.translation import ugettext as _ +from forms import GastroPinForm + def landingpage(request): is_ceymaster = is_admin = False if 'ceymaster' in [g.name for g in request.user.groups.all()]: @@ -25,3 +29,23 @@ def groups_list(request, group_name): if 'ldap_admins' in [g.name for g in request.user.groups.all()]: is_admin = True return render_to_response("group_list.html", locals()) + +@login_required +def gastropin(request): + if request.method == 'POST': + form = GastroPinForm(request.POST) + if form.is_valid(): + user = request.user + user_profile = user.get_profile() + user_profile.gastropin = form.cleaned_data['gastropin'] + user_profile.save() + return render(request, 'gastropin.html', + {'message': _('Your Gastro-PIN was changed. Thank you!'), + 'form:': form}) + else: + return render(request, 'gastropin.html', {'form:': form}) + + else: + form = GastroPinForm() + + return render(request, 'gastropin.html', {'form': form}) diff --git a/manage.py b/manage.py old mode 100644 new mode 100755 diff --git a/requirements.txt b/requirements.txt index 8063f0b..5a224e6 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,4 @@ Django==1.4.2 MySQL-python==1.2.4 -django-auth-ldap==1.1.4 + django-auth-ldap==1.1.4 django-json-rpc==0.6.1