def insert_user(request): user_id = get_params(request, "user_id") password = get_params(request, "password") m = hashlib.md5() m.update(password) password = str(m.hexdigest()) email = get_params(request, "email") mobile = get_params(request, "mobile") name = get_params(request, "name") id_card = get_params(request, "id_card") age = get_params(request, "age") sex = get_params(request, "sex") exist_user = User.objects.filter(user_id=user_id) if len(exist_user): return JsonResponse({ "error_no": REQUEST_ERROR, "message": USER_EXISTS }) else: u = User(user_id=user_id, name=name, mobile=mobile, email=email, sex=sex, age=age, id_card=id_card) u.save() upf = UserProfile(user=u, password=password) upf.save() return JsonResponse({ "error_no": REQUEST_SUCCESS })
def test_student_edit_info_leak(self): c = Client() t_email, t_pass = signup_teacher_directly() c.login(email=t_email, password=t_pass) profile = UserProfile(user=User.objects.create_user("test")) profile.save() stu = Student(user=profile) stu.save() self.assertEqual( c.get(reverse("teacher_edit_student", kwargs={"pk": "9999"})).status_code, c.get(reverse("teacher_edit_student", kwargs={"pk": stu.pk})).status_code, )
def signup(self, request, user): acct = Account.objects.filter( name = self.cleaned_data['account_name'], verification = self.cleaned_data['account_verifification_key'], ) if len(acct) <> 0: user.first_name = self.cleaned_data['first_name'] user.last_name = self.cleaned_data['last_name'] user.save() up = UserProfile(user=user) up.save() # Associate existing account with new user #acct[0].holder = up #acct[0].save() return user
def handle(self, *args, **options): # create Ingredients Ingredient(ingredient_name="Hefe", price_per_unit=0.014, unit="g").save() Ingredient(ingredient_name="Wasser", price_per_unit=0.01, unit="l").save() Ingredient(ingredient_name="Eier", price_per_unit=0.17, unit="Stück").save() Ingredient(ingredient_name="Zucker", price_per_unit=0.00065, unit="g").save() Ingredient(ingredient_name="Salz", price_per_unit=0.014, unit="Prise").save() Ingredient(ingredient_name="Milch", price_per_unit=0.014, unit="1").save() butter = Ingredient(ingredient_name="Butter", price_per_unit=0.01, unit="g") butter.save() mehl = Ingredient(ingredient_name="Mehl", price_per_unit=0.49, unit="kg") mehl.save() # create SuperUser superuser = User.objects.create_superuser('superuser', '*****@*****.**', 'hallo123') # create NormalUsers user_georg = User.objects.create_user('Bäckerei ungebunden', password='******') user_georg.first_name = "Georg" user_georg.last_name = "Ungebunden" user_georg.save() # create Bakery-Profile UserProfile(user=user_georg, bakery_name="Bäckerei ungebunden", adress_street="Musterstraße", adress_street_number=10, adress_plz="0815", adress_city="Mosbach").save() # create Recipes kuchen = Recipe(rezept_bezeichnung="Kuchen", benutzer=user_georg) kuchen.save() mehlsack = Recipe(rezept_bezeichnung="Sackvoll Mehl", benutzer=superuser) mehlsack.save() # create RecipeIngredients RecipeList(recipe=kuchen, ingredient=butter, amount=300).save() RecipeList(recipe=kuchen, ingredient=mehl, amount=0.5).save() RecipeList(recipe=mehlsack, ingredient=mehl, amount=100).save() print("Inserted sample entries into the database.")
def create_remote_customer(self, user): try: profile = user.userprofile except: profile = UserProfile() profile.user = user profile.save() if not profile.remote_customer_id: data = {'email': user.email} customer = Customer() res = customer.create(data) if res['id']: user.userprofile.remote_customer_id = res['id'] user.userprofile.save() if user.userprofile.remote_customer_id: return user return False
def create_remote_costumer(self, user): try: profile = user.userprofile except: profile = UserProfile() profile.user = user profile.save() if not profile.remote_costumer_id: data = { 'email': user.email, 'name': user.first_name + user.last_name, } customer = Customer() res = customer.create(data) if res['id']: user.userprofile.remote_costumer_id = res['id'] user.userprofile.save() if user.userprofile.remote_costumer_id: print('yeahh') return user return False
def register(request): unidades = Empresa.objects.all().order_by('nome_fantasia') if request.method == 'POST': user_form = RegistroForm(request.POST) if user_form.is_valid(): User.objects.create_user( username=user_form.cleaned_data['username'], password=user_form.cleaned_data['password'], email=user_form.cleaned_data['email'], first_name=user_form.cleaned_data['first_name'], # last_name=user_form.cleaned_data['last_name'], is_active=False, ) id = request.POST['SelectUnidade'] empresa = get_object_or_404(Empresa, id=id) usuario = get_object_or_404( User, username=user_form.cleaned_data['username']) profile = UserProfile() profile.user = usuario profile.empresa = empresa profile.siape = int(request.POST['username']) profile.save() email = [] email_user = [] email.append(empresa.email_responsavel_sistema) email_user.append(usuario.email) ResponsavelUsuarioMail(usuario).send(email) RegistraUsuarioMail(usuario).send(email_user) return redirect('login_register_success') user_form = RegistroForm() context = {'user_form': user_form, 'unidades': unidades} return render(request, 'registration/login.html', context)
def my_data(request): user = User.objects.get(pk=request.user.pk) user_form = UserForm(instance=user) try: user_profile = UserProfile.objects.get(user=user) except: user_profile = UserProfile() user_profile.user = user user_profile.save() profile_form = UserProfileForm(instance=user_profile) if request.method == 'POST': user_form = UserForm(request.POST) profile_form = UserProfileForm(request.POST) if user_form.is_valid() and profile_form.is_valid(): user.first_name = user_form.cleaned_data['first_name'] user.last_name = user_form.cleaned_data['last_name'] user.save() user_profile.cpf = profile_form.cleaned_data['cpf'] user_profile.address = profile_form.cleaned_data['address'] user_profile.number = profile_form.cleaned_data['number'] user_profile.address2 = profile_form.cleaned_data['address2'] user_profile.city = profile_form.cleaned_data['city'] user_profile.district = profile_form.cleaned_data['district'] user_profile.state = profile_form.cleaned_data['state'] user_profile.country = profile_form.cleaned_data['country'] user_profile.zipcode = profile_form.cleaned_data['zipcode'] user_profile.phone = profile_form.cleaned_data['phone'] user_profile.remote_receiver_id = profile_form.cleaned_data[ 'remote_receiver_id'] user_profile.save() context = { 'user_form': user_form, 'profile_form': profile_form, 'user': user } return render(request, 'portal/my_data.html', context)
def edit_user(request): # This stuff is on pretty much every page, should really put it in it's own function context = RequestContext(request) context_dict = { 'pagetitle': 'Threatened Species Portal', 'pagemsg': 'Add New Animal', } dataset_list = AnimalDataset.objects.order_by('name') # shows user details uo = request.user context_dict['user']= uo up = UserProfile.objects.get(user=uo) # Grabs current dataset (if not available set it) if (request.session.get('cur_ds') is None): if (up.default_dataset is None): request.session['cur_ds'] = 1 else: request.session['cur_ds'] = up.default_dataset.pk # Prepare the context cur_ds = AnimalDataset.objects.get(pk=request.session.get('cur_ds')) context_dict['cur_ds'] = cur_ds context_dict['datasets'] = dataset_list context_dict['defaultds'] = up.default_dataset # Determine if user is admin, if not redirect to no_perms page if request.user.is_superuser and request.method == 'POST': # FROM VALIDATION id = request.POST['id'] user_username = request.POST['username'] user_first_name = request.POST['first_name'] user_last_name = request.POST['last_name'] user_email = request.POST['email'] user_organisation = request.POST['organisation'] user_phone = request.POST['phone'] user_password = request.POST['password'] user_confirm_password = request.POST['confirm_password'] user_perm = json.loads(request.POST['perm-field']) if request.POST.get('active', 0): user_active = True else: user_active = False if request.POST.get('administrator', 0): user_administrator = True else: user_administrator = False user_default = request.POST.get('default', 0) print "default ds id: " + str(user_default) ## validation if user_password != user_confirm_password: return HttpResponse("Passwords don't match!") # get the object for the default dataset so we can save it to user profile if user_default != 0: default_ds = AnimalDataset.objects.get(pk=user_default) user_password = make_password(user_password) ##if statement here if request.POST.get("new"): if request.POST['password']=="": return HttpResponse("Can not create a user without a password") ## add user to database u = User(username=user_username, first_name=user_first_name, last_name=user_last_name, email=user_email, password=user_password, is_active=user_active, is_superuser=user_administrator, is_staff=user_administrator) u.save() if request.POST.get('default_dataset', False): user_default_dataset = AnimalDataset.objects.get(id=request.POST['default_dataset']) up = UserProfile(user=u, organisation=user_organisation, phone=user_phone, default_dataset=user_default_dataset) else: user_default_dataset = AnimalDataset.objects.get(id=1) #TO DO - the edit user will not render if a default dataset is not picked - however we need to decide what it should be set to if they do not pick one up = UserProfile(user=u, organisation=user_organisation, phone=user_phone,default_dataset=user_default_dataset) up.save() print "save!" user = User.objects.get(username=user_username) user.email="" #Add user to groups in perm-field for p in user_perm: g = Group.objects.get(name=p) user.groups.add(g) user.save() context_dict['info_msg'] = "Successfully created user" else: uo = User.objects.get(id=id) up = UserProfile.objects.get(user=uo) uo.username = user_username uo.first_name = user_first_name uo.last_name = user_last_name uo.email = user_email uo.is_active = user_active uo.is_superuser = user_administrator uo.is_staff = user_administrator if request.POST["password"]!="": uo.password = user_password up.organisation = user_organisation up.phone = user_phone if request.POST.get('default_dataset'): dds = AnimalDataset.objects.get(id=request.POST['default_dataset']) up.default_dataset = dds up.save() #Clear user's perm groups old_perm = uo.groups.all() for p in old_perm: p.user_set.remove(uo) #Add user to groups in perm-field for p in user_perm: g = Group.objects.get(name=p) uo.groups.add(g) uo.save() context_dict['info_msg'] = "Successfully updated user" #if ends here # Display User List users_list = User.objects.order_by('username') context_dict['users'] = users_list return render_to_response("user_list.html", context_dict, context) else: return HttpResponse("Not superuser or POST method")