示例#1
0
    def get(self, **kwargs):
        """"Provides the full details for a study from the database.

            Provides a study, in JSON format, with all fields of data, given the ID of the study.
            Intended for use in administrative review.
            For normal previewing, use GetPreview.get().

            Args:
                study_id (Integer): The identifier of the study to be reviewed.


            Returns:
                JSON: The study with its template.
            """
        # obtain parameters
        parser = reqparse.RequestParser(bundle_errors=True)
        parser.add_argument("study_id",
                            type=int,
                            required=True,
                            help="Specify a study for details.")

        # the second parameter to each method call is purely for consistency,
        # they don't actually do anything. They should match the defaults above.
        returned_args = parser.parse_args()
        study_id = returned_args.get("study_id", None)

        # query database
        study = Auxiliary.getStudy(study_id)

        # return converted output
        return jsonify(study.build_dict())
示例#2
0
    def get(self, **kwargs):
        """"Establishes an owns relationship between a study and a user.

            Establishes the owns relationship only if the user has sufficient credits and doesn't already own the study.

            Args:
            
                token (String): A tokenized identifier of a user, tokenization is done with
                                a flask GET HTTP request using the crypto blueprint and formatted like this.
                                ('/token/generate', data={'user_id': 'VALID_USER_IN_DATABASE'})
                                
                study_id (int): The identifier for the study the user is trying to purchase.
                
                credits_available (int): The current credit balance for the user. Overrides any stored balance.

            Returns:
                JSON: The cost of the study, or an error message.
            """
        # obtain parameters
        parser = reqparse.RequestParser(bundle_errors=True)
        #parser.add_argument("user_id", type=str, required=True, help="The user ID of the customer is a String.")
        parser.add_argument(
            "study_id",
            type=int,
            required=True,
            help="The study ID of the study being purchased is an integer.")
        parser.add_argument(
            "credits_available",
            type=int,
            required=True,
            help="The credit balance available to the customer is an integer.")
        returned_args = parser.parse_args()
        user_id = kwargs["user_id"]  #returned_args.get("user_id", None)
        study_id = returned_args.get("study_id", None)
        credits_available = returned_args.get("credits_available", None)
        # verify the parameters exist - now handled by add_argument
        # if user_id == None or study_id == None or credits_available == None:
        #    return jsonify({"error": "missing parameter"})
        # get the necessary data from the database
        user = Auxiliary.getUser(user_id)
        # check for ownership first, because credits won't matter if already owned
        if study_id in user.get_ownedStudies():
            return jsonify({"cost": 0})
        study = Auxiliary.getStudy(study_id)
        cost = study.get_costInCredits()
        # check for sufficient credits and not already owning the study
        if cost > credits_available:
            return jsonify({"error": "insufficient credits"})
        # update the user data
        Auxiliary.addOwned(user_id, study_id, cost)
        # return the cost
        return jsonify({"cost": cost})