"""Problem feedback related endpoints."""
from flask import jsonify
from flask_restplus import Namespace, Resource

import api
from api import (block_before_competition, check_csrf, PicoException,
                 require_login)

from .schemas import feedback_list_req, feedback_submission_req

ns = Namespace('feedback', description='List and submit problem feedback')


@ns.route('')
class FeedbackList(Resource):
    """Get the list of problem feedback, or submit new feedback."""

    # @TODO only show the current user's feedback, unless an admin, then allow
    #       uid/pid/tid filtering
    @block_before_competition
    @require_login
    @ns.response(200, 'Success')
    @ns.response(400, 'Error parsing request')
    @ns.response(401, 'Not logged in')
    @ns.response(422, 'Competition has not started')
    @ns.expect(feedback_list_req)
    def get(self):
        """Get the list of problem feedback, with optional filtering."""
        if not api.config.get_settings()['enable_feedback']:
            raise PicoException('Problem feedback is not currently being ' +
                                'accepted.', 500)

        req = feedback_list_req.parse_args(strict=True)

        # Handle args if they are present but unset
        for arg in ['pid', 'uid', 'tid']:
            if req[arg] == '':
                req[arg] = None

        return jsonify(api.problem_feedback.get_problem_feedback(
            pid=req['pid'], tid=req['tid'], uid=req['uid']
        ))

    @check_csrf
    @block_before_competition
    @require_login
    @ns.response(201, 'Feedback accepted')
    @ns.response(400, 'Error parsing request')
    @ns.response(403, 'CSRF token invalid')
    @ns.response(404, 'Problem not found')
    @ns.response(422, 'Competition has not started')
    @ns.response(500, 'Feedback submissions disabled')
    @ns.expect(feedback_submission_req)
    def post(self):
        """
        Submit problem feedback.

        Will update existing feedback if the current user and provided
        problem ID match a previous submission.
        """
        if not api.config.get_settings()['enable_feedback']:
            raise PicoException(
                'Problem feedback is not currently being accepted.',
                response_code=500
            )
        req = feedback_submission_req.parse_args(strict=True)
        api.problem_feedback.upsert_feedback(
            req['pid'], req['feedback']
        )
        res = jsonify({
            "success": True
        })
        res.status_code = 201
        return res

@@ -482,121 +482,8 @@
 %22%22%0A%0A
-    # @TODO only show the current user's feedback, unless an admin, then allow%0A    #       uid/pid/tid filtering%0A
     
@@ -646,16 +646,56 @@
 ed in')%0A
+    @ns.response(403, 'Not authorized')%0A
     @ns.
@@ -799,24 +799,33 @@
 %0A        %22%22%22
+%0A        
 Get the list
@@ -820,32 +820,47 @@
 Get the list of 
+your submitted 
 problem feedback
@@ -863,37 +863,10 @@
 back
-, with optional filtering.%22%22%22
+.%0A
 %0A   
@@ -874,148 +874,157 @@
     
-if not api.config.get_settings()%5B'enable_feedback'%5D:%0A            raise PicoException('Problem feedback is not currently being ' +%0A  
+If called as an admin, returns all submitted feedback, with optional%0A        filtering using the ?pid, ?uid, and ?tid querystring arguments.%0A
         
     
@@ -1015,32 +1015,36 @@
 ts.%0A        
+%22%22%22%0A
         
             
@@ -1035,40 +1035,39 @@
     
-              'accepted.', 500)%0A
+curr_user = api.user.get_user()
 %0A   
@@ -1278,16 +1278,404 @@
 = None%0A%0A
+        # Non-admins can only view their own feedback%0A        if not curr_user.get('admin', False):%0A            for arg in %5B'pid', 'uid', 'tid'%5D:%0A                if req%5Barg%5D is not None:%0A                    raise PicoException(%0A                        'You must be an admin to filter the feedback list',%0A                        status_code=403)%0A            req%5B'uid'%5D = curr_user%5B'uid'%5D%0A%0A
         

Fix /feedback authorization