def registration(request): ''' A form to allow for registration of new users ''' template_data = {} template_data.update(csrf(request)) # Don't use captcha when registering through an app is_app = check_request_amazon(request) or check_request_android(request) FormClass = RegistrationFormNoCaptcha if is_app else RegistrationForm # Redirect regular users, in case they reached the registration page if request.user.is_authenticated() and not request.user.userprofile.is_temporary: return HttpResponseRedirect(reverse('core:dashboard')) if request.method == 'POST': form = FormClass(data=request.POST) # If the data is valid, log in and redirect if form.is_valid(): username = form.cleaned_data['username'] password = form.cleaned_data['password1'] email = form.cleaned_data['email'] user = Django_User.objects.create_user(username, email, password) user.save() # Pre-set some values of the user's profile language = Language.objects.get(short_name=translation.get_language()) user.userprofile.notification_language = language # Set default gym, if needed gym_config = GymConfig.objects.get(pk=1) if gym_config.default_gym: user.userprofile.gym = gym_config.default_gym # Create gym user configuration object config = GymUserConfig() config.gym = gym_config.default_gym config.user = user config.save() user.userprofile.save() user = authenticate(username=username, password=password) django_login(request, user) messages.success(request, _('You were successfully registered')) return HttpResponseRedirect(reverse('core:dashboard')) else: form = FormClass() template_data['form'] = form template_data['title'] = _('Register') template_data['form_fields'] = [i for i in form] template_data['form_action'] = reverse('core:user:registration') template_data['submit_text'] = _('Register') template_data['extend_template'] = 'base.html' return render(request, 'form.html', template_data)
def save(self, *args, **kwargs): ''' Perform additional tasks ''' if self.default_gym: # All users that have no gym set in the profile are edited UserProfile.objects.filter(gym=None).update(gym=self.default_gym) # All users in the gym must have a gym config for profile in UserProfile.objects.filter(gym=self.default_gym): user = profile.user if not is_any_gym_admin(user): try: user.gymuserconfig except GymUserConfig.DoesNotExist: config = GymUserConfig() config.gym = self.default_gym config.user = user config.save() logger.debug( 'Creating GymUserConfig for user {0}'.format( user.username)) return super(GymConfig, self).save(*args, **kwargs)
def registration(request): """ A form to allow for registration of new users """ # If global user registration is deactivated, redirect if not settings.WGER_SETTINGS['ALLOW_REGISTRATION']: return HttpResponseRedirect(reverse('software:features')) template_data = {} template_data.update(csrf(request)) # Don't show captcha if the global parameter is false FormClass = RegistrationForm if settings.WGER_SETTINGS['USE_RECAPTCHA'] \ else RegistrationFormNoCaptcha # Redirect regular users, in case they reached the registration page if request.user.is_authenticated and not request.user.userprofile.is_temporary: return HttpResponseRedirect(reverse('core:dashboard')) if request.method == 'POST': form = FormClass(data=request.POST) # If the data is valid, log in and redirect if form.is_valid(): username = form.cleaned_data['username'] password = form.cleaned_data['password1'] email = form.cleaned_data['email'] user = User.objects.create_user(username, email, password) user.save() # Pre-set some values of the user's profile language = Language.objects.get( short_name=translation.get_language()) user.userprofile.notification_language = language # Set default gym, if needed gym_config = GymConfig.objects.get(pk=1) if gym_config.default_gym: user.userprofile.gym = gym_config.default_gym # Create gym user configuration object config = GymUserConfig() config.gym = gym_config.default_gym config.user = user config.save() user.userprofile.save() user = authenticate(username=username, password=password) django_login(request, user) messages.success(request, _('You were successfully registered')) return HttpResponseRedirect(reverse('core:dashboard')) else: form = FormClass() template_data['form'] = form template_data['title'] = _('Register') return render(request, 'form.html', template_data)
def save(self, *args, **kwargs): """ Perform additional tasks """ if self.default_gym: # All users that have no gym set in the profile are edited UserProfile.objects.filter(gym=None).update(gym=self.default_gym) # All users in the gym must have a gym config for profile in UserProfile.objects.filter(gym=self.default_gym): user = profile.user if not is_any_gym_admin(user): try: user.gymuserconfig except GymUserConfig.DoesNotExist: config = GymUserConfig() config.gym = self.default_gym config.user = user config.save() logger.debug("Creating GymUserConfig for user {0}".format(user.username)) return super(GymConfig, self).save(*args, **kwargs)
# Even with the uuid part, usernames are not guaranteed to be unique, # in this case, just ignore and continue except IntegrityError as e: continue if gym_list: gym_id = random.choice(gym_list) user.userprofile.gym_id = gym_id user.userprofile.gender = '1' if gender == 'm' else 2 user.userprofile.age = random.randint(18, 45) user.userprofile.save() config = GymUserConfig() config.gym_id = gym_id config.user = user config.save() print(' - {0}, {1}'.format(name, surname)) # # Gym generator # if hasattr(args, 'number_gyms'): print("** Generating {0} gyms".format(args.number_gyms)) gym_list = [] names_part1 = [] names_part2 = []
user.save() # Even with the uuid part, usernames are not guaranteed to be unique, # in this case, just ignore and continue except IntegrityError as e: continue if gym_list: gym_id = random.choice(gym_list) user.userprofile.gym_id = gym_id user.userprofile.gender = '1' if gender == 'm' else 2 user.userprofile.save() config = GymUserConfig() config.gym_id = gym_id config.user = user config.save() print(' - {0}, {1}'.format(name, surname)) # # Gym generator # if hasattr(args, 'number_gyms'): print("** Generating {0} gyms".format(args.number_gyms)) gym_list = [] names_part1 = [] names_part2 = []
def create(self, request): request_data = JSONParser().parse(request) # get user profile user_profile = UserProfile.objects.get(user=request.user) # check if user is allowed to create users via API if user_profile.can_create_user: password = request_data.get('password', None) confirm_password = request_data.get('confirm_password', None) email = request_data.get('email', None) username = request_data.get('username', None) # check passwords match don't match return 400 if password != confirm_password: return Response( {'message': 'Passwords provided do not match!'}, status=status.HTTP_400_BAD_REQUEST) # pass request data through serializer user_serializer = UserCreateSerializer(data=request_data) # check if serializer is valid if user_serializer.is_valid(): # get creator ID by introspecting token creator = User.objects.get(pk=Token.objects.get( key=request.auth).user_id) data = user_serializer.data username, email, password = data.get('username'), data.get( 'email', None), data.get('password') # create a user object user = User.objects.create_user(username=username, email=email, password=password) user.save() # create user profile user.userprofile.creator = creator.username user.userprofile.token = request.auth.key # assign user a gym gym_config = GymConfig.objects.get(pk=1) if gym_config.default_gym: user.userprofile.gym = gym_config.default_gym # Create gym user configuration object config = GymUserConfig() config.gym = gym_config.default_gym config.user = user config.save() user.userprofile.save() return Response( { "message": "You have successfully created user: {}".format( username) }, status=status.HTTP_201_CREATED) # if serializer is invalid return Response(user_serializer.errors, status=status.HTTP_400_BAD_REQUEST) # if user is not allowed to create users via API return Response( {"message": "You are not authorised to create users via API"}, status=status.HTTP_403_FORBIDDEN)
def registration(request): """ A form to allow for registration of new users """ template_data = {} template_data.update(csrf(request)) # Don't use captcha when registering through an app is_app = check_request_amazon(request) or check_request_android(request) FormClass = RegistrationFormNoCaptcha if is_app else RegistrationForm # Don't show captcha if the global parameter is false if not settings.WGER_SETTINGS["USE_RECAPTCHA"]: FormClass = RegistrationFormNoCaptcha # Redirect regular users, in case they reached the registration page if request.user.is_authenticated() and not request.user.userprofile.is_temporary: return HttpResponseRedirect(reverse("core:dashboard")) if request.method == "POST": form = FormClass(data=request.POST) # If the data is valid, log in and redirect if form.is_valid(): username = form.cleaned_data["username"] password = form.cleaned_data["password1"] email = form.cleaned_data["email"] user = Django_User.objects.create_user(username, email, password) user.save() # Pre-set some values of the user's profile language = Language.objects.get(short_name=translation.get_language()) user.userprofile.notification_language = language # Set default gym, if needed gym_config = GymConfig.objects.get(pk=1) if gym_config.default_gym: user.userprofile.gym = gym_config.default_gym # Create gym user configuration object config = GymUserConfig() config.gym = gym_config.default_gym config.user = user config.save() user.userprofile.save() user = authenticate(username=username, password=password) django_login(request, user) messages.success(request, _("You were successfully registered")) return HttpResponseRedirect(reverse("core:dashboard")) else: form = FormClass() template_data["form"] = form template_data["title"] = _("Register") template_data["form_fields"] = [i for i in form] template_data["form_action"] = reverse("core:user:registration") template_data["submit_text"] = _("Register") template_data["extend_template"] = "base.html" return render(request, "form.html", template_data)