コード例 #1
0
def delete_interest(*, account: Account, id: int) -> bool:
    p = Post.objects.filter(id=id).first()
    if not p:
        raise InvalidInputFormat("Post with id {} doesn't exist.".format(id))
        return { 'interested': False }

    user_account = get_user_account(account)
    if not p.interested_users.filter(account=account).exists():
        raise InvalidInputFormat("User with id {} hasn't interested post with id {} yet.".format(account.id, id))
    p.interested_users.remove(user_account)
    return { 'interested': False }
コード例 #2
0
def username_format_check(username: str, raise_exception=True) -> bool:
    if len(username) > 36 or len(username) < 6:
        if raise_exception:
            raise InvalidInputFormat(
                "Username must be 6 to 36 characters long.")
        return False
    match = re.fullmatch(r'[A-Za-z0-9.]+', username)
    if not match:
        if raise_exception:
            raise InvalidInputFormat("Username must be alphanumeric")
        return False
    return True
コード例 #3
0
def create_interest(*, account: Account, id: int) -> bool:
    p = Post.objects.filter(id=id).first()
    if not p:
        raise InvalidInputFormat("Post with id {} doesn't exist.".format(id))
        return {'interested': False}

    student_account = get_student_account(account)
    if p.interested_students.filter(account=account).exists():
        raise InvalidInputFormat(
            "User with id {} already interested post with id {}.".format(
                account.id, id))
    p.interested_students.add(student_account)
    return {'interested': True}
コード例 #4
0
def delete_follow(*, account: Account, id: int) -> dict:
    a = Account.objects.filter(id=id).first()
    if not a:
        raise InvalidInputFormat(
            "Account with id {} doesn't exist.".format(id))

    f = Follow.objects.filter(sender=account, receiver=a)
    if not f:
        raise InvalidInputFormat(
            "Account with id {} has not followed account with id {} yet.".
            format(account.id, id))
    f.delete()
    return {'followed': False}
コード例 #5
0
def delete_follow(*, account: Account, id: int) -> bool:
    c = Company.objects.filter(account_id=id).first()
    if not c:
        raise InvalidInputFormat(
            "Company with id {} doesn't exist.".format(id))
        return {'followed': False}

    student_account = get_student_account(account)
    if not c.followers.filter(account=account).exists():
        raise InvalidInputFormat(
            "User with id {} hasn't followed comp. with id {} yet.".format(
                account.id, id))
    c.followers.remove(student_account)
    return {'followed': False}
コード例 #6
0
def create_follow(*, account: Account, id: int) -> dict:
    a = Account.objects.filter(id=id).first()
    if not a:
        raise InvalidInputFormat(
            "Account with id {} doesn't exist.".format(id))

    if Follow.objects.filter(sender=account, receiver=a).exists():
        raise InvalidInputFormat(
            "Account with id {} already followed account with id {}.".format(
                account.id, id))
    new_follow = Follow(sender=account, receiver=a).save()
    create_notification(type='follow', account=account, receiver=a)

    return {'followed': True}
コード例 #7
0
def create_interest(*, account: Account, id: int) -> bool:
    p = Post.objects.filter(id=id).first()
    if not p:
        raise InvalidInputFormat("Post with id {} doesn't exist.".format(id))
        return { 'interested': False }

    user_account = get_user_account(account)
    if p.interested_users.filter(account=account).exists():
        raise InvalidInputFormat("User with id {} already interested post with id {}.".format(account.id, id))
    p.interested_users.add(user_account)
    create_notification(
        type='interest', account=account, post_job_id=id, receiver=p.user.account)

    return { 'interested': True }
コード例 #8
0
def create_follow(*, account: Account, id: int) -> bool:
    c = Company.objects.filter(account__id=id).first()
    if not c:
        raise InvalidInputFormat(
            "Company with id {} doesn't exist.".format(id))
        return {'followed': False}

    student_account = get_student_account(account)
    if c.followers.filter(account=account).exists():
        raise InvalidInputFormat(
            "User with id {} already followed comp. with id {}.".format(
                account.id, id))
    c.followers.add(student_account)
    return {'followed': True}
コード例 #9
0
ファイル: job.py プロジェクト: HKAB/summer-weeb-1920H_PTUDW
def job_exist(id: int, raise_exception=True) -> bool:
    j = Job.objects.filter(id=id).first()
    if j is None:
        if raise_exception:
            raise InvalidInputFormat("Job with id {} not found.".format(id))
        return False
    return True
コード例 #10
0
def post_exist(id: int, raise_exception=True) -> bool:
    p = Post.objects.filter(id=id).first()
    if p is None:
        if raise_exception:
            raise InvalidInputFormat("Post with id {} not found.".format(id))
        return False
    return True
コード例 #11
0
def password_format_check(password: str, raise_exception=True) -> bool:
    if len(password) > 36 or len(password) < 8:
        if raise_exception:
            raise InvalidInputFormat(
                "Password must be 8 to 36 characters long.")
        return False
    return True
コード例 #12
0
def change_password(*, account: Account, current_password: str,
                    new_password: str) -> Account:
    if account.password != hashed_password(current_password):
        raise InvalidInputFormat("Wrong current password.")
    password_format_check(new_password)
    account.password = hashed_password(new_password)
    account.save()
コード例 #13
0
def account_type_check(account_type: str, raise_exception=True) -> bool:
    if not account_type in ['student', 'company']:
        if raise_exception:
            raise InvalidInputFormat(
                "account_type should be 'student' or 'company'.")
        return False
    return True
コード例 #14
0
ファイル: phone.py プロジェクト: HKAB/summer-weeb-1920H_PTUDW
def phone_exist(account_id: int, phone: str, raise_exception=True) -> bool:
    p = Phone.objects.filter(account__id=account_id, phone=phone).first()
    if p is not None:
        if raise_exception:
            raise InvalidInputFormat("Phone number already exist: " + phone)
        return False
    return True
コード例 #15
0
def company_account_check(account: Account, raise_exception=True):
    if account.account_type != 'company':
        if raise_exception:
            raise InvalidInputFormat(
                'Account {} is not a company account.'.format(account.id))
        return False
    return True
コード例 #16
0
ファイル: user.py プロジェクト: ultoxtung/LinkedOutServer
def create_user(*, account: Account, firstname: str, lastname: str,
                dateofbirth: str, gender: str, **kwargs) -> User:
    user_account_check(account)
    if user_exist(account.id, raise_exception=False):
        raise InvalidInputFormat("Account {} already has a user.".format(
            account.id))
    profile_picture = None
    if gender.lower() == "male":
        profile_picture = 'profile/user_default_male.jpg'
    elif gender.lower() == "female":
        profile_picture = 'profile/user_default_female.jpg'
    elif gender.lower() == "other":
        profile_picture = 'profile/user_default_other.jpg'
    else:
        profile_picture = 'profile/user_default_other.jpg'
        print("Gender {} ???".format(gender))
    s = User(account=account,
             firstname=firstname,
             lastname=lastname,
             dateofbirth=dateofbirth,
             gender=gender,
             profile_picture=profile_picture,
             **kwargs)
    s.save()
    return s
コード例 #17
0
def count_follow(*, account: Account, id: int) -> dict:
    c = Company.objects.filter(account__id=id).first()
    if not c:
        raise InvalidInputFormat(
            "Company with id {} doesn't exist.".format(id))
        return {'count': 0}
    return {'count': c.followers.count()}
コード例 #18
0
def student_account_check(account: Account, raise_exception=True):
    if account.account_type != 'student':
        if raise_exception:
            raise InvalidInputFormat(
                'Account {} is not a student account.'.format(account.id))
        return False
    return True
コード例 #19
0
def student_exist(account_id: str, raise_exception=True) -> bool:
    s = Student.objects.filter(account__id=account_id).first()
    if s is None:
        if raise_exception:
            raise InvalidInputFormat(
                "Student with account id {} not found.".format(account_id))
        return False
    return True
コード例 #20
0
def count_interest(*, account: Account, id: int) -> dict:
    p = Post.objects.filter(id=id).first()
    if not p:
        raise InvalidInputFormat("Post with id {} doesn't exist.".format(id))
        return { 'count': 0 }
    return {
        'count': p.interested_users.count()
    }
コード例 #21
0
def delete_post(*, account: Account, id: int) -> list:
    student_account_check(account)
    author_check(account, id)
    p = Post.objects.filter(id=id).first()
    if p is None:
        raise InvalidInputFormat("Post with id {} not found".format(id))
    p.delete()
    return list_post(id=account.id)
コード例 #22
0
def author_check(account: Account, id: int) -> bool:
    p = Post.objects.filter(id=id).first()
    if p.student != get_student_account(account):
        raise InvalidInputFormat(
            'Account with id {} isn\'t author of post with id {}'.format(
                account.id, id))
        return False
    return True
コード例 #23
0
ファイル: job.py プロジェクト: HKAB/summer-weeb-1920H_PTUDW
def author_check(account: Account, id: int) -> bool:
    j = Job.objects.filter(id=id).first()
    if j.company != get_company_account(account):
        raise InvalidInputFormat(
            'Account with id {} isn\'t author of job with id {}'.format(
                account.id, id))
        return False
    return True
コード例 #24
0
ファイル: job.py プロジェクト: HKAB/summer-weeb-1920H_PTUDW
def delete_job(*, account: Account, id: int) -> list:
    company_account_check(account)
    author_check(account, id)
    j = Job.objects.filter(id=id).first()
    if j is None:
        raise InvalidInputFormat("Job entry not found!")
    j.delete()
    return list_job(id=account.id)
コード例 #25
0
def author_check(account: Account, id: int) -> bool:
    c = Comment.objects.filter(id=id).first()
    if c.user != get_user_account(account):
        raise InvalidInputFormat(
            'Account with id {} isn\'t author of comment with id {}'.format(
                account.id, id))
        return False
    return True
コード例 #26
0
def company_exist(account_id: str, raise_exception=True) -> bool:
    c = Company.objects.filter(account__id=account_id).first()
    if c is None:
        if raise_exception:
            raise InvalidInputFormat(
                "Company with account id {} not found.".format(account_id))
        return False
    return True
コード例 #27
0
def check_follow(*, account: Account, id: int) -> dict:
    a = Account.objects.filter(id=id).first()
    if not a:
        raise InvalidInputFormat(
            "Account with id {} doesn't exist.".format(id))
    if Follow.objects.filter(sender=account, receiver=a).exists():
        return {'followed': True}
    return {'followed': False}
コード例 #28
0
def delete_comment(*, account: Account, id: int) -> list:
    user_account_check(account)
    c = Comment.objects.filter(id=id).first()
    if c is None:
        raise InvalidInputFormat("Comment with id {} not found".format(id))
    author_check(account, id)
    post_id = c.post.id
    c.delete()
    return list_comment(id=post_id)
コード例 #29
0
def check_interest(*, account: Account, id: int) -> bool:
    p = Post.objects.filter(id=id).first()
    if not p:
        raise InvalidInputFormat("Post with id {} doesn't exist.".format(id))
        return { 'interested': False }
    else:
        if p.interested_users.filter(account=account).exists():
            return { 'interested': True }
        return { 'interested': False }
コード例 #30
0
ファイル: phone.py プロジェクト: HKAB/summer-weeb-1920H_PTUDW
def delete_phone(*, account: Account, phone: str) -> dict:
    phone_format_check(phone)
    p = Phone.objects.filter(account__id=account.id, phone=phone).first()
    if p is None:
        raise InvalidInputFormat("User has no phone number: " + phone)
    p.delete()
    return {
        "phones":
        [p.phone for p in Phone.objects.filter(account__id=account.id)]
    }