def handle(self, *args, **options): ''' Process all users ''' for user in User.objects.all(): if is_any_gym_admin(user): try: user.gymadminconfig except ObjectDoesNotExist: config = GymAdminConfig() config.user = user config.gym = user.userprofile.gym config.save() else: if user.userprofile.gym_id: try: user.gymuserconfig except ObjectDoesNotExist: config = GymUserConfig() config.user = user config.gym = user.userprofile.gym config.save()
def form_valid(self, form): ''' Create the user, set the user permissions and gym ''' gym = Gym.objects.get(pk=self.kwargs['gym_pk']) password = password_generator() user = User.objects.create_user(form.cleaned_data['username'], form.cleaned_data['email'], password) user.first_name = form.cleaned_data['first_name'] user.last_name = form.cleaned_data['last_name'] form.instance = user # Update profile user.userprofile.gym = gym user.userprofile.save() # Set appropriate permission groups if 'user' in form.cleaned_data['role']: user.groups.add(Group.objects.get(name='gym_member')) if 'trainer' in form.cleaned_data['role']: user.groups.add(Group.objects.get(name='gym_trainer')) if 'admin' in form.cleaned_data['role']: user.groups.add(Group.objects.get(name='gym_manager')) if 'manager' in form.cleaned_data['role']: user.groups.add(Group.objects.get(name='general_gym_manager')) request = self.request request.session['gym.user'] = { 'user_pk': user.pk, 'password': password } # Create config if is_any_gym_admin(user): config = GymAdminConfig() else: config = GymUserConfig() config.user = user config.gym = gym config.save() form.send_email(request, user, gym) return super(GymAddUserView, self).form_valid(form)
def form_valid(self, form): ''' Create the user, set the user permissions and gym ''' gym = Gym.objects.get(pk=self.kwargs['gym_pk']) password = password_generator() user = User.objects.create_user(form.cleaned_data['username'], form.cleaned_data['email'], password) user.first_name = form.cleaned_data['first_name'] user.last_name = form.cleaned_data['last_name'] form.instance = user # Update profile user.userprofile.gym = gym user.userprofile.birthdate = form.cleaned_data['birthdate'] user.userprofile.save() # Set appropriate permission groups if 'user' in form.cleaned_data['role']: user.groups.add(Group.objects.get(name='gym_member')) if 'trainer' in form.cleaned_data['role']: user.groups.add(Group.objects.get(name='gym_trainer')) if 'admin' in form.cleaned_data['role']: user.groups.add(Group.objects.get(name='gym_manager')) if 'manager' in form.cleaned_data['role']: user.groups.add(Group.objects.get(name='general_gym_manager')) self.request.session['gym.user'] = {'user_pk': user.pk, 'password': password} # Create config if is_any_gym_admin(user): config = GymAdminConfig() else: config = GymUserConfig() config.user = user config.gym = gym config.save() return super(GymAddUserView, self).form_valid(form)
def handle(self, **options): """ Process all users """ for user in User.objects.all(): if is_any_gym_admin(user): try: user.gymadminconfig except ObjectDoesNotExist: config = GymAdminConfig() config.user = user config.gym = user.userprofile.gym config.save() else: if user.userprofile.gym_id: try: user.gymuserconfig except ObjectDoesNotExist: config = GymUserConfig() config.user = user config.gym = user.userprofile.gym config.save()