def create_comment_feed(data: dict, id_feed: int, user: User) -> comments_models.CommentFeed: """ Method to create a new comment about feed :param data: contains data to comment :type param: dict :param user: user that comment feed :type user: User :param id_feed: feed id comment :type id: int :raise: ValueError :return: comment_models.CommentFeed """ if data.get("comment") is not None: accounts_validations.validate_length("Comment", data.get("comment"), 2, 255) else: raise ValueError(str(_("Comment is required"))) with transaction.atomic(): try: feed = comments_models.CommentFeed.objects.create( user=user, feed=feed_models.Feed.objects.get(id=id_feed), comment=data.get("comment")) except Exception as e: print(e) raise ValueError( str(_("An error occurred while saving the comment feed"))) return feed
def create_feed(data: dict, user: accounts_models.User) -> feed_models.Feed: """ Method to create a new post in feed :param data: contains data about post :type data: dict :param user: user that is creating post in feed :type user: accounts_models.User :raise: ValueError :return: feed_models.Feed """ if data.get("image") is None: raise ValueError(str(_("Image is required"))) if data.get("description") is not None: accounts_validations.validate_length("Description", data.get("description"), 0, 65000) else: raise ValueError(str(_("Description is required"))) with transaction.atomic(): try: feed = feed_models.Feed.objects.create( userRegisterer=user, image=accounts_services.updateImage(data.get("image")), description=data.get("description")) except Exception as e: print(e) raise ValueError(str(_("An error occurred while saving the feed"))) return feed
def update_feed(data: dict, user: accounts_models.User) -> feed_models.Feed: """ Method to updte feed by user :param data: data to update in feed :type data: dict :param user: user who created the feed and now updates it :type user: accounts_models.User :raise: ValueError :return: feed_models.Feed """ if data.get("id") is not None: feed_validations.validate_id(data.get("id")) else: raise ValueError(str(_("id feed is required"))) try: feed = feed_models.Feed.objects.get(id=data.get("id"), userRegisterer=user) except feed_models.Feed.DoesNotExist as e: raise ValueError(str( _("You dont have permission to update this feed"))) if data.get("image") is not None: feed.image = accounts_services.updateImage(data.get("image")) if data.get("description") is not None: accounts_validations.validate_length("Description", data.get("description"), 0, 65000) feed.description = data.get("description") feed.save() return feed
def createComment(data, id_homeless, user): comments = [] if data.get("comment") is not None: accounts_validations.validate_length("Comment", data.get("comment"), 2, 255) else: raise ValueError(str(_("Comment is required"))) comment: str = data.get("comment", None) try: commentResult = comments_models.Comment.objects.create( user=User.objects.get(id=user.id), homeless=accounts_models.HomelessProfile.objects.get( id=id_homeless), comment=comment) comments = comments_models.Comment.objects.filter( homeless__id=id_homeless) except Exception as e: print(e) raise ValueError(str(_("An error occurred while saving the comment"))) return comments
def login(data: dict) -> accounts_models.User: """ Get access user Raise exception if user or password are incorrect or user does not exist. :param data: username and password of user. :type: dict. :return: user. :raises: ValueError, PermissionDenied. """ username = data.get("username", None) password = data.get("password", None) if username is None or not username: raise ValueError(str(_("The username cannot be empty"))) else: accounts_validations.validate_length('Username', username, 3, 25) if password is None or not password: raise ValueError(str(_("The password cannot be empty"))) else: accounts_validations.validate_length('Password', password, 5, 25) try: # Obtain user from database if exist user = accounts_models.User.objects.get( Q(username=username) | Q(email=username.lower())) except accounts_models.User.DoesNotExist as e: print(e) raise ValueError(str(_("The username or password is incorrect"))) # Verify is user is active if not user.is_active: raise PermissionDenied( str(_("Account blocked, contact the administrators."))) # Verify if password match if not user.check_password(password): raise ValueError(str(_("The username or password is incorrect"))) user = authenticate(username=user.username, password=password) return user
def update_comment_feed(data: dict, user: User) -> comments_models.CommentFeed: """ Method to udate a comment feed by user :param data: new data in comment :type data: dict :param user: User who update comment :type user: Uer :raise: ValueError :return: comments_models.CommentFeed """ try: comment_feed = comments_models.CommentFeed.objects.get( user=user, id=data.get("id")) if data.get("comment") is not None: accounts_validations.validate_length("Comment", data.get("comment"), 2, 255) else: raise ValueError(str(_("Comment is required"))) comment_feed.comment = data.get("comment") comment_feed.save() except comments_models.CommentFeed.DoesNotExist as e: raise ValueError(str(_("Comment not exist"))) return comment_feed
def update_homeless_profile(data: dict, user: accounts_models.User) -> Profile: """ update info in homeless profile. :param data: data of homeless an id of homeles_profile :type: dict. :param user: user in system :type: accounts_models.User. :return: homeless profile. :raises: ValueError. """ if data.get("id") is None: raise ValueError(str(_("Id homeless is required"))) try: # Obtain profile from database if exist profile = HomelessProfile.objects.get(Q(id=data.get("id"))) if profile.userRegisterer != user: raise ValueError( str(_("User have not permission to edit this homeless"))) if data.get("firstName") is not None: accounts_validations.validate_length("First Name", data.get("firstName"), 3, 25) profile.firstName = data.get("firstName") if data.get("lastName") is not None: accounts_validations.validate_length("Last Name", data.get("lastName"), 3, 25) profile.lastName = data.get("lastName") if data.get("email") is not None: accounts_validations.validate_length("Email", data.get("email"), 5, 75) if profile.email != data.get("email"): accounts_validations.validate_email(data.get("email")) profile.email = data.get("email") if data.get("show_email") is not None: data["show_email"] = accounts_validations.validate_show_email( data.get("show_email")) profile.show_email = data.get("show_email") if data.get("dateOfBirth") is not None: profile.dateOfBirth = accounts_validations.validate_birth( data.get("dateOfBirth"), 4380) if data.get("occupation") is not None: accounts_validations.validate_length("Occupation", data.get("occupation"), 3, 25) profile.occupation = data.get("occupation") if data.get("city") is not None: accounts_validations.validate_length("city", data.get("city"), 3, 15) profile.city = data.get("city") if data.get("country") is not None: accounts_validations.validate_length("country", data.get("country"), 4, 25) profile.country = data.get("country") if data.get("aboutYou") is not None: accounts_validations.validate_length("About You", data.get("aboutYou"), 4, 100) profile.aboutYou = data.get("aboutYou") if data.get("location_detail") is not None: accounts_validations.validate_length("Loation detail", data.get("location_detail"), 3, 50) profile.location_detail = data.get("location_detail") if data.get("photo") is not None: profile.photo = updateImage(data.get("photo")) profile.save() if data.get("portfolio") is not None: portfolio = data.get("portfolio") for photo in portfolio: portfolio_user = portfolio_models.HomelessPortfolio.objects.create( homeless=profile, userRegisterer=user, image=updateImage(photo.get("photo"))) except Profile.DoesNotExist as e: raise ValueError(str(_("The homeless not have profile"))) return profile
def create_homeless_profile(data: dict, user: accounts_models.User) -> Profile: """ Get access user into. Raise exception if user or password are incorrect or user does not exist. :param data: data of homeless :type: dict. :param user: user in system :type: accounts_models.User. :return: homeless profile. :raises: ValueError. """ userRegisterer = accounts_models.User.objects.get(id=user.id) typeUser: str = 'homeless' if data.get("firstName") is not None: accounts_validations.validate_length("First Name", data.get("firstName"), 3, 25) else: raise ValueError(str(_("First Name field is required"))) if data.get("lastName") is not None: accounts_validations.validate_length("Last Name", data.get("lastName"), 3, 25) else: raise ValueError(str(_("Last Name field is required"))) if data.get("show_email") is not None: data["show_email"] = accounts_validations.validate_show_email( data.get("show_email")) else: raise ValueError(str(_("Show email field is required"))) if data.get("dateOfBirth") is not None: birth = accounts_validations.validate_birth(data.get("dateOfBirth"), 4380) else: raise ValueError(str(_("Date of birth field is required"))) if data.get("occupation") is not None: accounts_validations.validate_length("Occupation", data.get("occupation"), 3, 25) else: raise ValueError(str(_("Occupation field is required"))) if data.get("city") is not None: accounts_validations.validate_length("city", data.get("city"), 3, 15) else: raise ValueError(str(_("City field is required"))) if data.get("country") is not None: accounts_validations.validate_length("country", data.get("country"), 4, 25) else: raise ValueError(str(_("Country field is required"))) if data.get("aboutYou") is not None: accounts_validations.validate_length("About You", data.get("aboutYou"), 4, 100) else: raise ValueError(str(_("About you field is required"))) if data.get("location_detail") is not None: accounts_validations.validate_length("Loation detail", data.get("location_detail"), 3, 50) else: raise ValueError(str(_("Location detail field is required"))) try: profile: Profile = HomelessProfile.objects.create( userRegisterer=userRegisterer, firstName=data.get("firstName"), lastName=data.get("lastName"), typeUser=typeUser, show_email=data.get("show_email"), #Additional information personal occupation=data.get("occupation"), # phone = phone, # address = address, city=data.get("city"), country=data.get("country"), location_detail=data.get("location_detail"), dateOfBirth=birth, aboutYou=data.get("aboutYou"), ) except Exception as e: print(e) raise ValueError(str(_("An error occurred while saving the profile"))) if data.get("photo") is not None: #accounts_validations.validate_length('Photo',data.get("photo"),0,300) profile.photo = updateImage(data.get("photo")) if data.get("email") is not None: accounts_validations.validate_length("Email", data.get("email"), 3, 75) accounts_validations.validate_email(data.get("email")) profile.email = data.get("email") url = 'homeless-profile/' + str(profile.id) saveQrCode(url, str(profile.id)) profile.qr_code = 'media/qrCodes/' + str(profile.id) + '.png' profile.save() if data.get("portfolio") is not None: portfolio = data.get("portfolio") for photo in portfolio: portfolio_user = portfolio_models.HomelessPortfolio.objects.create( homeless=profile, userRegisterer=userRegisterer, image=updateImage(photo.get("photo"))) return profile
def change_profile(data: dict, user: accounts_models.User) -> Profile: """ Method to change profile name, lastname or profile. raises exception if profile does not exist or the data sent is empty :param data: user data. :type data: dict. :param user: user massone. :type user: Model User. :return: user :raises: ValueError """ profile_exists = accounts_validations.validate_user_profile(user) if profile_exists == False: raise ValueError(str(_("Profile not exist"))) # Edit user account if data.get("firstName") is not None: accounts_validations.validate_length("First Name", data.get("firstName"), 3, 25) user.first_name = data.get("firstName") if data.get("lastName") is not None: accounts_validations.validate_length("Last Name", data.get("lastName"), 3, 25) user.last_name = data.get("lastName") if data.get("email") is not None: accounts_validations.validate_length("Email", data.get("email"), 5, 75) user.email = data.get("email") user.save() # Edit profile profile = Profile.objects.get(user=user) # valitation of data profile if data.get("show_email") is not None: show_email = accounts_validations.validate_show_email( data.get("show_email")) profile.show_email = show_email if data.get("dateOfBirth") is not None: birth = accounts_validations.validate_birth(data.get("dateOfBirth"), 6570) profile.dateOfBirth = birth if data.get("occupation") is not None: accounts_validations.validate_length("Occupation", data.get("occupation"), 3, 25) profile.occupation = data.get("occupation") if data.get("city") is not None: accounts_validations.validate_length("City", data.get("city"), 3, 15) profile.city = data.get("city") if data.get("country") is not None: accounts_validations.validate_length("country", data.get("country"), 4, 25) profile.country = data.get("country") if data.get("aboutYou") is not None: accounts_validations.validate_length("About You", data.get("aboutYou"), 4, 100) profile.aboutYou = data.get("aboutYou") if data.get("photo") is not None: profile.photo = updateImage(data.get("photo")) profile.save() # logger.debug("profile has been changed correctly in account of %s" % user.username) return profile
def create_profile(data: dict, user: accounts_models.User) -> Profile: """ Get access user into. Raise exception if user or password are incorrect or user does not exist. :param data: user, photo, position, phone, address, city, state, country, zipcode :type: dict. :return: user. :raises: ValueError, PermissionDenied. """ # Validate profile dont exist profile_exists = accounts_validations.validate_user_profile(user) if profile_exists == True: raise ValueError(str(_("Profile already exist"))) # Edit user account if data.get("firstName") is not None: accounts_validations.validate_length("First Name", data.get("firstName"), 3, 25) user.first_name = data.get("firstName") if data.get("lastName") is not None: accounts_validations.validate_length("Last Name", data.get("lastName"), 3, 25) user.last_name = data.get("lastName") if data.get("email") is not None: accounts_validations.validate_length("Email", data.get("email"), 5, 75) user.email = data.get("email") user.save() # valitation of data profile if data.get("show_email") is not None: data["show_email"] = accounts_validations.validate_show_email( data.get("show_email")) else: raise ValueError(str(_("Show email field is required"))) if data.get("dateOfBirth") is not None: birth = accounts_validations.validate_birth(data.get("dateOfBirth"), 6570) else: raise ValueError(str(_("Data of birth field is required"))) if data.get("occupation") is not None: accounts_validations.validate_length("Occupation", data.get("occupation"), 3, 25) else: raise ValueError(str(_("Occupation field is required"))) if data.get("city") is not None: accounts_validations.validate_length("city", data.get("city"), 3, 15) else: raise ValueError(str(_("City field is required"))) if data.get("country") is not None: accounts_validations.validate_length("country", data.get("country"), 4, 25) else: raise ValueError(str(_("Country field is required"))) if data.get("aboutYou") is not None: accounts_validations.validate_length("About You", data.get("aboutYou"), 4, 100) else: raise ValueError(str(_("About you field is required"))) with transaction.atomic(): try: profile_registered = Profile.objects.create( user=user, show_email=data.get("show_email"), occupation=data.get("occupation"), city=data.get("city"), country=data.get("country"), dateOfBirth=birth, aboutYou=data.get("aboutYou")) except Exception as e: print(e) raise ValueError(str(_("An error occurred while saving the user"))) if data.get("photo") is not None: profile_registered.photo = updateImage(data.get("photo")) profile_registered.save() return profile_registered
def register_user(data: dict, user: accounts_models.User): """ Method to register user in massone :param data: information of user to register :type data: dict :param user: user admin :type user: Model User :return: user :raises: ValueError """ email = data.get('email') # validate email if email is not None: accounts_validations.validate_length("Email", data.get("email"), 5, 75) accounts_validations.validate_email(email) else: raise ValueError(str(_("Email field is required"))) # validate username if data.get('username') is not None: accounts_validations.validate_length('Username', data.get('username'), 3, 25) accounts_validations.validate_username(data.get('username')) else: raise ValueError(str(_("Username field is required"))) # validate password1 if data.get('password1') is not None: accounts_validations.validate_length('Password', data.get('password1'), 5, 25) else: raise ValueError(str(_("Password field is required"))) # validate password2 if data.get('password2') is not None: accounts_validations.validate_length('Password confirmation', data.get('password2'), 5, 25) else: raise ValueError(str(_("Password confirmation field is required"))) # validate first name if data.get('first_name') is not None: accounts_validations.validate_length('First Name', data.get('first_name'), 3, 25) else: raise ValueError(str(_("First name field is required"))) # validate last name if data.get('last_name') is not None: accounts_validations.validate_length('Last Name', data.get('last_name'), 3, 25) else: raise ValueError(str(_("Last name confirmation field is required"))) if data.get('password1') != data.get('password2'): raise ValueError( str( _("An error occurred while saving the user, Passwords do not match" ))) with transaction.atomic(): try: user_registered = accounts_models.User.objects.create( username=data.get('username').lower(), first_name=data.get('first_name'), last_name=data.get('last_name'), email=email, password=make_password(data.get('password1')), ) # if user_registered.email != "": # welcome_email.delay(user_registered.username, user_registered.email) except Exception as e: print(e) raise ValueError(str(_("An error occurred while saving the user"))) return user_registered