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.
186 lines
6.1 KiB
186 lines
6.1 KiB
# -*- coding: utf-8 -*-
|
|
|
|
import random
|
|
import json
|
|
from hashlib import md5
|
|
from django.shortcuts import render_to_response
|
|
from django.http import HttpResponseRedirect, HttpResponse
|
|
from django.core.context_processors import csrf
|
|
from django.contrib.auth.decorators import login_required
|
|
from django.utils.translation import ugettext as _
|
|
#models and forms
|
|
from models import User
|
|
from forms import UserForm, UserCreationForm, ChangePasswordForm, EmailAnnouncementForm, UserFilterForm
|
|
#custom views
|
|
from functions.admin_views import AdminView, AdminListView
|
|
from django.views.generic import UpdateView
|
|
|
|
class UserListView(AdminListView):
|
|
template_name = 'admin/accounts/user_list.html'
|
|
form_class = UserFilterForm
|
|
model = User
|
|
|
|
|
|
class EditUser(UpdateView):
|
|
model = User
|
|
form_class = UserForm
|
|
success_url = '/admin/accounts/all'
|
|
template_name = 'user_change.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.
|
|
"""
|
|
try:
|
|
user = User.objects.get(url=url)
|
|
except User.DoesNotExist:
|
|
try:
|
|
user = User.objects.get(id=url)
|
|
except User.DoesNotExist, User.MultipleObjectsReturned:
|
|
return HttpResponseRedirect('/admin/accounts/all')
|
|
|
|
except User.MultipleObjectsReturned:
|
|
return HttpResponseRedirect('/admin/accounts/all')
|
|
|
|
if request.POST:
|
|
# bug with saving staff users(set is_staff to False)
|
|
staff = user.is_staff
|
|
|
|
form = UserForm(request.POST, request.FILES, instance=user)
|
|
if form.is_valid():
|
|
|
|
user = form.save()
|
|
if staff:
|
|
#
|
|
user.is_staff = True
|
|
user.save()
|
|
return HttpResponseRedirect('/admin/accounts/all')
|
|
else:
|
|
if user.profile.city:
|
|
form.fields['city'].widget.attrs['data-init-text'] = user.profile.city.name
|
|
|
|
else:
|
|
profile = user.profile
|
|
data = {'country':profile.country_id, 'city': profile.city_id,
|
|
'title': profile.title, 'descriptions': profile.descriptions,
|
|
'keywords': profile.keywords, 'phone': profile.phone, 'web_page': profile.web_page,
|
|
'about': profile.about, 'skype':profile.skype,'facebook':profile.facebook, 'linkedin':profile.linkedin,
|
|
'twitter':profile.twitter, 'vk':profile.vk}
|
|
|
|
form = UserForm(instance=user,initial=data)
|
|
|
|
if user.profile.city:
|
|
form.fields['city'].widget.attrs['data-init-text'] = user.profile.city.name
|
|
|
|
context = {}
|
|
context.update(csrf(request))
|
|
|
|
context['form'] = form
|
|
context['object'] = user
|
|
|
|
return render_to_response('user_change.html', context)
|
|
|
|
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)
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
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'Пароль именен')
|
|
return HttpResponse(json.dumps(success), content_type='application/json')
|
|
else:
|
|
errors = {'errors': [_(u'Не правильный пароль')]}
|
|
success.update(errors)
|
|
return HttpResponse(json.dumps(success), content_type='application/json')
|
|
else:
|
|
errors = [err[0] for err in form.errors.values()]
|
|
errors = {'errors': errors}
|
|
success.update(errors)
|
|
return HttpResponse(json.dumps(success), content_type='application/json')
|
|
else:
|
|
return HttpResponse(json.dumps(success), content_type='application/json')
|
|
|