def init_db(self): self.expired_token = ( 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.' 'eyJleHAiOjE1MjA2OTI5MzEsImlhdCI6MTUxODEwMDkzMSwic3ViIjoxfQ.' '4uuIsAIpxng-dcPQxXQvN3vjibxxeJpQFcaqBw0z4Eg') thing1 = User(name='thing1') thing2 = User(name='thing2') self.valid_token = User.encode_auth_token(1).decode('utf-8') db.session.add(thing1) db.session.add(thing2) db.session.commit()
def setUpTestData(cls): cls.test_users = [User(username='******'.format(i)) for i in range(3)] for user in cls.test_users: user.save() cls.chat = Chat(author_id=1, pk=1) cls.chat.save() Chat.objects.get(pk=1).users.add(*cls.test_users)
def record_to_user(sql_user: dict) -> User: user = User() user.pk = sql_user['id'] user.pikabu_id = sql_user['pikabu_id'] user.username = sql_user['username'] user.avatar_url = sql_user['avatar_url'] user.rating = sql_user['rating'] user.comments_count = sql_user['comments_count'] user.posts_count = sql_user['posts_count'] user.hot_posts_count = sql_user['hot_posts_count'] user.pluses_count = sql_user['pluses_count'] user.minuses_count = sql_user['minuses_count'] user.subscribers_count = sql_user['subscribers_count'] user.is_rating_ban = sql_user['is_rating_ban'] user.gender = sql_user['gender'] user.approved = sql_user['approved'] user.awards = sql_user['awards'] user.signup_timestamp = sql_user['signup_timestamp'] user.info = sql_user['info'] user.updating_period = sql_user['updating_period'] user.is_updated = sql_user['is_updated'] user.last_update_timestamp = sql_user['last_update_timestamp'] return user
def consumers_new(request): error = "" if request.method == 'POST': if is_cpf_valid(request.POST['username']): try: user = User(username=request.POST['username'], first_name=request.POST['first_name'], last_name=request.POST['last_name'], is_consumer=True) user.set_password(request.POST['password']) user.save() consumer = Consumer( credit=0, has_studentship=request.POST['has_studentship'], type=request.POST['type'], user=User.objects.get(username=request.POST['username'])) consumer.save() return redirect('consumers') except Exception as e: error = "CPF Já Cadastrado!" else: error = "CPF Invalido!" return render(request, 'application/consumers_new.html', {'error': error})
def cadastro_cliente(requisicao): if requisicao.POST: nome = requisicao.POST.get('nome') data_nasc = requisicao.POST.get('data_nasc') sexo = requisicao.POST.get('sexo') cpf = requisicao.POST.get('cpf') endereco = requisicao.POST.get('endereco') telefone = requisicao.POST.get('telefone') email = requisicao.POST.get('email') pass_user = requisicao.POST.get('pass_user') form = CadastroUsuarioForm(requisicao.POST) if form.is_valid(): email_existe = User.objects.filter(username=email).exists() cpf_existe = ClienteProfile.objects.filter(cpf=cpf).exists() if email_existe: messages.error(requisicao, 'E-mail já cadastrado.') if cpf_existe: messages.error(requisicao, 'CPF já cadastrado.') if not email_existe and not cpf_existe: user = User(username=email, password=pass_user, is_cliente=True) # criar usuário user_profile = ClienteProfile(nome=nome, data_nasc=data_nasc, sexo=sexo, cpf=cpf, endereco=endereco, telefone=telefone, email=email, user=user) user.set_password(pass_user) # criptografria da senha user.save() user_profile.save() return redirect('/cadastro/cliente/ok/') else: form = CadastroUsuarioForm() return render(requisicao, 'cadastro_cliente.html', context={'form': form}) # se for GET
def cadastro_clinica(requisicao): if requisicao.POST: nome = requisicao.POST.get('nome') cnpj = requisicao.POST.get('cnpj') cep = requisicao.POST.get('cep') endereco = requisicao.POST.get('endereco') cidade = requisicao.POST.get('cidade') telefone = requisicao.POST.get('telefone') email = requisicao.POST.get('email') pass_user = requisicao.POST.get('pass_user') form = CadastroClinicaForm(requisicao.POST) if form.is_valid(): email_existe = User.objects.filter(username=email).exists() cnpj_existe = ClinicaProfile.objects.filter(cnpj=cnpj).exists() if email_existe: messages.error(requisicao, 'E-mail já cadastrado.') if cnpj_existe: messages.error(requisicao, 'CNPJ já cadastrado.') if not email_existe and not cnpj_existe: user = User(username=email, password=pass_user, is_clinica=True) # criar usuário user_profile = ClinicaProfile(nome=nome, cnpj=cnpj, cep=cep, endereco=endereco, cidade=cidade, telefone=telefone, email=email, user=user) user.set_password(pass_user) # criptografia da senha user.save() user_profile.save() return redirect('/cadastro/clinica/ok/') else: form = CadastroClinicaForm() return render(requisicao, 'cadastro_clinica.html', context={'form': form}) # se for GET
def load_csv(path): """ Load CSV to populate database `User` with users """ with open(path, 'rt') as csvFile: reader = csv.reader(csvFile, delimiter=',', quotechar="\"") fields_name = next(reader) for r, row in enumerate(reader): user = User() for i, field in enumerate(row): print("{}{}".format("Currently processing ", fields_name[i])) print("{}{}".format("Currently processing ", field)) setattr(user, fields_name[i], field) user.save() user.set_password(user.password) user.save() print("{}{}{}".format("User ", user, " is saved.")) # Assign Users to a specific role if (r < 6000): # Users 1 - 6000 Patient.objects.create(username=user) elif (r >= 6000) and (r < 8000): # Users 6001 - 8000 Healthcare.objects.create(username=user, license=user.postalcode) elif (r >= 8000) and (r < 9059): # Users 8000 - 9059 Researcher.objects.create(username=user) else: # User 9060 Admin.objects.create(username=user)
def create(self, validated_data): user = User( username=validated_data['username'] ) user.set_password(validated_data['password']) user.save() return user
def user_join(request): if (request.method == "POST"): form = join_form(request.POST) if form.is_valid(): #save for to DB user = form.save() #encrypt the password user.set_password(user.password) #save encrypted password to db user.save() #create Core User object User(username=form.cleaned_data.get("username"), password=form.cleaned_data.get("password"), role=form.cleaned_data.get("role"), firstName=form.cleaned_data.get("first_name"), lastName=form.cleaned_data.get("last_name"), email=form.cleaned_data.get("email")).save() #redirects back to login #login() return redirect('/') else: #form is invalid print("Join form validation failed.") return render(request, 'core/join.html', {"join_form": join_form}) else: return render(request, 'core/join.html', {"join_form": join_form})
def test_unique_name(self): """ name must be unique """ u = User(name='Michael', email='*****@*****.**', password='******') self.assertRaises(IntegrityError, u.save)
def put(self, user_id): """ PUT handler """ edit_user = User(**api.payload) edit_user.uuid = user_id edit_user.save()
def register(): """Register""" if current_user.is_authenticated: return redirect(url_for("main.home")) categories = db.session.query(Category).all() form = RegistrationForm() if form.validate_on_submit(): hashed_password = generate_password_hash(form.password.data) user = User( user_name=form.user_name.data, full_name=form.full_name.data, email=form.email.data, password=hashed_password, phone=form.phone.data, ) db.session.add(user) db.session.flush() # add role for user user_role = db.session.query(Role).filter_by(role_name="user").first() user_role.users.append(user) db.session.commit() flash( _(f"Your account have been created. You are now able to log in."), "success", ) return redirect(url_for("users.login")) return render_template("user/register.html", title=_("Register"), form=form, categories=categories)
def search(request, *args, **kwargs): payload = json.loads(request.body.decode()) if 'steam' in payload: steamid = SteamID.from_url('https://steamcommunity.com/id/' + payload['steam']) if not steamid: data = [] else: data = User.objects.filter(username=str(steamid.as_64)) if not data: user = User() user.is_steam = True user.is_active = False user.username = str(steamid.as_64) user.save() populate(user) data = [user] elif 'steam64' in payload: data = User.objects.filter(username=str(payload['steam64'])) if not data: user = User() user.is_steam = True user.is_active = False user.username = str(payload['steam64']) populate(user) data = [user] elif 'local' in payload: data = User.objects.filter(is_steam=True, namespace__icontains=payload['local']) data = list(data) elif 'steam' in payload: user = User() user.is_steam = False user.is_active = False user.username = payload['steam'] user.save() data = [user] return render(request, 'components/globals/dropdown/wrapper.pug', {'data': data[:settings.PAGE_SIZE]})