def upload_staff_photo(self, info, files): if self._user and self._profile: if self._profile.profile_photo: self._profile.profile_photo.delete() self._form = StaffPhotoForm(info, files, instance=self._profile) if self.validate_form(): self._profile = self._object return True else: return False else: return False
def edit_user(self, user_info): self._form = EditUserForm(user_info, instance=self._user) if self.validate_form(): self._user = self._object return True else: return False
def new_user(self, user_info, profile_info): if not user_info['password'] == user_info['confirm_password']: self.add_error('Passwords don\'t match.') return False self._form = NewUserForm(user_info) if self._form.is_valid(): user = self._form.save(commit=False) user.set_password(user_info['password']) user.save() self._user = user if self.scope == 'staff': profile_info['language'] = 'en' g = Group.objects.get(name='staff') g.user_set.add(self._user) if self.new_staff_profile(profile_info): return True return False elif self.scope == 'client': g = Group.objects.get(name='client') g.user_set.add(self._user) if self.new_client_profile(profile_info): em = EmailUtil(self._user, self._profile) user_info['name'] = self._user.get_full_name() if em.send_welcome_email(user_info, profile_info['language']): return True return False else: self.add_error('User scope not found.') return False else: self.add_form_errors() return False
def new_client_profile(self, info): info['user'] = self._user.id self._form = NewClientProfileForm(info) if self.validate_form(): self._profile = self._object return True else: return False
def new_staff_profile(self, info): info['user'] = self._user.id info['language'] = 'en' self._form = NewStaffProfileForm(info) if self.validate_form(): self._profile = self._object return True else: return False
def edit_profile(self, info, scope=None): if (scope if scope else self.scope) == 'staff': info['language'] = 'en' self._form = EditStaffProfileForm(info, instance=self._profile) if self.validate_form(): self._profile = self._object return True return False elif (scope if scope else self.scope) == 'client': info['language'] = info.get('language', self._profile.client.language) self._form = EditClientProfileForm(info, instance=self._profile) if self.validate_form(): self._profile = self._object return True return False else: self.add_error('User scope not found.') return False
class UserUtil(BaseUtil): def __init__(self, user, scope): super(UserUtil, self).__init__() self._util_name = 'User Util' self._profile = None self._user = user self.scope = scope self._message_info = { 'code': 1, 'obj': 'user', 'action': 'get' } self.load_profile() self.language = self.load_language() def load_language(self): if self._profile: return self._profile.language return 'en' # # # SETTER FUNCTIONS # # def new_user(self, user_info, profile_info): if not user_info['password'] == user_info['confirm_password']: self.add_error('Passwords don\'t match.') return False self._form = NewUserForm(user_info) if self._form.is_valid(): user = self._form.save(commit=False) user.set_password(user_info['password']) user.save() self._user = user if self.scope == 'staff': profile_info['language'] = 'en' g = Group.objects.get(name='staff') g.user_set.add(self._user) if self.new_staff_profile(profile_info): return True return False elif self.scope == 'client': g = Group.objects.get(name='client') g.user_set.add(self._user) if self.new_client_profile(profile_info): em = EmailUtil(self._user, self._profile) user_info['name'] = self._user.get_full_name() if em.send_welcome_email(user_info, profile_info['language']): return True return False else: self.add_error('User scope not found.') return False else: self.add_form_errors() return False def edit_user(self, user_info): self._form = EditUserForm(user_info, instance=self._user) if self.validate_form(): self._user = self._object return True else: return False def delete_user(self): if self._user and self._profile: self._user.is_active = False self._user.save() self._profile.is_deleted = True self._profile.save() else: return False def new_staff_profile(self, info): info['user'] = self._user.id info['language'] = 'en' self._form = NewStaffProfileForm(info) if self.validate_form(): self._profile = self._object return True else: return False def new_client_profile(self, info): info['user'] = self._user.id self._form = NewClientProfileForm(info) if self.validate_form(): self._profile = self._object return True else: return False def staff_edit_profile(self, info, scope=None): if (scope if scope else self.scope) == 'staff': info['language'] = 'en' self._form = EditStaffMemberForm(info, instance=self._profile) if self.validate_form(): self._profile = self._object return True return False elif (scope if scope else self.scope) == 'client': info['language'] = info.get('language', self._profile.client.language) self._form = StaffEditClientProfileForm(info, instance=self._profile) if self.validate_form(): self._profile = self._object return True return False else: self.add_error('User scope not found.') return False def edit_profile(self, info, scope=None): if (scope if scope else self.scope) == 'staff': info['language'] = 'en' self._form = EditStaffProfileForm(info, instance=self._profile) if self.validate_form(): self._profile = self._object return True return False elif (scope if scope else self.scope) == 'client': info['language'] = info.get('language', self._profile.client.language) self._form = EditClientProfileForm(info, instance=self._profile) if self.validate_form(): self._profile = self._object return True return False else: self.add_error('User scope not found.') return False # # # GETTER FUNCTIONS # # def get_user(self, pk=None, scope=None): if pk: try: self._user = User.objects.get(pk=pk) if (scope if scope else self.scope) == 'staff': self._profile = StaffProfile.objects.get(user=self._user) elif (scope if scope else self.scope) == 'client': self._profile = ClientProfile.objects.get(user=self._user) else: self.add_error('User scope not found.') return None return self._user except ObjectDoesNotExist: self.add_error(get_status_message(self._message_info, self.language)['message']) return None else: return self._user def get_user_profile(self, user=None): if user: try: if self.scope == 'staff': self._profile = StaffProfile.objects.get(user=user) elif self.scope == 'client': self._profile = ClientProfile.objects.get(user=user) else: self.add_error('User scope not found.') return None return self._profile except ObjectDoesNotExist: self.add_error(get_status_message(self._message_info, self.language)['message']) return None else: return self._profile def load_profile(self): try: if self.scope == 'staff': self._profile = StaffProfile.objects.get(user=self._user) elif self.scope == 'client': self._profile = ClientProfile.objects.get(user=self._user) else: self.add_error('User scope not found.') return None return self._profile except ObjectDoesNotExist: self.add_error(get_status_message(self._message_info, self.language)['message']) return None def get_client_users(self, client, count=False): if count: return ClientProfile.objects.filter(client=client).count() return self.return_list(ClientProfile.objects.filter(client=client).order_by('id')) def get_staff_members(self, count=False): if count: return StaffProfile.objects.all().count() return self.return_list(StaffProfile.objects.all().order_by('-id')) # # # UPDATE FUNCTIONS # # def upload_client_photo(self, info, files): if self._user and self._profile: if self._profile.profile_photo: self._profile.profile_photo.delete() self._form = ClientPhotoForm(info, files, instance=self._profile) if self.validate_form(): self._profile = self._object return True else: return False else: return False def upload_staff_photo(self, info, files): if self._user and self._profile: if self._profile.profile_photo: self._profile.profile_photo.delete() self._form = StaffPhotoForm(info, files, instance=self._profile) if self.validate_form(): self._profile = self._object return True else: return False else: return False def change_password(self, info): if not info['password'] == info['confirm_password']: self.add_error('Passwords don\'t match.') return False if self._user and self._profile: self._user.set_password(info['password']) self._user.save() return True else: return False def check_password(self, password): if self._user.check_password(password): return True self.add_error('Current password incorrect.') return False