def upload_source(self, input, file):
        """
        POST /upload_source
        """
        alerts = []
        if get_exeflags(file["content"]):
            alerts.append({
                "severity": "warning",
                "message": "You have submitted an executable! Please send the "
                           "source code."
            })
            Logger.info("UPLOAD",
                        "User %s has uploaded an executable" % input["token"])
        if not alerts:
            alerts.append({
                "severity": "success",
                "message": "Source file uploaded correctly."
            })

        source_id = Database.gen_id()
        try:
            path = StorageManager.new_source_file(source_id, file["name"])
        except ValueError:
            BaseHandler.raise_exc(BadRequest, "INVALID_FILENAME",
                                  "The provided file has an invalid name")

        StorageManager.save_file(path, file["content"])
        file_size = StorageManager.get_file_size(path)

        Database.add_source(source_id, input["id"], path, file_size)
        Logger.info("UPLOAD", "User %s has uploaded the source %s" % (
            input["token"], source_id))
        output = BaseHandler.format_dates(Database.get_source(source_id))
        output["validation"] = {"alerts": alerts}
        return output
Exemplo n.º 2
0
 def _guess_token(**kwargs):
     if "token" in kwargs:
         return kwargs["token"]
     elif "input_id" in kwargs:
         input = Database.get_input(kwargs["input_id"])
         if input: return input["token"]
     elif "output_id" in kwargs:
         output = Database.get_output(kwargs["output_id"])
         if output:
             input = Database.get_input(output["input"])
             if input: return input["token"]
     elif "source_id" in kwargs:
         source = Database.get_source(kwargs["source_id"])
         if source:
             input = Database.get_input(source["input"])
             if input: return input["token"]
     elif "submission_id" in kwargs:
         submission = Database.get_submission(kwargs["submission_id"])
         if submission: return submission["token"]
     return None