コード例 #1
0
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_email(email)
    else:
        email = ""
    # validate username
    if data.get('username') is not None:
        accounts_validations.validate_username(data.get('username'))
    # validate password
    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'),
                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)
                pass
        except Exception as e:
            raise ValueError(str(_("An error occurred while saving the user")))
    return user_registered
コード例 #2
0
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
コード例 #3
0
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
コード例 #4
0
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