def mutate(root, info, emails, permissions): user = info.context.user my_permissions = list( map(lambda perm: perm.code, user.permissions.all())) if 'invite:users' not in my_permissions: raise ValueError("You Don't Have Permission To Invite Users") for perm in permissions: if perm not in my_permissions: raise ValueError( "You Don't Have Permission To Give Other User Permission \"{}\"" .format(perm)) current_users = User.objects.filter(contest__id=user.contest.id, email__in=emails).count() if current_users > 0: raise ValueError("Some User Already Invited") permissions = Permission.objects.filter(code__in=permissions) new_users = [] for email in emails: signup_otc = "".join( [random.choice("0123456789") for _ in range(8)]) new_user = User(email=email, contest=user.contest, signup_otc=signup_otc) new_user.full_clean() new_user.save() new_user.permissions.add(*permissions) new_users += [new_user] return new_users
def mutate(_self, _info, contest, email): if ('start_time' not in contest) and ('finish_time' not in contest): contest['start_time'] = datetime.datetime.now() + \ datetime.timedelta(days=10) contest['finish_time'] = contest['start_time'] + \ datetime.timedelta(hours=5) if 'start_time' not in contest: contest['start_time'] = contest['finish_time'] - \ datetime.timedelta(hours=5) if 'finish_time' not in contest: contest['start_time'] = contest['start_time'] + \ datetime.timedelta(hours=5) new_contest = Contest(**contest) new_contest.full_clean() new_contest.save() new_contest.permitted_languages.add(*Language.objects.all()) signup_otc = "".join([random.choice("0123456789") for _ in range(8)]) new_user = User(email=email, contest=new_contest, signup_otc=signup_otc) new_user.full_clean() new_user.save() new_user.permissions.add(*Permission.objects.all()) return CreateContest(contest=new_contest, admin=new_user)
def invite_users(user: User, emails: List[str], permissions: List[str]) -> Iterable[User]: if not user.has_permission('invite:users'): raise ForbiddenActionError("You Don't Have Permission To Invite Users") my_permissions = user.permission_codes for perm in permissions: if perm not in my_permissions: raise ForbiddenActionError( "You Don't Have Permission To Give Permission \"{}\"".format(perm)) current_users = User.objects.filter( contest__id=user.contest.id, email__in=emails).count() if current_users > 0: raise UserAlreadyInvitedError("Some User Already Invited") permissions = Permission.objects.filter(code__in=permissions) new_users: List[User] = [] for email in emails: signup_otc = "".join([random.choice("0123456789") for _ in range(8)]) # use constant otc for now signup_otc = "00000000" new_user = User(email=email, contest=user.contest, signup_otc=signup_otc) new_user.full_clean() new_user.save() new_user.permissions.add(*permissions) new_users += [new_user] return new_users
def create_contest(email: str, name: str, short_id: str, short_description: str, description: str = 'Just another competitive programming competition', start_time: Optional[datetime.datetime] = None, finish_time: Optional[datetime.datetime] = None, freezed: bool = False, grading_size: int = 1) -> Tuple[Contest, User]: if start_time is None and finish_time is None: start_time = datetime.datetime.now() + datetime.timedelta(days=10) finish_time = start_time + datetime.timedelta(hours=5) elif finish_time is not None: start_time = finish_time - datetime.timedelta(hours=5) elif start_time is not None: finish_time = start_time + datetime.timedelta(hours=5) if Contest.objects.filter(short_id=short_id).count() > 0: raise ContestError('Contest ID Already Taken') new_contest = Contest(name=name, short_id=short_id, short_description=short_description, description=description, start_time=start_time, finish_time=finish_time, freezed=freezed, grading_size=grading_size) new_contest.full_clean() new_contest.save() languages = get_all_languages() new_contest.permitted_languages.add(*languages) signup_otc = "".join([random.choice("0123456789") for _ in range(8)]) # use constant otc for now signup_otc = "00000000" new_user = User(email=email, contest=new_contest, signup_otc=signup_otc) new_user.full_clean() new_user.save() permissions = get_all_permissions() new_user.permissions.add(*permissions) return new_contest, new_user