def regist(req): if req.method == 'GET': return render(req, 'register.html') user = User() user.username = req.POST.get('username') user.passwd = crypt(req.POST.get('passwd')) user.token = newToken(user.username) user.photo = req.FILES['photo'] uploadFile = req.FILES.get('photo') saveFileName = newFileName(uploadFile.content_type) saveFilePath = os.path.join(settings.MEDIA_ROOT, saveFileName) with open(saveFilePath, 'wb') as f: for part in uploadFile.chunks(): f.write(part) f.flush() user.img = saveFilePath user.token = newToken(user.username) user.save() resp = redirect('/main/login') resp.set_cookie('token', user.token) return resp
def doRegister(request): username = request.jsondata["username"] password = request.jsondata["password"] if User.objects.filter(username=username).count() > 0: return JsonResponse({"success": False, "reason": "User already exists"}) user = User(username=username, password=password) user.save() request.session["username"] = username return JsonResponse({"success": True})
def register(): if current_user.is_authenticated: return redirect(url_for('index')) form = RegistrationForm() if form.validate_on_submit(): user = User(username=form.username.data, email=form.email.data) user.set_password(form.password.data) db.session.add(user) db.session.commit() flash(_('Congratulations, you are now a registered user!')) return redirect(url_for('login')) return render_template('register.html', title=_('Register'), form=form)
def get_context_data(self, **kwargs): managers_list = User.fetch() context = super(UpdateManagerView, self).get_context_data(**kwargs) context['operation'] = _("Update Manager") context['manager'] = str(self.object) context['managers'] = managers_list return context
def regist(): # {"phone": "", "code": ""} try: data = request.get_json() phone = data.get('phone') code = data.get('code') password = data.get('password') if sms_.validate_code(phone, code): userid = random.randint(100000, 999999) userimage = "https://hgcdn.handouzb.com/201945/2a62fcb98f3ba090759b1658077ab296.jpeg" username = "******" autograph = "一个伟大的签名正待产生" user = User(userid=userid, telphone=phone, password=encode4md5(password), userimage=userimage, username=username, autograph=autograph) db.session.add(user) db.session.commit() # 提交事务 except Exception as e: print(e) return jsonify({'status': 1, 'msg': '已注册过,请直接登录'}) return jsonify({'status': 0, 'msg': '注册成功'})
def registerValidate(request): username = request.form.get('Username') password = request.form.get('Password') email = request.form.get('Email') lastname = request.form.get('Lastname') firstname = request.form.get('Firstname') sex = request.form.get('sex') address = request.form.get('Address') isUsernameDuplicate = not not User.query.filter( User.username == username.strip()).first() isMailDuplicate = not not User.query.filter( User.email == email.strip()).first() if isUsernameDuplicate: return None, "User existed" if isMailDuplicate: return None, "Email existed" password = str(hashlib.md5(password.strip().encode('utf-8')).hexdigest()) user = User(username=username, password=password, email=email, firstname=firstname, lastname=lastname, address=address, sex=sex, role=2) return user, "Register successfully"
def register(req): if req.method == 'GET': return render(req, 'regist.html') user = User() user.userName = req.POST.get('username') user.userPasswd = crypt(req.POST.get('passwd')) user.nickName = req.POST.get('nickname') user.phone = req.POST.get('phone') user.token = newToken(user.userName) user.save() resp = redirect('/app/mine') resp.set_cookie('token', user.token) return resp
def create(self, validated_data): password = validated_data.pop('password') user = User(**validated_data) user.is_active = True user.set_password(password) user.save() #send email here return user
def upload(req): # 上传文件 uploadFile = req.FILES.get('photo') print(uploadFile.__dict__) # content_type user = User() saveFileName = str(uuid.uuid4()) + ".jpg" saveFilePath = os.path.join(settings.MEDIA_ROOT, saveFileName) with open(saveFilePath, 'wb') as f: for part in uploadFile.chunks(): f.write(part) f.flush() user.img = saveFilePath # user.token = newToken(user.userName) user.save() # resp = HttpResponseRedirect('/app/home') # resp.set_cookie('token', user.token) # return resp return JsonResponse({"path": saveFileName})
def delete_manager(request, pk): try: user = request.user # check for superuser if not user.is_superuser: return redirect('mainapp:new:home') manager = User.fetch(user_id=pk) if manager: manager.delete() return redirect('mainapp:new:list_manager') except: return redirect('mainapp:new:list_manager')
def delete_manager(request, manager_id): try: user = request.user # check for superuser if not user.is_superuser: return redirect('mainapp:add_manager') manager = User.fetch(user_id=manager_id) if manager: manager.delete() return redirect('mainapp:add_manager') except: return redirect('mainapp:add_manager')
def test_follow(self): u1 = User(username='******', email='*****@*****.**') u2 = User(username='******', email='*****@*****.**') db.session.add(u1) db.session.add(u2) db.session.commit() self.assertEqual(u1.followed.all(), []) self.assertEqual(u1.followers.all(), []) u1.follow(u2) db.session.commit() self.assertTrue(u1.is_following(u2)) self.assertEqual(u1.followed.count(), 1) self.assertEqual(u1.followed.first().username, 'susan') self.assertEqual(u2.followers.count(), 1) self.assertEqual(u2.followers.first().username, 'john') u1.unfollow(u2) db.session.commit() self.assertFalse(u1.is_following(u2)) self.assertEqual(u1.followed.count(), 0) self.assertEqual(u2.followers.count(), 0)
def compte(): form = RegistrationForm() if form.validate_on_submit(): hashed_password = bcrypt.generate_password_hash( form.password.data).decode('utf-8') user = User(nom=form.nom.data, prenom=form.prenom.data, email=form.email.data, password=hashed_password, niveau=form.niveau.data, specialite=form.specialite.data, role='etudiant') db.session.add(user) db.session.commit() flash('The account has been created!', 'success') return redirect(url_for('compte')) elif request.method == 'GET': users = User.query.order_by( User.niveau.desc(), User.specialite).filter(User.role != 'admin').all() return render_template('compte.html', form=form, users=users)
def get_context_data(self, **kwargs): managers_list = User.fetch() context = super(CreateManagerView, self).get_context_data(**kwargs) context['operation'] = _("Add Manager") context['managers'] = managers_list return context
def test_avatar(self): u = User(username='******', email='*****@*****.**') self.assertEqual(u.avatar(128), ('https://www.gravatar.com/avatar/' 'd4c74594d841139328695756648b6bd6' '?d=identicon&s=128'))
def test_password_hashing(self): u = User(username='******') u.set_password('cat') self.assertFalse(u.check_password('dog')) self.assertTrue(u.check_password('cat'))
def test_follow_posts(self): # create four users u1 = User(username='******', email='*****@*****.**') u2 = User(username='******', email='*****@*****.**') u3 = User(username='******', email='*****@*****.**') u4 = User(username='******', email='*****@*****.**') db.session.add_all([u1, u2, u3, u4]) # create four posts now = datetime.utcnow() p1 = Post(body="post from john", author=u1, timestamp=now + timedelta(seconds=1)) p2 = Post(body="post from susan", author=u2, timestamp=now + timedelta(seconds=4)) p3 = Post(body="post from mary", author=u3, timestamp=now + timedelta(seconds=3)) p4 = Post(body="post from david", author=u4, timestamp=now + timedelta(seconds=2)) db.session.add_all([p1, p2, p3, p4]) db.session.commit() # setup the followers u1.follow(u2) # john follows susan u1.follow(u4) # john follows david u2.follow(u3) # susan follows mary u3.follow(u4) # mary follows david db.session.commit() # check the followed posts of each user f1 = u1.followed_posts().all() f2 = u2.followed_posts().all() f3 = u3.followed_posts().all() f4 = u4.followed_posts().all() self.assertEqual(f1, [p2, p4, p1]) self.assertEqual(f2, [p2, p3]) self.assertEqual(f3, [p3, p4]) self.assertEqual(f4, [p4])
def regist(): # {"phone": "", "code": ""} data = request.get_json() status = data["flag"] if status == "注册": try: phone = data['phone'] query: Query = db.session.query(User).filter( User.phone_num == phone) if query.count() != 0: return jsonify({"status": "1", "msg": "该手机号已注册"}) code = data['code'] if sms_.validate_code(phone, code): user = User() user.phone_num = phone user.password = encode4md5(phone[-6:]) print(user.password) try: query = data['query'] user.invited_user_id = query except Exception as e: print(e) db.session.add(user) db.session.commit() # 提交事务 except Exception as e: print(e) return jsonify({'status': 1, 'msg': '注册失败'}) try: u = db.session.query(User).filter(User.phone_num == phone).first() u_id = u.id print(u_id) ud = UserDetail() ud.user_id = u_id ud.username = '' ud.portrait = '' ud.address = '' ud.risk_rank = '' ud.zip_code = '' ua = UserAccount() ua.user_id = u_id print(ua.user_id) ua.user_balance = 0 ua.pay_password = '' ua.pay_count = 0 db.session.add(ua) db.session.add(ud) db.session.commit() except Exception as e: print(e) return jsonify({'status': 4, 'msg': '用户基本信息创建失败'}) return jsonify({'status': 0, 'msg': '注册成功'}) if status == "忘记密码": try: data = request.get_json() phone = data['phone'] query: Query = db.session.query(User).filter( User.phone_num == phone) if query.count() == 0: return jsonify({'status': 1, 'msg': '该手机号未被注册'}) code = data['code'] if sms_.validate_code(phone, code): return jsonify({'status': 0, 'msg': '重置密码请求成功'}) except Exception as e: print(e) return jsonify({'status': 1, 'msg': '重置密码请求失败'})