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 }
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
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}
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}
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}
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}
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 }
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}
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
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
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
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()
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
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
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
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
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()}
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
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
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() }
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)
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
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
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)
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
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
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}
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)
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 }
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)] }