You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
234 lines
7.5 KiB
234 lines
7.5 KiB
# -*- coding: utf-8 -*-
|
|
from django.shortcuts import render_to_response
|
|
from django.http import HttpResponseRedirect, HttpResponse
|
|
from django.template import RequestContext
|
|
from django.core.context_processors import csrf
|
|
from django.contrib.auth.decorators import login_required
|
|
import random
|
|
#models and forms
|
|
from models import User
|
|
from forms import UserForm, UserCreationForm, ChangePasswordForm, EmailAnnouncementForm
|
|
#custom views
|
|
from functions.custom_views import objects_list
|
|
|
|
from hashlib import md5
|
|
|
|
|
|
@login_required
|
|
def profile(request):
|
|
args = {'change_password_form': ChangePasswordForm(),
|
|
'email_announcement_form': EmailAnnouncementForm()}
|
|
args.update(csrf(request))
|
|
return render_to_response('profile.html', args, context_instance=RequestContext(request))
|
|
|
|
|
|
|
|
def user_all(request):
|
|
"""
|
|
Return list of all users with pagination
|
|
"""
|
|
return objects_list(request, User, 'user_all.html')
|
|
|
|
def user_change(request, url):
|
|
"""
|
|
Return form of user and post it on the server.
|
|
If form is posted redirect on the page of all users.
|
|
"""
|
|
user = User.objects.safe_get(id=url)
|
|
# try get user by url if doesnt work by id
|
|
if user is None:
|
|
user = User.objects.safe_get(url=url)
|
|
#redirect to list of all users if cannot find user
|
|
if user is None:
|
|
return HttpResponseRedirect('/admin/accounts/all')
|
|
|
|
if request.POST:
|
|
form = UserForm(request.POST, instance=user)
|
|
if form.is_valid():
|
|
form.save()
|
|
return HttpResponseRedirect('/admin/accounts/all')
|
|
else:
|
|
form = UserForm(instance=user)
|
|
|
|
args = {}
|
|
args.update(csrf(request))
|
|
|
|
args['form'] = form
|
|
|
|
return render_to_response('user_change.html', args)
|
|
|
|
def create_admin(request):
|
|
if request.POST:
|
|
form = UserCreationForm(request.POST)
|
|
if form.is_valid():
|
|
user = form.save(commit=False)
|
|
user.is_admin = False
|
|
user.save()
|
|
return HttpResponseRedirect('/admin/accounts/all')
|
|
|
|
else:
|
|
form = UserCreationForm()
|
|
|
|
args = {}
|
|
args.update(csrf(request))
|
|
args['form'] = form
|
|
|
|
return render_to_response('create_admin.html', args)
|
|
|
|
def create_md5(request):
|
|
if request.POST:
|
|
form = UserCreationForm(request.POST)
|
|
if form.is_valid():
|
|
user = User()
|
|
user.email = request.POST['email']
|
|
user.first_name = request.POST['first_name']
|
|
user.last_name = request.POST['last_name']
|
|
user.password = md5(request.POST['password2']).hexdigest()
|
|
user.is_admin = True
|
|
user.save()
|
|
|
|
return HttpResponseRedirect('/admin/accounts/all')
|
|
|
|
else:
|
|
form = UserCreationForm()
|
|
|
|
args = {}
|
|
args.update(csrf(request))
|
|
args['form'] = form
|
|
|
|
return render_to_response('create_admin.html', args)
|
|
|
|
from django.core.mail import EmailMessage
|
|
|
|
|
|
def registration(request):
|
|
if request.POST:
|
|
form = UserCreationForm(request.POST)
|
|
if form.is_valid():
|
|
user = form.save()
|
|
email = EmailMessage('Subject', 'Body', to=['%s'%user.email])
|
|
email.send()
|
|
return HttpResponseRedirect('/admin/accounts/registration')
|
|
else:
|
|
form = UserCreationForm()
|
|
|
|
args = {}
|
|
args.update(csrf(request))
|
|
|
|
args['form'] = form
|
|
|
|
return render_to_response('registration.html', args)
|
|
|
|
def generatePassword():
|
|
"""
|
|
generate random password from 8 symbols
|
|
"""
|
|
SYMBOLS = [',', '.', '?', '!', '-', '+', '1', '2', '3', '4', '5', '6', '7', '8',
|
|
'9', '0', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
|
|
'm', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A',
|
|
'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
|
|
'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '#']
|
|
PASSWORD_LENGTH = 8
|
|
newPassword = []
|
|
for i in range(PASSWORD_LENGTH):
|
|
newPassword.append(SYMBOLS[random.randrange(0, len(SYMBOLS))])
|
|
return ''.join(newPassword)
|
|
|
|
from django.conf import settings
|
|
|
|
def reset_password_email(request):
|
|
"""
|
|
generate random password
|
|
set this password to user and send on email
|
|
"""
|
|
if request.GET:
|
|
user = User.objects.get(email=request.GET['email'])
|
|
new_pass = generatePassword()
|
|
user.email_user('Reset password', 'Your new password: "%s" '%new_pass, settings.DEFAULT_FROM_EMAIL, )
|
|
user.set_password(u'%s'%new_pass)
|
|
user.save()
|
|
return HttpResponse('success')
|
|
|
|
return HttpResponse('error')
|
|
|
|
@login_required
|
|
def change_password(request):
|
|
"""
|
|
Change current user password if new password is valid
|
|
"""
|
|
success = {'success': False}
|
|
if request.POST:
|
|
form = ChangePasswordForm(request.POST)
|
|
if form.is_valid():
|
|
user = request.user
|
|
if(user.check_password(form.cleaned_data.get('old_password'))):
|
|
user.set_password(form.cleaned_data.get('new_password'))
|
|
user.save()
|
|
success['success'] = True
|
|
success['message'] = _(u'Password has been changed')
|
|
return HttpResponse(json.dumps(success), content_type='application/json')
|
|
else:
|
|
errors = {'old_password': _(u'Invalid password')}
|
|
success.update(errors)
|
|
return HttpResponse(json.dumps(success), content_type='application/json')
|
|
else:
|
|
success.update(form.errors)
|
|
return HttpResponse(json.dumps(success), content_type='application/json')
|
|
else:
|
|
return HttpResponse(json.dumps(success), content_type='application/json')
|
|
#--------------------------------------------------------------
|
|
'''
|
|
from django.views.decorators.debug import sensitive_post_parameters
|
|
from django.views.decorators.cache import never_cache
|
|
from django.views.decorators.csrf import csrf_protect
|
|
from django.utils.http import base36_to_int, is_safe_url
|
|
from django.shortcuts import resolve_url
|
|
from django.conf import settings
|
|
from django.conf.global_settings import LOGIN_REDIRECT_URL
|
|
from django.contrib.auth.forms import AuthenticationForm
|
|
|
|
@sensitive_post_parameters()
|
|
@csrf_protect
|
|
@never_cache
|
|
def login(request, template_name='registration/login.html',
|
|
redirect_field_name=REDIRECT_FIELD_NAME,
|
|
authentication_form=AuthenticationForm,
|
|
current_app=None, extra_context=None):
|
|
"""
|
|
Displays the login form and handles the login action.
|
|
"""
|
|
redirect_to = request.REQUEST.get(redirect_field_name, '')
|
|
|
|
if request.method == "POST":
|
|
form = authentication_form(data=request.POST)
|
|
if form.is_valid():
|
|
|
|
# Ensure the user-originating redirection url is safe.
|
|
if not is_safe_url(url=redirect_to, host=request.get_host()):
|
|
redirect_to = resolve_url(settings.LOGIN_REDIRECT_URL)
|
|
|
|
# Okay, security check complete. Log the user in.
|
|
auth_login(request, form.get_user())
|
|
|
|
if request.session.test_cookie_worked():
|
|
request.session.delete_test_cookie()
|
|
|
|
return HttpResponseRedirect(redirect_to)
|
|
else:
|
|
form = authentication_form(request)
|
|
|
|
request.session.set_test_cookie()
|
|
|
|
current_site = get_current_site(request)
|
|
|
|
context = {
|
|
'form': form,
|
|
redirect_field_name: redirect_to,
|
|
'site': current_site,
|
|
'site_name': current_site.name,
|
|
}
|
|
if extra_context is not None:
|
|
context.update(extra_context)
|
|
return TemplateResponse(request, template_name, context,
|
|
current_app=current_app)
|
|
''' |