def upload(): try: # read data as json form_data = json.loads(request.data.decode('utf-8')) response_texts = [] # write json data to csv for sheet, sheet_data in form_data["data"].items(): if form_data["type"] == "csv": sheet_data = xsv_to_array2d(sheet_data) table_name = sqlify(sheet) if Config.DESTINATION == 'redshift': response_texts.append( destination_redshift(sheet_data, table_name, current_user.path)) elif Config.DESTINATION == 'azuredw': response_texts.append( destination_azuredw(sheet_data, table_name, current_user.path)) elif Config.DESTINATION == 'snowflake': response_texts.append( destination_snowflake(sheet_data, table_name, current_user.path)) else: response_texts.append( destination_local(sheet_data, table_name, current_user.path)) response = app.response_class(response=json.dumps(response_texts), status=200, mimetype='application/json') except Exception as e: # return any error as a response to the excel macro response = app.response_class(response=json.dumps({"error": str(e)}), status=500, mimetype='application/json') return response
def move_coordinates(): if not s.board.is_game_over(): source = int(request.args.get('from', default='')) target = int(request.args.get('to', default='')) promotion = True if request.args.get('promotion', default='') == 'true' else False move = s.board.san( chess.Move(source, target, promotion=chess.QUEEN if promotion else None)) if move is not None and move != "": print("Human moves:", move) try: s.board.push_san(move) get_best_move(s.board) # move_comp = minimaxRoot(3, s.board, True) # move_comp = chess.Move.from_uci(str(move_comp)) # s.board.push(move_comp) except Exception: traceback.print_exc() response = app.response_class(response=s.board.fen(), status=200) return response print("GAME IS OVER") response = app.response_class(response="game over", status=200) return response
def add_data_point(): # this takes a dictionary with the following keys: # users string # activity int # keys (as a string) # values (as a string) # keys and values must have same length or we return 400 args = request.args.to_dict() if 'activity' in args: activity_id = int(float(args['activity'])) if Activity.exists(activity_id): jargs = combine_nl_keys_and_data(args['keys'], args['values']) if jargs[0]: data = jargs[1] data.update({'users': args['users']}) dp = DataPoint(data=data, activity_id=activity_id) db.session.add(dp) db.session.commit() return (app.response_class(response=json.dumps("OK"), status=200, mimetype='application/json')) else: return (app.response_class(response=json.dumps( "Length of keys and values did not match"), status=400, mimetype='application/json')) else: return (app.response_class( response=json.dumps("Activity doesnt exist"), status=400, mimetype='application/json'))
def save_answer(): if request.method == 'POST': try: posted_data = request.form.to_dict() board = posted_data['boggle_board'] found_list = ','.join(request.form.getlist('found_list[]')) words_list = ','.join(request.form.getlist('words_list[]')) score = posted_data['score'] gh = Game(user_id=current_user.id, board=board, found=found_list, result=words_list, score=score) db.session.add(gh) db.session.commit() message = "Score saved." response = app.response_class(response=json.dumps(message), status=200, mimetype='application/json') except: message = "Some error occurred. Sorry for the inconvineince" response = app.response_class(response=json.dumps(data), status=400, mimetype='application/json') return response
def get_credentials(current_user): if request.method == 'GET': data = request.json identifier = data['identifier'] device = Device.query.filter_by(device_identifier=identifier).first() device_json = json.dumps({ 'identifier': identifier, 'password': device.password }) curr_sess = FotaSession.query.filter_by(device=device.id).first() if curr_sess: return app.response_class(response=json.dumps( {"Error": "There is a session running"}), status=401) salt = secrets.token_urlsafe(32) enc_json = hmac.new(salt.encode('ASCII'), device_json.encode('ASCII'), sha256) fota_session = FotaSession(salt=salt, client=current_user.id, device=device.id, password=enc_json.hexdigest()) db.session.add(fota_session) db.session.commit() device_mqtt = DeviceMQTT.query.filter_by(device=device.id).first() response = app.response_class(response=json.dumps({ 'identifier': device.device_identifier, 'password': enc_json.hexdigest(), 'mqtt_session': device_mqtt.topic }), status=200, mimetype='application/json') return response
def login_user(): username = request.json.get('username', None) password = request.json.get('password', None) if not username or not password: response = app.response_class(response=json.dumps( {'error': 'Send username and password.'}), status=500, mimetype='application/json') return response user = User.query.filter_by(username=username).first() if user and user.password == hashlib.sha256( password.encode("utf-8")).hexdigest(): access_token = create_access_token( identity=user.id, fresh=True) # Puts User ID as Identity in JWT refresh_token = create_refresh_token(identity=user.id) data = json.dumps({ 'access_token': access_token, 'refresh_token': refresh_token, 'user': user.serialize() }) response = app.response_class(response=data, status=200, mimetype='application/json') return response response = app.response_class(response=json.dumps( {'error': 'The password entered is incorrect.'}), status=500, mimetype='application/json') return response
def signup(): try: email = request.form.get("email") password = request.form.get("password") salt = bcrypt.gensalt() if (is_email_available(email)): hashed_password = bcrypt.hashpw(password.encode('utf8'), salt) user = {"email": email, "password": hashed_password, "token": ""} user_data = user_col.insert_one(user) response = app.response_class(response=json.dumps({ "message": 'success', "status": 200, }), status=200, mimetype='application/json') return response else: response = app.response_class(response=json.dumps({ "message": 'already exist', "status": 403, }), status=403, mimetype='application/json') return response except Exception as e: print(e) response = app.response_class(response=json.dumps({ "message": 'something wrong', "status": 422, }), status=422, mimetype='application/json') return response
def add_entry(username, password, title): if request.method == "OPTIONS": response = app.response_class(status=200) response.headers.add('Access-Control-Allow-Origin', '*') response.headers.add('Access-Control-Allow-Headers', 'content-type, *') return response global USERNAME, PASSWORD username = hashlib.md5(username.encode('utf-8')).hexdigest() password = hashlib.md5(password.encode('utf-8')).hexdigest() print("inside add_entry") if username == USERNAME and password == PASSWORD: elements = request.get_json() global BLOG_FILE_LOCATION file_read = open(BLOG_FILE_LOCATION, 'r') prev_data = file_read.read() file_read.close() print(json.dumps(elements)) prev_data_json = json.loads( prev_data.replace("\n", " ").replace("\t", " ")) print(prev_data_json) print(prev_data_json['posts']) print(prev_data_json) posts = prev_data_json['posts'] posts += [{"title": title, 'sections': elements}] prev_data_json['posts'] = posts print(prev_data_json) file_write = open(BLOG_FILE_LOCATION, 'w') file_write.write(json.dumps(prev_data_json)) file_write.close() response = app.response_class(status=200) else: response = app.response_class(status=400) response.headers.add('Access-Control-Allow-Origin', '*') return response
def spam_item(id): if request.method == 'GET': "Get a single spam from the db" row = db.session.query(Spam).\ filter(Spam.id == id).\ first() if not row: return app.response_class(response='{"error": "Spam not found"}', mimetype='application/json', status=404) spam = row.toObject() spam['text_body'] = row.getTextBody() spam['html_body'] = row.getHtmlBody() return app.response_class(response=json.dumps(spam), mimetype='application/json') elif request.method == 'DELETE': "Delete a single spam from the db" row = db.session.query(Spam).\ filter(Spam.id == id).\ first() if not row: return app.response_class(response='{"error": "Spam not found"}', mimetype='application/json', status=404) db.session.delete(row); db.session.commit() return app.response_class(response='{"status": "ok"}', mimetype='application/json')
def sync_stations(): try: data = set(request.get_json()) full_json = requests.get('https://api.hh.ru/metro/1').json() base_stations = set() for lines in full_json['lines']: for station in lines['stations']: base_stations.add(station['name']) unchanged = data & base_stations updated = data - base_stations deleted = base_stations - data final_json = { "unchanged": list(unchanged), "updated": list(updated), "deleted": list(deleted) } response = app.response_class(response=json.dumps(final_json), status=200, mimetype='application/json') return response except Exception as ex: return app.response_class( response=str(ex), status=400, )
def get_single_package(package_id): if not package_id: return app.response_class(response=json.dumps( { error: True, message: "ID is required" }, default=str), status=200, mimetype='application/json') package = DB.packages.find_one({'_id': ObjectId(package_id)}) if not package: return app.response_class(response=json.dumps( { error: True, message: "ID is not correct" }, default=str), status=200, mimetype='application/json') hotel = DB.hotels.find_one({'_id': ObjectId(package['hotel_id'])}) package['hotel_info'] = hotel response = app.response_class(response=json.dumps(package, default=str), status=200, mimetype='application/json') return response
def process_file(): file_path = request.args.get('file_path') extension = file_path.split('.')[-1] if not path.exists(file_path): return app.response_class(response=jsonpickle.encode( {'reason': f"File {file_path} doesnt exists"}, make_refs=False, unpicklable=False), status=500, mimetype='application/json') if extension == 'mp3': type = 'audio' result = speech_to_text_service.process(file_path) else: type = 'video' result = classification_service.process_video_file(file_path) plot_image = str(plots_generator.generate_plot(result, 30 * 1000)) plot_image = plot_image[2:-1] response_body = { 'results': result, 'plot': plot_image, 'type': type, } response = app.response_class(response=jsonpickle.encode( response_body, make_refs=False, unpicklable=False), status=200, mimetype='application/json') return response
def del_entry(username, password, title): global USERNAME, PASSWORD username = hashlib.md5(username.encode('utf-8')).hexdigest() password = hashlib.md5(password.encode('utf-8')).hexdigest() if username == USERNAME and password == PASSWORD: global BLOG_FILE_LOCATION, USER_FILE_LOCATION read_file = open(BLOG_FILE_LOCATION, 'r') prev_data = read_file.read() read_file.close() print(prev_data) prev_data_json = json.loads( prev_data.replace("\n", " ").replace("\t", " ")) print(prev_data_json['posts']) posts = prev_data_json['posts'] for n in range(len(posts) - 1): print(posts[n]) if posts[n]['title'] == title: posts = posts[:n] + posts[n + 1:] print(posts) prev_data_json['posts'] = posts write_file = open(BLOG_FILE_LOCATION, 'w+') write_file.write(json.dumps(prev_data_json)) write_file.close() response = app.response_class(status=200) else: response = app.response_class(status=400) response.headers.add('Access-Control-Allow-Origin', '*') return response
def doPut(request, username): ''' check and save PUT request ''' name = username response = '' if not request.is_json: response = app.response_class( response='{"result": "incorrect json"}', status=400, mimetype='application/json' ) elif not name.isalpha(): response = app.response_class( response='{"result": "incorrect username"}', status=400, mimetype='application/json' ) else: b_date = request.json.get('dateOfBirth', None) try: resp = check_date(b_date) except ValueError as err: resp = str(err) response = app.response_class( response='{"result": "'+resp+'"}', status=200, mimetype='application/json' ) return response save_to_db(name, b_date) response = app.response_class( status=204, ) return response
def whitelist_collection(): if not is_authenticated(): return app.response_class(response='{"error": "Invalid API key"}', mimetype='application/json', status=403) if not mailgun_explicit_whitelist(request.form['email'], request.form['destination']): return app.response_class(response='{"error": "Could not whitelist email"}', mimetype='application/json', status=500) return app.response_class(response='{"status": "ok"}', mimetype='application/json')
def admin(): result = controllers.admin() if result['success']: return app.response_class(status=200, response=json.dumps( {'users': result['users']})) else: return app.response_class(status=501)
def login(): json_data = json.loads(request.data) result = controllers.login(json_data['password']) if result['success']: token = result['token'] return app.response_class(status=200, response=json.dumps({'token': token})) return app.response_class(status=501)
def logout(): username = request.form['username'] session_token = request.form['session_token'] if username in SESSION_TOKENS and SESSION_TOKENS[username] == session_token: del SESSION_TOKENS[username] response = app.response_class(response='Ok', status=200) else: response = app.response_class(response='Error', status=400) return response
def address_collection(): if not is_authenticated(): return app.response_class(response='{"error": "Invalid API key"}', mimetype='application/json', status=403) addresses = db.session.query(Address).\ all() response = [] for address in addresses: response.append(address.toObject()) return app.response_class(response=json.dumps(response), mimetype='application/json')
def domain_collection(): if not is_authenticated(): return app.response_class(response='{"error": "Invalid API key"}', mimetype='application/json', status=403) domains = db.session.query(Domain).\ all() response = [] for domain in domains: response.append(domain.toObject()) return app.response_class(response=json.dumps(response), mimetype='application/json')
def eval_desc_batch(): type_pattern = r"([a-z]*)_" radio_pattern = r"^(\w*)(_b)(\d*)(_e)(\d*)" print([item for item in request.form.items()]) batch_id = -1 de_id = -1 for tag_id, value in request.form.items(): str_match = [ t(s) for t, s in zip((str, ), re.search(type_pattern, tag_id).groups()) ][0] try: if str_match == "radio": (_, _, batch_id, _, de_id) = [ t(s) for t, s in zip((str, str, int, str, int), re.search(radio_pattern, tag_id).groups()) ] de: DescEval = query_by_id(DescEval, de_id) de.approve(value=value) de.add_self() elif str_match == "comment": (_, _, batch_id, _, de_id) = [ t(s) for t, s in zip((str, str, int, str, int), re.search(radio_pattern, tag_id).groups()) ] de: DescEval = query_by_id(DescEval, de_id) de.comments = value de.add_self() elif str_match == "better": (_, _, batch_id, _, de_id) = [ t(s) for t, s in zip((str, str, int, str, int), re.search(radio_pattern, tag_id).groups()) ] de: DescEval = query_by_id(DescEval, de_id) de.set_compare_baseline(value) de.add_self() except Exception as e: PrintException() return app.response_class(response=json.dumps({"error": str(e)}), status=601, mimetype="application/json") try: _commit_session() except Exception as exc: PrintException() return app.response_class(response=json.dumps({"error": str(exc)}), status=621, mimetype="application/json") return app.response_class(response=json.dumps(request.form), status=200, mimetype="application/json")
def register_device(): if request.method == 'POST': data = request.json identifier = data['identifier'] password = data['password'] device = Device.query.filter_by(device_identifier=identifier).first() if not bool(device): key = app.config.get('SECRET_KEY') new_password = hmac.new(key.encode('ASCII'), password.encode('ASCII'), sha256) new_device = Device(device_identifier=identifier, password=new_password.hexdigest()) db.session.add(new_device) rand_string = secrets.token_urlsafe(8) device_mqtt = DeviceMQTT(topic=rand_string, device=new_device.id) db.session.add(device_mqtt) db.session.commit() response = app.response_class(response=json.dumps({ 'status': 'Device Has Been Added', 'mqtt_topic': device_mqtt.topic }), status=200, mimetype='application/json') return response else: device_mqtt = DeviceMQTT.query.filter_by(device=device.id).first() if bool(device_mqtt): response = app.response_class(response=json.dumps({ 'status': 'Device Already Exist', 'mqtt_topic': device_mqtt.topic }), status=200, mimetype='application/json') else: rand_string = secrets.token_urlsafe(8) device_mqtt = DeviceMQTT(topic=rand_string, device=device.id) db.session.add(device_mqtt) db.session.commit() response = app.response_class(response=json.dumps({ 'status': 'Device Already Exist', 'mqtt_topic': device_mqtt.topic }), status=200, mimetype='application/json') return response else: abort(403)
def generateSentiment(current_user): try: keyword = request.form.get("keyword") # TODO # CALL ML SERVICE FUNCTION # REMOVE SLEEP time.sleep(10) #TODO #GET DATA RESULT FROM ML SERVICE #CHANGE THE ACTUAL RESULT history = { "user":current_user, "date_created": str(date.today()), "type":"Sentiment Analysis", "keyword":keyword, "result": {} } history_data = history_col.insert_one(history) #TODO #CHANGE THE ACTUAL DATA RESULT return app.response_class( response=json.dumps({ "message": 'success', "data":{ "name":"ardhito", "sentiment":"positive", "percentage":20 }, "status": 200, }), status=200, mimetype='application/json') except Exception as e: print(e) response = app.response_class( response=json.dumps({ "message": 'something wrong' }), status=422, mimetype='application/json' ) return response
def submit_response(): print(request.args.to_dict()) if "response" in request.args.to_dict().keys(): with open("responses.txt", "a") as outf: outf.write(json.dumps(request.args.to_dict())) outf.write("\n") return (app.response_class(response=json.dumps("OK"), status=200, mimetype='application/json')) else: return (app.response_class(response=json.dumps("didnt work"), status=400, mimetype='application/json'))
def use_blog_updater(username, password): global USERNAME, PASSWORD username = hashlib.md5(username.encode('utf-8')).hexdigest() password = hashlib.md5(password.encode('utf-8')).hexdigest() if username == USERNAME and password == PASSWORD: update_blog() response = app.response_class(status=200) else: response = app.response_class(status=400) response.headers.add('Access-Control-Allow-Origin', '*') return response
def register(): if request.method == 'POST': try: data = request.json username = data['username'] password = data['password'] hashed_pass = generate_password_hash(password, method='sha256') user = Client(username=username, password=hashed_pass) db.session.add(user) db.session.commit() return app.response_class(status=200) except: return app.response_class(status=400)
def spam_filter_item(id): if not is_authenticated(): return app.response_class(response='{"error": "Invalid API key"}', mimetype='application/json', status=403) url = "%s/routes/%s" % (app.config['MAILGUN_API_URL'], id) auth = ('api', app.config['MAILGUN_API_KEY']) if request.method == 'GET': r = requests.get(url, auth=auth) elif request.method == 'DELETE': r = requests.delete(url, auth=auth) return app.response_class(response=r.text, mimetype='application/json', status=r.status_code)
def authenticate(username, password): global USERNAME, PASSWORD print(username, password) print(USERNAME, PASSWORD) username = hashlib.md5(username.encode('utf-8')).hexdigest() password = str(hashlib.md5(password.encode('utf-8')).hexdigest()) print(username, password) if username == USERNAME and password == PASSWORD: response = app.response_class(status=200) else: response = app.response_class(status=400) response.headers.add('Access-Control-Allow-Origin', '*') return response
def update(): data = json.loads(request.data) environment = data.get("environment") branch = data.get("branch") jira_id = data.get("jira_id") # Ensure the correct data is provided in if not environment or not branch: return app.response_class( response=json.dumps({ "title": "missing parameters", "message": "Both 'environment' and `branch` fields are required", "status:": 400, }), status=400, mimetype="application/json", ) return make_response() if environment not in app.config["ACTIVE_ENVIRONMENTS"]: return app.response_class( response=json.dumps({ "title": "invalid environment", "message": "'environment' must be one of the following: {}".format( ",".join(app.config["ACTIVE_ENVIRONMENTS"])), "status:": 400, }), status=400, mimetype="application/json", ) redis_db.set(environment, json.dumps({ "branch": branch, "jira_id": jira_id })) return app.response_class( response=json.dumps({ "data": "{} marked in used by {}".format(environment, branch), "status:": 201 }), status=201, mimetype="application/json", )
def describe(): try: response = do_describe(request.form["link"], method='baseline4') response.pop('news', None) response.pop('grupo', None) return app.response_class(response=json.dumps(response), status=200, mimetype='application/json', direct_passthrough=True) except Exception as exc: PrintException() return app.response_class(response=json.dumps({'error': 'erro'}), status=530, mimetype='application/json', direct_passthrough=True)
def authenticate_device(): if request.method == 'POST': data = request.json identifier = data['identifier'] request_password = data['password'] print(identifier + " || " + request_password) device = Device.query.filter_by(device_identifier=identifier).first() device_dumps = json.dumps({ 'identifier': device.device_identifier, 'password': device.password }) fota_session = FotaSession.query.filter_by( device=device.id, password=request_password).first() if (not fota_session.used): received_password = hmac.new(fota_session.salt.encode('ASCII'), device_dumps.encode('ASCII'), sha256) if hmac.compare_digest(fota_session.password, received_password.hexdigest()): fota_session.used = True db.session.commit() response = app.response_class(status=200, mimetype='application/json') return response else: abort(401)
def gallery_get(gallery_id): """ Get a gallery """ gallery = find_gallery_by_id(gallery_id) if not gallery: abort(404) return app.response_class(response=json.dumps(gallery.to_object()), mimetype='application/json')
def delete_step_and_references(step_name): if current_user.username in app.config.get('ADMINS'): if step_name: delete_step_and_references_from_database(IMAGE_PROCESSING_DB, step_name) response = app.response_class(status=200, mimetype='application/json') return response
def search_results(): if request.method == 'POST': os.system('python ' + crawlerFile + ' ' + request.json['product']) print('Crawling Completed') title = [] image = [] price = [] url = [] source = [] with open(resultFile) as f: records = csv.DictReader(f) for row in records: title.append(row['product_name']) image.append(row['image_url']) price.append(row['price']) url.append(row['product_url']) source.append(row['source']) data = dict({ 'product_name': title, 'image_url': image, 'price': price, 'product_url': url, 'source': source }) response = app.response_class(response=json.dumps(data, cls=MyEncoder), status=200, mimetype='application/json') return response
def excludes(): print('Adding excludes for [' + request.args.get('uuid') + '] : ' + request.args.get('excludes')) return app.response_class(response=json.dumps( json.loads('{"success": true}')), status=200, mimetype='application/json')
def handle(token): if token == bot.token: request_body_dict = request.json update = types.Update.de_json(request_body_dict) bot.process_new_updates([update]) return app.response_class( response='OK', status=200, mimetype='core/json' ) else: return app.response_class( response='Error', status=403, mimetype='core/json' )
def upload_view(token): upload = TrexUpload.get_404(token=token) if upload.user != g.user: if TrexUploadTemporaryAccess.objects(upload=upload, user=g.user).first() is None: abort(404) if 'If-None-Match' in request.headers and request.headers['If-None-Match'] == upload.token: return app.response_class('', 304) response = app.response_class(upload.file.get(), 200) response.headers['Content-Type'] = upload.file.content_type response.headers['ETag'] = upload.token response.headers['Cache-Control'] = 'private, max-age=31622400' response.headers['Content-Disposition'] = 'filename=%s' % upload.file.filename return response
def getpic(username,name): user = g.user qu=User.query.filter_by(username=username).first().pics.filter_by(name='profile.jpg').first() if qu is None: abort(404) else: mimetype = 'image/jpg' return app.response_class(qu.data,mimetype=mimetype,direct_passthrough=False)
def domain_item(domain_id): if not is_authenticated(): return app.response_class(response='{"error": "Invalid API key"}', mimetype='application/json', status=403) row = db.session.query(Domain).\ filter(Domain.id == domain_id).\ first() if not row: return app.response_class(response='{"error": "Not found"}', mimetype='application/json', status=404) response = row.toObject() if request.method == 'DELETE': db.session.delete(row) db.session.commit() return app.response_class(response=json.dumps(response), mimetype='application/json')
def address_item(address_id): if not is_authenticated(): return app.response_class(response='{"error": "Invalid API key"}' % request.headers['Authorization'], mimetype='application/json', status=403) row = db.session.query(Address).\ filter(Address.id == address_id).\ first() if not row: return app.response_class(response='{"error": "Not found"}', mimetype='application/json', status=404) response = row.toObject() if request.method == 'DELETE': db.session.delete(row) db.session.commit() return app.response_class(response=json.dumps(response), mimetype='application/json')
def decorated_function(*args, **kwargs): callback = request.args.get('callback', False) if callback: data = str(func(*args, **kwargs).data) content = str(callback) + '(' + data + ')' mimetype = 'application/javascript' return app.response_class(content, mimetype=mimetype) else: return func(*args, **kwargs)
def daemon(): # Get a list of all domains domains = db.session.query(Domain).\ order_by(Domain.name).\ all() # Do a whois lookup on each domain for domain in domains: # Retrieve the stored whois for this domain whois = db.session.query(Whois).\ filter(Whois.id == domain.whois_id).\ first() # Lookup the current whois whois_fresh = get_whois(domain.name)[0] # Strip \r returns whois_fresh = re.sub(r'\r', '', whois_fresh, flags=re.MULTILINE) # Filter the whois info for .ca domains whois_fresh = re.sub(r'(%.*)\n', '', whois_fresh, flags=re.MULTILINE) # Filter the whois info for .com domains whois_fresh = re.sub(r'(>>>.*)\n', '', whois_fresh, flags=re.MULTILINE) whois_fresh = re.sub(r'(^Timestamp:.*)\n', '', whois_fresh, flags=re.MULTILINE) whois_fresh = re.sub(r'(^Cached on:.*)\n', '', whois_fresh, flags=re.MULTILINE) if len(whois_fresh.splitlines()) < 10: continue if whois is None: # No previous whois found, so just save it whois = Whois(domain=domain.name, value=whois_fresh) db.session.add(whois) db.session.commit() elif whois_fresh is not None and whois.value != whois_fresh: # Build the email body diff = unified_diff(whois.value.split('\n'), whois_fresh.split('\n'), fromfile="old-whois.txt", tofile="new-whois.txt") email_body = '<strong>%s changes:</strong><br /><pre>%s</pre>' % (domain.name, "\n".join(diff)) # If the whois value differs, send an email with the difference email = Message("%s Domain Name Notification" % domain.name, recipients=[app.config['NOTIFY_EMAIL']], html=email_body) mail.send(email) # Save the new whois value whois = Whois(domain=domain.name, value=whois_fresh) db.session.add(whois) db.session.commit() # Associate the domain with this whois value if domain.whois_id != whois.id: domain.whois_id = whois.id db.session.add(domain) db.session.commit() return app.response_class(response='{"status":"ok"}', mimetype='application/json')
def member(domain): if not is_authenticated(): return app.response_class(response='{"error": "Invalid API key"}' % request.headers['Authorization'], mimetype='application/json', status=403) row = find_domain(domain) if not row: return app.response_class(response='{"error": "Not found"}' % domain, mimetype='application/json', status=404) response = { "id": row.id, "name": row.name, "whois": find_whois(row.whois_id) } if request.method == 'DELETE': db.session.delete(row) db.session.commit() return app.response_class(response=json.dumps(response), mimetype='application/json')
def message_collection(): # Extract the local_part@domain_name local_part, domain_name = parse_address(request.form.get('recipient')) # Get the domain name try: domain = db.session.query(Domain).\ filter(Domain.name == domain_name).\ first() except ValueError, e: return app.response_class(response='{"error": "%s"}' % e, mimetype='application/json', status=400)
def index(): if 'X-Forwarded-For' in request.headers: ipAddress = request.headers['X-Forwarded-For'] else: ipAddress = request.remote_addr response = json.dumps({ "address": re.sub(r",.+$", "", ipAddress) }) return app.response_class(response=response, mimetype='application/json')
def gallery_post(): """ Add a new gallery """ if current_user.role == "guest": abort(404) gallery = Gallery(name=request.form['name']) gallery.save() response = gallery.to_object() return app.response_class(response=json.dumps(response), mimetype='application/json')
def file_add(): """ Add a file """ file = File( name=urldecode(request.form['name']), folder=request.form['folder'], size=int(request.form['size']), owner_id=int(current_user.id) ) db.session.add(file) db.session.commit() return app.response_class(response=json.dumps(file.to_object()), mimetype='application/json')
def serve_file(document, field, index=None, set_filename=True): from app import app try: file = getattr(document, field) if index is not None: file = file[int(index)] except IndexError: abort(404) if 'If-None-Match' in request.headers and request.headers['If-None-Match'] == str(file._id): return app.response_class('', 304) response = app.response_class(file.get(), 200) response.headers['Content-Type'] = file.content_type response.headers['ETag'] = str(file._id) response.headers['Cache-Control'] = 'private, max-age=31622400' response.headers['Content-Length'] = file.length if set_filename: response.headers['Content-Disposition'] = 'filename="%s"' % file.filename return response
def collection(): if not is_authenticated(): return app.response_class(response='{"error": "Invalid API key"}', mimetype='application/json', status=403) if request.method == 'POST': try: domains = json.loads(request.data) except ValueError: return app.response_class(response='{"error": "Invalid JSON"}', mimetype='application/json', status=400) for domain in domains: try: row = find_domain(domain) except ValueError, e: db.session.rollback() return app.response_class(response='{"error": "%s"}' % e, mimetype='application/json', status=400) if row is None: row = Domain(name=domain) db.session.add(row) db.session.commit()
def spam(): if request.method == 'GET': if not is_authenticated(): return app.response_class(response='{"error": "Invalid API key"}', mimetype='application/json', status=403) spams = db.session.query(Spam).\ order_by(Spam.created).\ all() response = [] for spam in spams: response.append(spam.toObject()) return app.response_class(response=json.dumps(response), mimetype='application/json') elif request.method == 'POST': try: row = Spam( to_header=request.form.get('recipient'), from_header=request.form.get('sender'), subject_header=request.form.get('subject'), text_body=request.form.get('body-plain'), html_body=request.form.get('body-html'), spam_score=request.form.get('X-Mailgun-Sscore') ) db.session.add(row) db.session.commit() except: print "Error saving Spam: %s" % json.dumps(request.form) return app.response_class(response='{"status": "error"}', mimetype='application/json', status=400) return app.response_class(response='{"status": "ok"}', mimetype='application/json') elif request.method == 'DELETE': "Delete all rows from the db" db.session.query(Spam).delete() db.session.commit() return app.response_class(response='{"status": "ok"}', mimetype='application/json')
def spam_filter(): if not is_authenticated(): return app.response_class(response='{"error": "Invalid API key"}', mimetype='application/json', status=403) url = "%s/routes" % app.config['MAILGUN_API_URL'] auth = ('api', app.config['MAILGUN_API_KEY']) if request.method == 'GET': r = requests.get(url, auth=auth) elif request.method == 'POST': params = { "priority": 50, "expression": 'match_recipient("%s@%s")' % (request.form['local'], request.form['domain']), "action": "stop()" } if 'description' in request.form: params['description'] = request.form['description'] r = requests.post(url, params=params, auth=auth) return app.response_class(response=r.text, mimetype='application/json', status=r.status_code)
def file_delete(file_id): """ Delete a file """ file = find_file_by_id(file_id) if not file: abort(404) # Delete the file from S3 file.delete() # Remove the file from the database db.session.delete(file) db.session.commit() return app.response_class(response=json.dumps(file.to_object()), mimetype='application/json')
def gallery_item(gallery_id): """ Get/update/delete an individual gallery """ if current_user.role == "guest": abort(404) gallery = Gallery.find_by_id(gallery_id) if not gallery: abort(404) if not gallery.delete(): response = ['Error'] else: reponse = [] return app.response_class(response=json.dumps(response), mimetype='application/json')
def photo_delete(gallery_id, photo_id): """ Delete a photo from a gallery """ if current_user.role == "guest": abort(404) photo = Photo.find_by_id(photo_id) if not photo: abort(404) response = [] if not photo.delete(): response = ["error"] # Update the gallery modified date photo.gallery.update_modified() return app.response_class(response=json.dumps(response), mimetype='application/json')
def photo_delete(gallery_id, photo_id): """ Delete a photo from a gallery """ if current_user.role == "guest": abort(404) gallery = find_gallery_by_id(gallery_id) if not gallery: abort(404) response = [] if not delete_photo(gallery.folder, photo_id): response = ["error"] # Update the gallery modified date gallery.updateModified() return app.response_class(response=json.dumps(response), mimetype='application/json')
def photo_thumbnail(): """ Add a photo to a gallery """ if current_user.role == "guest": abort(404) photo = Photo( name=urldecode(request.form['name']), ext=request.form['ext'], gallery_id=request.form['gallery_id'], aspect_ratio=1.0, owner_id=1, created="2000:01:01 00:00:00" ) # Tell the thumbnail daemon to generate a thumbnail for this photo photo.generate_thumbnail() return app.response_class(response="[]", mimetype='application/json')
def gallery_index(): """ List all galleries """ if not request.args.get('page'): abort(500) page_num = int(request.args.get('page')) if request.args.get('q'): search_query = request.args.get('q') else: search_query = None limit = 5 offset = (page_num - 1) * limit galleries = find_gallery_all(offset=offset, limit=limit, search_query=search_query) response = [] for gallery in galleries: response.append(gallery.to_object()) return app.response_class(response=json.dumps(response), mimetype='application/json')
def view_point_values(): query_from = request.args.get("from") query_days = request.args.get("days") if (query_from is not None) and (query_days is not None): # if "from" or "to" timestamps are present in the query string, get values from that time span. try: timestamp_start = DatetimeConverter.to_python(query_from) days = int(query_days) except (ValueError, TypeError): abort(400) results = get_all_points_value_range(timestamp_start, days) else: results = get_all_points_all_values() if not results: # no objects found matching the request - 404 abort(404) return app.response_class(json.dumps(results, indent=2), mimetype='application/json')
def gallery_item(gallery_id): """ Get/update/delete an individual gallery """ if current_user.role == "guest": abort(404) gallery = find_gallery_by_id(gallery_id) if not gallery: abort(404) if request.method == 'GET': response = gallery.to_object() elif request.method == 'DELETE': # Delete all photos from the gallery gallery.delete() # Delete the gallery from the database db.session.delete(gallery) db.session.commit() response = [] return app.response_class(response=json.dumps(response), mimetype='application/json')
def photo_post(): """ Add a photo to a gallery """ if current_user.role == "guest": abort(404) photo = Photo( name=urldecode(request.form['name']), ext=request.form['ext'], aspect_ratio=float(request.form['aspect_ratio']), gallery=Gallery.find_by_id(request.form['gallery_id']), owner=current_user, created=request.form['created'] ) # Save the updated photos JSON for this gallery photo.save() # Update the gallery modified date photo.gallery.update_modified() return app.response_class(response=json.dumps(photo.to_object()), mimetype='application/json')