from django.http import (Http404, HttpResponseRedirect, HttpResponseNotFound, HttpResponse) from django.core.paginator import Paginator, InvalidPage, EmptyPage from django.core.urlresolvers import reverse from django.conf import settings from django.contrib import messages from django.utils.functional import lazy from django.shortcuts import render_to_response from django.core.context_processors import csrf from django.views.decorators.csrf import csrf_protect from django.template import Context, RequestContext from django.contrib.auth.decorators import login_required from datetime import datetime, date, timedelta from django_couchdb_utils.auth.models import User from lepublicateur.app import RegisterForm, UserForm, ResetpwdForm, ChangepwdForm from lepublicateur.views.utils import sender, template_sender from lepublicateur.context_processors import * import sys, os, subprocess, socket, uuid from django.utils.translation import gettext_lazy as _ def _lookup_template(name): return 'lepublicateur/%s.html' % name def _device_template(name): if name: return '%s.html' % name else: return 'base.html' @csrf_protect def register_email(request): """Display the register page. """ device_template = _device_template(get_device()) d = {} d.update(csrf(request)) if 's' in request.POST: csrf_token = request.COOKIES['csrf_token'] if csrf_token == "": # Fall back to X-CSRFToken, to make things easier for AJAX request_csrf_token = request.META.get('HTTP_X_CSRFTOKEN', '') else: request_csrf_token = csrf_token if request_csrf_token != csrf_token: return HttpResponseRedirect('start') # Use correct entry type Document class entry_type = User # Select correct form for entry form_class = RegisterForm if request.method == 'POST': form = form_class(request.POST) if form.is_valid(): entry = entry_type(**form.cleaned_data) uuidd = unicode(uuid.uuid4()) entry['password'] = entry_type.gen_password(entry['password']) entry['uuid'] = uuidd entry['is_active'] = False # Save the entry to the DB entry.save() messages.info(request, _('Welcome !')) else: messages.error(request, _('Error, no register')) return HttpResponseRedirect('/register-account/%s%s/' % (get_device(), ".html")) else: form = RegisterForm() context = { 'form': form, 'device_template': device_template } return render_to_response(_lookup_template('register_email'), context, context_instance=RequestContext(request)) @csrf_protect def register_you(request, uuid): """Display the register page. """ # Use correct entry type Document class entry_type = User # Select correct form for entry form_class = UserForm if request.method == 'POST': try: param = {'key': uuid } except: raise Http404 try: entry = User.get_user_by_uuid(uuid) except IndexError: raise Http404 form = form_class(request.POST) if form.is_valid(): data = entry_type(**form.cleaned_data) entry['username'] = data['username'] entry['first_name'] = data['first_name'] entry['last_name'] = data['last_name'] entry['uuid'] = unicode(entry.uuid) password = entry_type.gen_password(data['password']) if password: entry['password'] = unicode(password) entry['is_active'] = True # Save the entry to the DB entry.save() entry.has_usable_password() messages.info(request, _('Welcome !')) else: messages.error(request, _('Error, no register')) return HttpResponseRedirect(reverse('log-in')) else: form = UserForm() context = { 'form': form, } return render_to_response(_lookup_template('register_you'), context, context_instance=RequestContext(request)) @csrf_protect def activate_account(request): """Display the register page. """ if request.method == 'POST': email = request.POST['email'] entry_type = User entry = entry_type.get_user_by_email(email) entry['is_active'] = True entry.save() title = _('Activate your account') context = { 'title': title } return render_to_response(_lookup_template('email/thanks_register'), context, context_instance=RequestContext(request)) @csrf_protect def register_confirm(request): """Active invitation. """ if request.method == 'POST': try: entry_id = request.POST['entry_id'] entry = User.get_user_by_uuid(entry_id) except IndexError: raise Http404 if entry: entry['is_active'] = True entry.save() messages.info(request, _('Register confirm is correct')) template_sender( entry, entry['email'], _(u'[Mariage] : Your account is activated'), 'lepublicateur/email/activate_register.txt', 'lepublicateur/email/activate_register.html') template_sender( entry, settings.DEFAULT_FROM_EMAIL, _(u'[Mariage] : New register'), 'lepublicateur/email/notifiation_admin_register.txt', 'lepublicateur/email/notifiation_admin_register.html') else: messages.error(request, _('Error, Register confirm not is correct')) # # Get beautiful template # plaintext = get_template('lepublicateur/email/activate_register.txt') # htmly = get_template('lepublicateur/email/activate_register.html') # # get current url # current_path = get_full_domaine(settings.DOMAINNAME) # uuidd = unicode(uuid.uuid4()) # context = Context({ 'email': entry['email'], 'username': entry['username'], # 'first_name': entry['first_name'], # 'last_name': entry['last_name'], 'current_path': current_path }) # subject = _(u'[Mariage] : Your account is activated') # to_email = entry['email'] # sender(subject, context, settings.DEFAULT_FROM_EMAIL, to_email, plaintext, htmly) # #messages.success(request, 'success test') return HttpResponseRedirect(reverse('list-users')) @login_required @csrf_protect def register_admin(request): """Display the register page. """ d = {} d.update(csrf(request)) if 's' in request.POST: csrf_token = request.COOKIES['csrf_token'] if csrf_token == "": # Fall back to X-CSRFToken, to make things easier for AJAX request_csrf_token = request.META.get('HTTP_X_CSRFTOKEN', '') else: request_csrf_token = csrf_token if request_csrf_token != csrf_token: return HttpResponseRedirect('start') # Use correct entry type Document class entry_type = User # Select correct form for entry form_class = UserForm if request.method == 'POST': form = form_class(request.POST) if form.is_valid(): entries = entry_type(**form.cleaned_data) uuidd = unicode(uuid.uuid4()) username = request.POST['username'] password = entry_type.gen_password(entries['password']) email = request.POST['email'] if password and uuidd and email: entries['username'] = unicode(username) entries['email'] = unicode(email) entries['uuid'] = unicode(uuidd) entries['password'] = unicode(password) entries['is_active'] = True # Save the entry to the DB entries.save() entries.has_usable_password() messages.info(request, _('Insert user is correct')) else: messages.error(request, _('Error, Insert user is not correct')) return HttpResponseRedirect(reverse('list-users')) else: # Add email for register register = RegisterForm() context = { 'register': register } return render_to_response(_lookup_template('register_admin'), context, context_instance=RequestContext(request)) @csrf_protect def reset_password(request): """Display the reset password page. """ d = {} d.update(csrf(request)) if 's' in request.POST: csrf_token = request.COOKIES['csrf_token'] if csrf_token == "": # Fall back to X-CSRFToken, to make things easier for AJAX request_csrf_token = request.META.get('HTTP_X_CSRFTOKEN', '') else: request_csrf_token = csrf_token if request_csrf_token != csrf_token: return HttpResponseRedirect('start') # Use correct entry type Document class entry_type = User # Select correct form for entry form_class = ResetpwdForm if request.method == 'POST': form = form_class(request.POST) if form.is_valid(): data = entry_type(**form.cleaned_data) try: entry = entry_type.get_user_by_email(data['email']) if entry != None: if entry['email'] == data['email']: template_sender( entry, settings.DEFAULT_FROM_EMAIL, _(u'[Mariage] : Reset password'), 'lepublicateur/email/reset_password.txt', 'lepublicateur/email/reset_password.html') messages.info(request, _('Reset password is correct')) else: messages.error(request, _('Error, Reset password is not correct')) else: messages.error(request, _('Error, Reset password is not correct')) except IndexError: raise Http404 return HttpResponseRedirect(reverse('log-in')) else: form = form_class() context = { 'form': form, } return render_to_response(_lookup_template('reset_password'), context, context_instance=RequestContext(request)) @csrf_protect def change_password(request, uuid): """Display the reset password page. """ d = {} d.update(csrf(request)) if 's' in request.POST: csrf_token = request.COOKIES['csrf_token'] if csrf_token == "": # Fall back to X-CSRFToken, to make things easier for AJAX request_csrf_token = request.META.get('HTTP_X_CSRFTOKEN', '') else: request_csrf_token = csrf_token if request_csrf_token != csrf_token: return HttpResponseRedirect('start') # Use correct entry type Document class entry_type = User # Select correct form for entry form_class = ChangepwdForm if request.method == 'POST': form = form_class(request.POST) if form.is_valid(): data = entry_type(**form.cleaned_data) try: entry = entry_type.get_user(request.POST['username']) except IndexError: raise Http404 if data['password'] == data['confirm_password']: password = entry_type.gen_password(data['password']) if password: entry['password'] = unicode(password) # Save the entry to the DB entry.save() entry.has_usable_password() messages.info(request, _('Change password is correct')) else: messages.error(request, _('Error, Change password is not correct')) return HttpResponseRedirect(reverse('log-in')) else: if request.method == 'GET': try: entry = entry_type.get_user_by_uuid(uuid) if entry: form = form_class() messages.info(request, _('Change password is correct')) else: messages.error(request, _('Error, Change password is not correct')) except IndexError: raise Http404 context = { 'entry': entry, 'form': form, } return render_to_response(_lookup_template('change_password'), context, context_instance=RequestContext(request))