def edit_user_view(request, id): try: u = User.objects.get(id = id) except User.DoesNotExist: raise Http404 if request.method == "POST": flag = False data = request.POST.copy() data['date_joined'] = datetime.date.today() data['last_login'] = datetime.date.today() if(len(data['password']) == 0): flag = True data['password'] = u.password form = UserForm(data, instance = u) if form.is_valid(): edit = form.save(commit = False) if not flag: u.set_password(data['password']) edit.save() messages.success(request, 'Se actualizó el usuario.') else: messages.error(request, "Ingresa todos los campos. %s"%(dict(form.errors.items()))) if request.method == "GET": form = UserForm(instance = u) context = {'form': form, 'user': u} return render_to_response('admin/user/edit.html', context, context_instance = RequestContext(request))
def login_page(request): form = UserForm(request.GET or None) if form.is_valid() and form.clean(): session_key = get_random_string(32, VALID_KEY_CHARS) while Session.objects.filter(sessionid=session_key): session_key = get_random_string(32, VALID_KEY_CHARS) name = request.GET['username'] if user.objects.filter(username = str(name))[0].type == 'admin': response = HttpResponseRedirect('../admin/home/') else: response = HttpResponseRedirect('../home/') response.set_cookie('sessionid', session_key) Session.objects.create(sessionid=session_key, username = name) return response return render_to_response('login/login.html', locals(), RequestContext(request))
def user(request): if request.method == 'POST': form = UserForm(request.POST) if form.is_valid(): userValue = form.cleaned_data print userValue['name'],userValue['status'] print form user=form.save() print user.name,user.status # userValue = form.cleaned_data # print userValue['name'] # print userValue # user=User(name=userValue['name'],email=userValue['email'],description=userValue['description']) # user.save() else: form = UserForm(initial={'name': 'input the name'}) return render_to_response('user.html', {'form': form},context_instance=RequestContext(request))
def add_user_view(request): uform = UserForm() if request.method == "POST": data = request.POST.copy() data['date_joined'] = datetime.date.today() data['last_login'] = datetime.date.today() uform = UserForm(data) if uform.is_valid(): user = uform.save(commit = False) user.set_password(data['password']) user.save() messages.success(request, 'Se creó el usuario.') return HttpResponseRedirect(reverse('list_user_view')) else: messages.error(request, dict(uform.errors.items())) context = {'form': uform} return render_to_response('admin/user/add.html', context, context_instance = RequestContext(request)) else: uform = UserForm() context = {'form': uform} return render_to_response('admin/user/add.html', context, context_instance = RequestContext(request))
def AdminModifyUserView(request): result="Error" if request.user.is_superuser: user_id = request.POST.get('user_id') try: user = User.objects.get(id=user_id) form = UserForm(request.POST) if form.is_valid(): user.username = form.cleaned_data['username'] user.set_password(form.cleaned_data['password'] or user.password) user.first_name = form.cleaned_data['nickname'] user.email = form.cleaned_data['email'] user.is_superuser = form.cleaned_data['is_superuser'] user.is_active = form.cleaned_data['is_active'] user.save() result="Done" except User.DoesNotExist: pass else: result="You are not superuser" return HttpResponse(result)
def userdetail(request, uid): #uid是用户的id user = User.objects.get(pk=uid) useraddr = user.useraddr_set.filter(isdefault=0).first() form = UserForm() if request.method == 'POST': form = UserForm(request.POST) print(request.POST) if form.is_valid(): print('-' * 80) file = request.FILES.get('photo') print(file) print('+' * 70) if file: path = os.path.join(settings.MEDIA_ROOT, file.name) # list = path.split('/') # print(path.split('/')) # path = '/'+ list[-3]+'/'+list[-2]+'/'+list[-1] print(path) print('+' * 70) # # 文件类型过滤 # ext = os.path.splitext(file.name) # print(ext) # if len(ext) < 1 or not ext[1] in settings.ALLOWED_FILEEXTS: # return redirect(reverse('admin:userdetail',kwargs={'uid':uid})) # 解决文件重名 # if os.path.exists(path): # # 日期目录 # dir = datetime.today().strftime("%Y/%m/%d") # dir = os.path.join(settings.MEDIA_ROOT, dir) # if not os.path.exists(dir): # os.makedirs(dir) # 递归创建目录 # # list.png # file_name = ext[0] + datetime.today().strftime("%Y%m%d%H%M%S") + str(randint(1, 1000)) + ext[1] if len(ext) > 1 else '' # path = os.path.join(dir, file_name) # print(path) with open(path, 'wb') as fp: # 如果文件超过2.5M,则分块读写 if file.multiple_chunks(): for block1 in file.chunks(): fp.write(block1) else: fp.write(file.read()) print(path) user.picture = '/' + path user.username = form.cleaned_data.get('username') password = form.cleaned_data.get('password') password = hashlib.sha1(password.encode('utf8')).hexdigest() user.password = password user.ugrade = form.cleaned_data.get('ugrade') user.email = form.cleaned_data.get('email') user.phone = form.cleaned_data.get('phone') user.save() useraddr.province = form.cleaned_data.get('province') # print(useraddr.pro) useraddr.city = form.cleaned_data.get('city') useraddr.district = form.cleaned_data.get('district') useraddr.address = form.cleaned_data.get('address') useraddr.save() user = User.objects.get(pk=uid) useraddr = user.useraddr_set.filter(isdefault=0).first() return render(request, 'admin/user_detail.html', context={ 'user': user, 'useraddr': useraddr }) return render(request, 'admin/user_detail.html', context={ 'user': user, 'useraddr': useraddr, 'form': form })