def user(request, key): user = UserProfile.get(key) form = UserForm() if request.method == 'POST': if form.validate(request.form): user.user_name = request.form['user_name'] user.first_name = request.form['first_name'] user.last_name = request.form['last_name'] user.email = request.form['email'] user.newsletter_type = int(request.form['newsletter_type']) manager_key = request.form['manager'] if manager_key: manager = Manager.get(manager_key) user.manager = manager try: if request.form['activated'] == 'on': user.activated = True except: user.activated = False try: if request.form['is_admin'] == 'on': user.is_admin = True except: user.is_admin = False if request.form['password']: user.set_password(request.form['password']) else: user.put() return redirect(url_for('admin/users')) manager_ = None try: if user.manager: manager_ = user.manager except Exception: pass managers_ = list(Manager.all()) if not manager_: managers = [('', '-')] else: managers = [('', manager_)] for manager in managers_: if manager_ is None or manager.key() != manager_.key(): managers.append((manager.key(), manager)) form = UserForm( initial={ 'user_name': user.user_name, 'activated': user.activated, 'is_admin': user.is_admin, 'email': user.email, 'first_name': user.first_name, 'last_name': user.last_name, 'newsletter_type': user.newsletter_type }) form.manager.choices = managers return render_to_response( 'admin/user.html', { 'user': user, 'form': form.as_widget() })
def add_user(request): form = UserForm(initial={'password':gen_password(6)}) if request.method == 'POST': if form.validate(request.form): user_name = request.form['user_name'].lower() new_user = create_new_user(user_name=user_name, password=request.form['password']) new_user.email=request.form['email'] new_user.first_name = request.form['first_name'] new_user.last_name = request.form['last_name'] manager_key = request.form['manager'] if manager_key: manager = Manager.get(manager_key) new_user.manager = manager try: new_user.newsletter_type = int(request.form['newsletter_type']) except: pass try: if request.form['activated'] == 'on': new_user.activated = True except: new_user.activated = False try: if request.form['is_admin'] == 'on': new_user.is_admin = True except: new_user.is_admin = False new_user.put() return redirect(url_for('admin/users')) return render_to_response('admin/add_user.html', {'form': form.as_widget()})
def edit_manager(request, key): manager = Manager.get(key) form = ManagerForm(manager) if request.method == 'POST': if request.form and form.validate(request.form): manager = form.save() manager.put() return redirect(url_for('admin/managers')) photo_form = ManagerPhoto() return render_to_response('admin/edit_manager.html', {'manager':manager, 'form':form.as_widget(), 'photo_form':photo_form.as_widget()})
def add_manager_photo(request): if request.method == 'POST': manager_key = request.values.get('manager_key') manager = Manager.get(manager_key) if manager is None: return redirect('/admin/managers') new_th_form = ManagerPhoto() if request.form and new_th_form.validate(request.form, request.files): if manager.photo: thumb = ThumbImage.get(manager.photo) thumb.delete() manager.photo = None thumb = new_th_form['img'] content_type = 'image/jpeg' thumb_img = ThumbImage() thumb_img.add_new_thumb(blob_img=thumb, thumb_size=(200, 200,), content_type=content_type) thumb_img.add_new_thumb(blob_img=thumb, thumb_size=(100, 100,), content_type=content_type) thumb_img.put() manager.photo = str(thumb_img.key()) manager.put() return redirect('/admin/edit_manager/%s' % manager.key()) return redirect('/admin/managers')
def delete_manager(request, key): manager = Manager.get(key) manager.delete() return redirect(url_for('admin/managers'))
def reg_request(request): form = RegistrationRequestForm() managers = Manager.all().filter("is_hidden =", False) if request.method == "POST": if form.validate(request.form): email = request.form["email"] user = UserProfile.all().filter("email =", email).count() if user: default_manager = request.form.get("manager") default_manager = Manager.get(default_manager).key() form.append_errors( u"Этот email уже используется. \ Возможно Вы уже зарегистрировались.", "email", ) return render_to_response( "auth5/reg_request.html", {"form": form.as_widget(), "managers": managers, "default_manager": default_manager}, ) reg_reqs = RegistrationRequest.all().filter("email =", email).count() if reg_reqs: default_manager = request.form.get("manager") default_manager = Manager.get(default_manager).key() form.append_errors( u"Этот email уже используется. \ Возможно Вы уже отправили заявку на регистрацию.", "email", ) return render_to_response( "auth5/reg_request.html", {"form": form.as_widget(), "managers": managers, "default_manager": default_manager}, ) def txn(): reg_request_obj = RegistrationRequest( first_name=request.form["first_name"], email=request.form["email"], telephone=request.form["telephone"], company=request.form["company"], password=crypto.gen_pwhash(request.form["password"]), manager=db.Key(request.form.get("manager")), ) reg_request_obj.put() taskqueue.add( url=url_for("auth5/send_request_to_manager", request_key=str(reg_request_obj.key())), transactional=True, ) db.run_in_transaction(txn) return render_to_response("auth5/thanks.html") default_manager = None if request.form.get("manager"): default_manager = request.form.get("manager") default_manager = Manager.get(default_manager).key() else: def_mangers = Manager.all().filter("is_hidden =", False).filter("is_default", True) if def_mangers.count(): default_manager = random.randrange(0, def_mangers.count()) default_manager = def_mangers[default_manager].key() else: if managers.count(): default_manager = random.randrange(0, managers.count()) default_manager = managers[default_manager].key() return render_to_response( "auth5/reg_request.html", {"form": form.as_widget(), "managers": managers, "default_manager": default_manager} )