def handle(self, *args, **options): ''' Process gyms and send emails ''' today = datetime.date.today() for gym in Gym.objects.all(): if int(options['verbosity']) >= 2: self.stdout.write("Processing gym '{}' ".format(gym)) user_list = [] trainer_list = [] user_list_no_activity = [] weeks = gym.config.weeks_inactive for profile in gym.userprofile_set.all(): user = profile.user if user.has_perm('gym.gym_trainer'): trainer_list.append(user) # Check appropriate permissions if is_any_gym_admin(user): continue # Check user preferences if not user.gymuserconfig.include_inactive: continue last_activity = get_user_last_activity(user) if not last_activity: user_list_no_activity.append({'user': user, 'last_activity': last_activity}) elif last_activity - today > datetime.timedelta(weeks=weeks): user_list.append({'user': user, 'last_activity': last_activity}) if user_list or user_list_no_activity: for trainer in trainer_list: # Profile might not have email if not trainer.email: continue # Check trainer preferences if not trainer.gymadminconfig.overview_inactive: continue translation.activate(trainer.userprofile.notification_language.short_name) subject = _('Reminder of inactive members') context = { 'weeks': weeks, 'user_list': user_list, 'user_list_no_activity': user_list_no_activity } message = render_to_string('gym/email_inactive_members.html', context) mail.send_mail(subject, message, EMAIL_FROM, [trainer.email], fail_silently=True)
def get_queryset(self): """ Return a list with the users, not really a queryset. """ out = {'admins': [], 'members': []} for u in Gym.objects.get_members( self.kwargs['pk']).select_related('usercache'): out['members'].append({ 'obj': u, 'last_log': u.usercache.last_activity }) # admins list for u in Gym.objects.get_admins(self.kwargs['pk']): out['admins'].append({ 'obj': u, 'perms': { 'manage_gym': u.has_perm('gym.manage_gym'), 'manage_gyms': u.has_perm('gym.manage_gyms'), 'gym_trainer': u.has_perm('gym.gym_trainer'), 'any_admin': is_any_gym_admin(u) } }) return out
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 get_queryset(self): ''' Return a list with the users, not really a queryset. ''' out = [] for u in User.objects.filter(userprofile__gym_id=self.kwargs['pk']): out.append({'obj': u, 'last_log': get_user_last_activity(u), 'perms': {'manage_gym': u.has_perm('gym.manage_gym'), 'manage_gyms': u.has_perm('gym.manage_gyms'), 'gym_trainer': u.has_perm('gym.gym_trainer'), 'any_admin': is_any_gym_admin(u)}}) return out
def get_queryset(self): ''' Return a list with the users, not really a queryset. ''' out = [] for u in User.objects.filter(userprofile__gym_id=self.kwargs['pk']): out.append({ 'obj': u, 'last_log': get_user_last_activity(u), 'perms': { 'manage_gym': u.has_perm('gym.manage_gym'), 'manage_gyms': u.has_perm('gym.manage_gyms'), 'gym_trainer': u.has_perm('gym.gym_trainer'), 'any_admin': is_any_gym_admin(u) } }) return out
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 get_queryset(self): ''' Return a list with the users, not really a queryset. ''' out = {'admins': [], 'members': []} for u in Gym.objects.get_members(self.kwargs['pk']).select_related('usercache'): out['members'].append({'obj': u, 'last_log': u.usercache.last_activity}) # admins list for u in Gym.objects.get_admins(self.kwargs['pk']): out['admins'].append({'obj': u, 'perms': {'manage_gym': u.has_perm('gym.manage_gym'), 'manage_gyms': u.has_perm('gym.manage_gyms'), 'gym_trainer': u.has_perm('gym.gym_trainer'), 'any_admin': is_any_gym_admin(u)} }) return out
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 get_queryset(self): ''' Return a list with the users, not really a queryset. ''' path = self.request.path status = path.split("/")[-1] self.user_status = status if status == 'active' or status == 'inactive' else '' out = {'admins': [], 'members': []} for u in Gym.objects.get_members(self.kwargs['pk'], self.user_status).select_related('usercache'): out['members'].append({'obj': u, 'last_log': u.usercache.last_activity}) # admins list for u in Gym.objects.get_admins(self.kwargs['pk']): out['admins'].append({'obj': u, 'perms': {'manage_gym': u.has_perm('gym.manage_gym'), 'manage_gyms': u.has_perm('gym.manage_gyms'), 'gym_trainer': u.has_perm('gym.gym_trainer'), 'any_admin': is_any_gym_admin(u)} }) return out
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()
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 handle(self, **options): """ Process gyms and send emails """ today = datetime.date.today() for gym in Gym.objects.all(): if int(options["verbosity"]) >= 2: self.stdout.write("* Processing gym '{}' ".format(gym)) user_list = [] trainer_list = [] user_list_no_activity = [] weeks = gym.config.weeks_inactive if not weeks: if int(options["verbosity"]) >= 2: self.stdout.write(" Reminders deactivatd, skipping") continue for profile in gym.userprofile_set.all(): user = profile.user # check if the account was deactivated (user can't login) if not user.is_active: continue # add to trainer list that will be notified if user.has_perm("gym.gym_trainer"): trainer_list.append(user) # Check appropriate permissions if is_any_gym_admin(user): continue # Check user preferences if not user.gymuserconfig.include_inactive: continue last_activity = user.usercache.last_activity if not last_activity: user_list_no_activity.append({"user": user, "last_activity": last_activity}) elif today - last_activity > datetime.timedelta(weeks=weeks): user_list.append({"user": user, "last_activity": last_activity}) if user_list or user_list_no_activity: for trainer in trainer_list: # Profile might not have email if not trainer.email: continue # Check trainer preferences if not trainer.gymadminconfig.overview_inactive: continue translation.activate(trainer.userprofile.notification_language.short_name) subject = _("Reminder of inactive members") context = {"weeks": weeks, "user_list": user_list, "user_list_no_activity": user_list_no_activity} message = render_to_string("gym/email_inactive_members.html", context) mail.send_mail( subject, message, settings.WGER_SETTINGS["EMAIL_FROM"], [trainer.email], fail_silently=True )
def handle(self, **options): ''' Process gyms and send emails ''' today = datetime.date.today() for gym in Gym.objects.all(): if int(options['verbosity']) >= 2: self.stdout.write("* Processing gym '{}' ".format(gym)) user_list = [] trainer_list = [] user_list_no_activity = [] weeks = gym.config.weeks_inactive if not weeks: if int(options['verbosity']) >= 2: self.stdout.write(" Reminders deactivatd, skipping") continue for profile in gym.userprofile_set.all(): user = profile.user # check if the account was deactivated (user can't login) if not user.is_active: continue # add to trainer list that will be notified if user.has_perm('gym.gym_trainer'): trainer_list.append(user) # Check appropriate permissions if is_any_gym_admin(user): continue # Check user preferences if not user.gymuserconfig.include_inactive: continue last_activity = user.usercache.last_activity if not last_activity: user_list_no_activity.append({ 'user': user, 'last_activity': last_activity }) elif today - last_activity > datetime.timedelta(weeks=weeks): user_list.append({ 'user': user, 'last_activity': last_activity }) if user_list or user_list_no_activity: for trainer in trainer_list: # Profile might not have email if not trainer.email: continue # Check trainer preferences if not trainer.gymadminconfig.overview_inactive: continue translation.activate( trainer.userprofile.notification_language.short_name) subject = _('Reminder of inactive members') context = { 'weeks': weeks, 'user_list': user_list, 'user_list_no_activity': user_list_no_activity } message = render_to_string( 'gym/email_inactive_members.html', context) mail.send_mail(subject, message, settings.WGER_SETTINGS['EMAIL_FROM'], [trainer.email], fail_silently=True)
def handle(self, *args, **options): ''' Process gyms and send emails ''' today = datetime.date.today() for gym in Gym.objects.all(): if int(options['verbosity']) >= 2: self.stdout.write("Processing gym '{}' ".format(gym)) user_list = [] trainer_list = [] user_list_no_activity = [] weeks = gym.config.weeks_inactive for profile in gym.userprofile_set.all(): user = profile.user if user.has_perm('gym.gym_trainer'): trainer_list.append(user) # Check appropriate permissions if is_any_gym_admin(user): continue # Check user preferences if not user.gymuserconfig.include_inactive: continue last_activity = get_user_last_activity(user) if not last_activity: user_list_no_activity.append({ 'user': user, 'last_activity': last_activity }) elif last_activity - today > datetime.timedelta(weeks=weeks): user_list.append({ 'user': user, 'last_activity': last_activity }) if user_list or user_list_no_activity: for trainer in trainer_list: # Profile might not have email if not trainer.email: continue # Check trainer preferences if not trainer.gymadminconfig.overview_inactive: continue translation.activate( trainer.userprofile.notification_language.short_name) subject = _('Reminder of inactive members') context = { 'weeks': weeks, 'user_list': user_list, 'user_list_no_activity': user_list_no_activity } message = render_to_string( 'gym/email_inactive_members.html', context) mail.send_mail(subject, message, EMAIL_FROM, [trainer.email], fail_silently=True)