Esempio n. 1
0
    def put(self, c_id: int, u_id: int):
        service: WordPressService = g.wordpress
        if service.is_logged_in:
            return throw_error('NEEDS_LOGIN')

        user: WPUser = g.user
        if not user.is_admin:
            return throw_error('PERMISSION_NEEDED')

        sub: Submission = Submission.query.\
            filter(Submission.user_id == u_id).\
            filter(Submission.challenge_id == c_id).\
            first()
        if sub.approved is None:
            return throw_error('NOT_EVALUATED')
        if not sub.approved:
            return throw_error(
                'ALREADY_EVALUATED',
                'You can\'t update an approved submission evaluation')
        args = evaluation_parser.parse_args()
        comment, approved = args['comment'], args['approved']

        challenge: Challenge = Challenge.query.filter(
            Challenge.id == c_id).first()
        c = service.get_challenge(challenge.wp_id, challenge.type)
        if approved:
            service.award_user(u_id, c, args['points'])

        sub.evaluation_note = comment
        sub.approved = approved
        db.session.commit()

        return jsonify(sub.json)
    def delete(self, id: int):
        """
        DELETE challenge from database and Wordpress
        """
        if not g.wordpress.is_logged_in:
            return throw_error('NEEDS_LOGIN')

        user: WPUser = g.user
        if not user.is_admin:
            return throw_error('PERMISSION_NEEDED')

        challenge = Challenge.query.filter_by(id=id).first()
        if challenge is None:
            return throw_error('NOT_FOUND_ID')

        service: WordPressService = g.wordpress
        try:
            service.delete_challenge(challenge.wp_id, challenge.type)
        except NotFoundError:
            pass

        db.session.delete(challenge)
        db.session.commit()
        return jsonify({
            'message':
            'Deleted challenge from https://www.usmgames.cl/ and from server'
        })
    def post(self):
        """
        POST Create a challenge
        """
        args = challenge_parser.parse_args()
        instance = Challenge(type=args['type'],
                             title=args['title'],
                             description=args['description'])

        for requirement in args['requirements']:
            r = ChallengeRequirement(description=requirement)
            instance.requirements.append(r)

        service: WordPressService = g.wordpress
        if not service.is_logged_in:
            return throw_error('NEEDS_LOGIN')
        wp_challenge = WPChallenge.from_challenge(instance)

        try:
            wp_c = service.publish_challenge(wp_challenge,
                                             status=args['status'])
            if args['notify']:
                discord = DiscordWebhookService()
                discord.post_challenge(instance)
            instance.wp_id = wp_c.id
        except ForbiddenError:
            return throw_error('PERMISSION_NEEDED')
        except ServiceError:
            return throw_error('WORDPRESS_ERROR')

        db.session.add(instance)
        db.session.commit()

        res = jsonify(instance.json)
        return res
Esempio n. 4
0
    def post(self):
        service: WordPressService = g.wordpress
        if not service.is_logged_in:
            return throw_error('NEEDS_LOGIN')
        user: WPUser = g.user
        if not user.is_admin:
            return throw_error('PERMISSION_NEEDED')
        args = register_parser.parse_args()

        email = args['email']
        display_name = args['display_name']
        is_admin = args['admin']

        users = service.get_users(context='edit',
                                  search=generate_username(display_name),
                                  fields=['username'])
        if email in [user.email for user in users]:
            return throw_error('EMAIL_IN_USE')
        username = generate_username(display_name,
                                     [user.username for user in users])
        password = generate_password()

        user_data = WPUser(email, username, display_name)
        new_user = service.create_user(user_data, password, is_admin=is_admin)
        mail: EmailService = g.mail
        if mail.is_valid:
            with open('./usmgpm/services/assets/email.html', 'r') as f:
                content = ''.join(f.readlines()) % (
                    user_data.display_name, user_data.username, password)
            mail.send(email, '[USM Games] Bienvenido a usmgames.cl!', content)

        return jsonify(new_user.json)
Esempio n. 5
0
    def get(self, u_id: int):
        service: WordPressService = g.wordpress
        if not service.is_logged_in:
            return throw_error('NEEDS_LOGIN')

        user: WPUser = g.user
        if user.id != u_id and not user.is_admin:
            return throw_error('PERMISSION_NEEDED')

        subs_q = Submission.query.filter(Submission.user_id == u_id)
        args = get_submission_parser.parse_args()
        if args['not_evaluated']:
            subs_q = subs_q.filter(Submission.approved.is_(None))
        subs: list = subs_q.order_by(Submission.submitted.desc()).all()

        return jsonify(list(map(lambda x: x.json, subs)))
Esempio n. 6
0
 def decorated_function(*args, **kwargs):
     auth: str = request.headers.get('Authorization')
     service = WordPressService()
     if auth:
         splitted = auth.split(' ')
         if len(splitted) != 2:
             return throw_error('INVALID_HEADER',
                                'Invalid Authorization header')
         service.set_token(splitted[1], splitted[0])
         try:
             g.user = service.me()
         except ForbiddenError:
             return throw_error('INVALID_TOKEN')
     g.wordpress = service
     g.mail = EmailService()
     return f(*args, **kwargs)
Esempio n. 7
0
    def get(self, c_id: int, u_id: int):
        service: WordPressService = g.wordpress
        if not service.is_logged_in:
            return throw_error('NEEDS_LOGIN')

        user: WPUser = g.user
        if user.id != u_id and not user.is_admin:
            return throw_error('PERMISSION_NEEDED')

        sub: Submission = Submission.query.\
            filter(Submission.user_id == u_id).\
            filter(Submission.challenge_id == c_id).\
            first()
        if sub is None:
            return throw_error('NOT_FOUND',
                               'No submission has been made by this user')
        else:
            return jsonify(sub.json)
Esempio n. 8
0
 def get(self):
     service: WordPressService = g.wordpress
     if not service.is_logged_in:
         return throw_error('NEEDS_LOGIN')
     user: WPUser = g.user
     if user.is_admin:
         users = service.get_users(context='edit')
     else:
         users = service.get_users()
     return jsonify(list(map(lambda x: x.json, users)))
 def get(self, id: int):
     """
     GET Get a challenge instance
     """
     challenge = Challenge.query.filter_by(id=id).first()
     if challenge is None:
         return throw_error('NOT_FOUND_ID')
     args = getter_parser.parse_args()
     data = challenge.json
     fetch_wp = args.get('fetch')
     if fetch_wp:
         if challenge.wp_id is None:
             return throw_error('NO_ASSOCIATED_ACHIEV')
         service: WordPressService = g.wordpress
         try:
             wp_challenge = service.get_challenge(challenge.wp_id,
                                                  challenge.type)
             data['wp'] = wp_challenge.json
         except UnauthorizedError:
             return throw_error('DELETED_FROM_WP')
     return jsonify(data)
Esempio n. 10
0
    def post(self, c_id: int):
        service: WordPressService = g.wordpress
        if not service.is_logged_in:
            return throw_error('NEEDS_LOGIN')
        user = service.me()

        sub: Submission = Submission.query. \
            filter(Submission.user_id == user.id). \
            filter(Submission.challenge_id == c_id). \
            first()
        if sub is not None:
            return throw_error('ALREADY_SUBMITTED')
        args = submission_parser.parse_args()
        evidences = list()
        for evidence in args['evidence']:
            evidence: dict = evidence
            description: str = evidence.get('description').replace(';', '')
            url: str = evidence.get('url').replace(';', '')
            if url and description:
                url = url.replace(';', '')
                description = description.replace(';', '')
                evidences.append(f'{url};{description}')
            else:
                return throw_error(
                    'MISSING_FIELDS',
                    'An evidence is missing a description or URL')

        comment = args['comment'].replace(';', '')
        content = ';'.join([comment] + evidences)

        sub = Submission(content=content,
                         challenge_id=c_id,
                         user_id=user.id,
                         username=user.username)
        db.session.add(sub)
        db.session.commit()

        return jsonify(sub.json)
Esempio n. 11
0
    def get(self, c_id: int):
        service: WordPressService = g.wordpress
        if not service.is_logged_in:
            return throw_error('NEEDS_LOGIN')

        user: WPUser = g.user
        if not user.is_admin:
            return throw_error('PERMISSION_NEEDED')

        subs_q = Submission.query.filter(Submission.challenge_id == c_id).\
            order_by(Submission.submitted.desc())
        args = get_submission_parser.parse_args()
        if args['not_evaluated']:
            subs_q = subs_q.filter(Submission.approved.is_(None))

        subs: List[Submission] = subs_q.all()
        updated = False
        for sub in subs:
            if sub.username is None:
                sub.username = service.get_user(sub.user_id).username
                updated = True
        if updated:
            db.session.commit()
        return jsonify(list(map(lambda x: x.json, subs)))
Esempio n. 12
0
 def get(self):
     """
     GET all challenges with a field to recognize if the logged in user has made a submission to it.
     """
     if not g.wordpress.is_logged_in:
         return throw_error('NEEDS_LOGIN')
     user: WPUser = g.user
     q = Challenge.query \
         .add_columns(Submission.query
                      .filter_by(user_id=user.id)
                      .filter_by(challenge_id=Challenge.id)
                      .exists()
                      ) \
         .order_by(Challenge.published.desc())
     results = q.all()
     data = list(
         map(lambda x: {
             **x.Challenge.json, 'submitted_to': x[1]
         }, results))
     return jsonify(data)
Esempio n. 13
0
 def get(self):
     service: WordPressService = g.wordpress
     if not service.is_logged_in:
         return throw_error('NEEDS_LOGIN')
     user: WPUser = g.user
     return jsonify(user.json)