<commit_before>'''
Name: <NAME>
Date: 2014-08-10
Project: MLSB API
Purpose: To create an application to act as an api for the database
'''
from flask.ext.restful import Resource, reqparse
from flask import Response
from json import dumps
from api.validators import string_validator
from api.model import Sponsor
from api import DB

parser = reqparse.RequestParser()
parser.add_argument('sponsor_name', type=str)


HEADERS = [{'header':'sponsor_name', 'required':True, 
            'validator':string_validator}]


class SponsorAPI(Resource):
    def get(self, sponsor_id):
        """
            GET request for Sponsor Object matching given sponsor_id
            Route: /sponsors/<sponsor_id: int>
            Returns:
                status: 200 
                mimetype: application/json
                data: 
                    success: tells if request was successful (boolean)
                    message: the status message (string)
                    data:  {sponsor_id:int, sponsor_name :string, 
                            sponsoer_picture_id: int, photo_file:string}
        """
        # expose a single Sponsor
        result = {'success': False,
                  'message': '',
                  'failures':[]}
        entry  = Sponsor.query.get(sponsor_id)
        if entry is None:
            result['message'] = 'Not a valid sponsor ID'
            return Response(dumps(result), status=404,
                             mimetype="application/json")
        result['success'] = True
        result['data'] = entry.json()
        return Response(dumps(result), status=200, mimetype="application/json")


    def delete(self, sponsor_id):
        """
            DELETE request for Sponsor
            Route: /sponsors/<sponsor_id:int>
            Returns:
                status: 200 
                mimetype: application/json
                data: 
                    success: tells if request was successful (boolean)
                    message: the status message (string)
        """
        result = {'success': False,
                  'message': '',}
        # delete a single user
        sponsor = Sponsor.query.get(sponsor_id)
        if sponsor is None:
            #Sponsor is not in the table
            result['message'] = 'Not a valid Sponsor ID'
            return Response(dumps(result), status=400,
                            mimetype="application/json")
        DB.session.delete(sponsor)
        DB.session.commit()
        result['success'] = True
        result['message'] = 'Sponsor was deleted'
        return Response(dumps(result), status=200, mimetype="application/json")


    def put(self, sponsor_id):
        """
            PUT request for Sponsor
            Route: /sponsors/<sponsor_id:int>
            Parameters :
                sponsor_name: The Sponsor's name (string)
                sponser_picture_id: the picture for the sponser (int)
            Returns:
                status: 200 
                mimetype: application/json
                data: 
                    success: tells if request was successful (boolean)
                    message: the status message (string)
                    failures: a list of parameters that failed to update 
                              (list of string)
        """
        # update a single user
        result = {'success': False,
                  'message': 'Failed to properly supply the required fields',
                  'failures':[]}
        sponsor = Sponsor.query.get(sponsor_id)
        args = parser.parse_args()
        if sponsor is None:
            result['message'] = 'Not a valid sponsor ID'
            return Response(dumps(result), status=404,
                            mimetype="application/json")
        args = parser.parse_args()
        if args['sponsor_name'] and string_validator(args['sponsor_name']):
            sponsor.name = args['sponsor_name']
            result['success'] = True
            result['message'] = ""
            DB.session.commit()
        elif args['sponsor_name'] and not string_validator(args['sponsor_name']):
            result['failures'].append("Invalid sponsor name")
        return Response(dumps(result), status=200, mimetype="application/json")
<commit_msg>Small fixes to a few logic errors<commit_after>'''
Name: <NAME>
Date: 2014-08-10
Project: MLSB API
Purpose: To create an application to act as an api for the database
'''
from flask.ext.restful import Resource, reqparse
from flask import Response
from json import dumps
from api.validators import string_validator
from api.model import Sponsor
from api import DB

parser = reqparse.RequestParser()
parser.add_argument('sponsor_name', type=str)


HEADERS = [{'header':'sponsor_name', 'required':True, 
            'validator':string_validator}]


class SponsorAPI(Resource):
    def get(self, sponsor_id):
        """
            GET request for Sponsor Object matching given sponsor_id
            Route: /sponsors/<sponsor_id: int>
            Returns:
                status: 200 
                mimetype: application/json
                data: 
                    success: tells if request was successful (boolean)
                    message: the status message (string)
                    data:  {sponsor_id:int, sponsor_name :string, 
                            sponsoer_picture_id: int, photo_file:string}
        """
        # expose a single Sponsor
        result = {'success': False,
                  'message': '',
                  'failures':[]}
        entry  = Sponsor.query.get(sponsor_id)
        if entry is None:
            result['message'] = 'Not a valid sponsor ID'
            return Response(dumps(result), status=404,
                             mimetype="application/json")
        result['success'] = True
        result['data'] = entry.json()
        return Response(dumps(result), status=200, mimetype="application/json")


    def delete(self, sponsor_id):
        """
            DELETE request for Sponsor
            Route: /sponsors/<sponsor_id:int>
            Returns:
                status: 200 
                mimetype: application/json
                data: 
                    success: tells if request was successful (boolean)
                    message: the status message (string)
        """
        result = {'success': False,
                  'message': '',}
        # delete a single user
        sponsor = Sponsor.query.get(sponsor_id)
        if sponsor is None:
            #Sponsor is not in the table
            result['message'] = 'Not a valid Sponsor ID'
            return Response(dumps(result), status=404,
                            mimetype="application/json")
        DB.session.delete(sponsor)
        DB.session.commit()
        result['success'] = True
        result['message'] = 'Sponsor was deleted'
        return Response(dumps(result), status=200, mimetype="application/json")


    def put(self, sponsor_id):
        """
            PUT request for Sponsor
            Route: /sponsors/<sponsor_id:int>
            Parameters :
                sponsor_name: The Sponsor's name (string)
                sponser_picture_id: the picture for the sponser (int)
            Returns:
                status: 200 
                mimetype: application/json
                data: 
                    success: tells if request was successful (boolean)
                    message: the status message (string)
                    failures: a list of parameters that failed to update 
                              (list of string)
        """
        # update a single user
        result = {'success': False,
                  'message': 'Failed to properly supply the required fields',
                  'failures':[]}
        sponsor = Sponsor.query.get(sponsor_id)
        args = parser.parse_args()
        if sponsor is None:
            result['message'] = 'Not a valid sponsor ID'
            return Response(dumps(result), status=404,
                            mimetype="application/json")
        args = parser.parse_args()
        if args['sponsor_name'] and string_validator(args['sponsor_name']):
            sponsor.name = args['sponsor_name']
            result['success'] = True
            result['message'] = ""
            DB.session.commit()
        elif args['sponsor_name'] and not string_validator(args['sponsor_name']):
            result['failures'].append("Invalid sponsor name")
            return Response(dumps(result), status=400, mimetype="application/json") 
        return Response(dumps(result), status=200, mimetype="application/json")

{'fraction_non_alphanumeric': 0.09626353160284018, 'fraction_numerical': 0.008497264579210802, 'mean_word_length': 1.829107672044781, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4697845', 'n_tokens_mistral': 2359, 'n_tokens_neox': 2129, 'n_words': 624}