def update_user(self, context: Context, args) -> Tuple[dict, MassEnergizeAPIError]: try: user_id = args.get('id', None) email = args.get('email', None) if not self._has_access(context, user_id, email): return None, CustomMassenergizeError("permission_denied") if context.user_is_logged_in and ((context.user_id == user_id) or (context.user_is_admin())): users = UserProfile.objects.filter(id=user_id) if not users: return None, InvalidResourceError() profile_picture = args.pop("profile_picture", None) users.update(**args) user = users.first() if profile_picture: if profile_picture == "reset": user.profile_picture = None user.save() else: pic = Media() pic.name = f'{user.full_name} profpic' pic.file = profile_picture pic.media_type = 'image' pic.save() user.profile_picture = pic user.save() return user, None else: return None, CustomMassenergizeError('permission_denied') except Exception as e: capture_message(str(e), level="error") return None, CustomMassenergizeError(e)
def create_user(self, context: Context, args) -> Tuple[dict, MassEnergizeAPIError]: try: email = args.get('email', None) community = get_community_or_die(context, args) # allow home address to be passed in location = args.pop('location', '') profile_picture = args.pop("profile_picture", None) if not email: return None, CustomMassenergizeError( "email required for sign up") user = UserProfile.objects.filter(email=email).first() if not user: new_user: UserProfile = UserProfile.objects.create( full_name=args.get('full_name'), preferred_name=args.get('preferred_name', None), email=args.get('email'), is_vendor=args.get('is_vendor', False), accepts_terms_and_conditions=args.pop( 'accepts_terms_and_conditions', False), preferences={'color': args.get('color', '')}) if profile_picture: pic = Media() pic.name = f'{new_user.full_name} profpic' pic.file = profile_picture pic.media_type = 'image' pic.save() new_user.profile_picture = pic new_user.save() else: new_user: UserProfile = user # if user was imported but profile incomplete, updates user with info submitted in form if not new_user.accepts_terms_and_conditions: new_user.accepts_terms_and_conditions = args.pop( 'accepts_terms_and_conditions', False) is_vendor = args.get('is_vendor', False) preferences = {'color': args.get('color')} community_member_exists = CommunityMember.objects.filter( user=new_user, community=community).exists() if not community_member_exists: # add them as a member to community CommunityMember.objects.create(user=new_user, community=community) # create their first household household = RealEstateUnit.objects.create( name="Home", unit_type="residential", community=community, location=location) new_user.real_estate_units.add(household) res = {"user": new_user, "community": community} return res, None except Exception as e: capture_message(str(e), level="error") return None, CustomMassenergizeError(e)