def create_server(): '''Создает сервер типа farhub ''' if (API_KEY != request.args.get('api')): return jsonify({'error': 'Wrong api key'}), 400 json_data = request.get_json(force=True) try: srv = Server.objects.get(hash_id=json_data['hash_id']) srv.delete() except Exception: pass finally: try: coun = Country.objects.get(name=json_data['location']) except: coun = Country(name=json_data['location']) coun.save() try: server_name = Type_Of_Server.objects.get( type_of_server='gtn_farhub') except: server_name = Type_Of_Server(type_of_server='gtn_farhub') server_name.save() server = Server(type_of_server=server_name, address=json_data['server_ip'], location=coun, api_key=json_data['api_key'], data=json_data['data'], description=json_data['description'], hash_id=json_data['hash_id']) server.save() Country.synhro() monitoring_service() return '200 OK'
def delete_server_obj(host): srv = Server.objects.get(address=host) coun = Country.objects.get(name=srv.location.name) coun.servers.pop( srv.location.servers.index(Server.objects.get(address=srv.address))) coun.save() srv.delete() Country.synhro() return render_template('delete.html', name=get_user_name())
def country_view(): Country.synhro() message = '' text_list = [] country_list = Country.objects.all() for country in country_list: text_list.append(country.to_json()) return render_template('countries.html', text_list=text_list, message=message, country_list=country_list, name=get_user_name())
def server_edit(host): types = Type_Of_Server.objects.all() countries = Country.objects.all() parsers = Searcher.objects.all() srv = Server.objects.get(address=host) prev = srv.location.name cont = Country.objects.get(name=prev) #this FOR deletes old info about Country, when location Server changes for el in cont.servers: if el.address == srv.address: cont.servers.pop(cont.servers.index(el)) cont.save() color = 'success' message = '' info = srv.to_json() if request.method == 'POST': type_of_server = request.form.get('type_of_server') address = request.form.get('address') description = request.form.get('description') location = request.form.get('location') coun = Country.objects.get(name=location) data = request.form.getlist('engines') if srv.data.get('aggregators', None) == None: srv.data['aggregators'] = [] for en in data: parser = Searcher.objects.get(engine=en) print(srv.data['aggregators']) if parser.to_json() not in srv.data['aggregators']: print('shit!') srv.data['aggregators'].append(parser.to_json()) application = Type_Of_Server.objects.get(type_of_server=type_of_server) srv.type_of_server = application srv.address = address srv.description = description srv.location = coun srv.save() Country.synhro() message = 'success' return render_template('editserver.html', message=message, color=color, info=info, countries=countries, types=types, parsers=parsers, name=get_user_name())
def server_list(): types = Type_Of_Server.objects.all() countries = Country.objects.all() color = 'success' message = '' if request.method == 'POST': type_of_server = request.form.get('type_of_server') address = request.form.get('address') description = request.form.get('description') location = request.form.get('location') type_server = Type_Of_Server.objects.get(type_of_server=type_of_server) coun = Country.objects.get(name=location) hash_id = ''.join( random.choices(string.ascii_lowercase + string.digits, k=24)) if address != '': srv = Server( type_of_server=type_server, address=address, description=description, location=coun, hash_id=hash_id, ) srv.save() Country.synhro() monitoring_service() message = 'success' else: color = 'danger' message = 'You must type anything!' return render_template('server.html', message=message, color=color, countries=countries, types=types, name=get_user_name())
from app import app from app.monitor import monitoring_service from app.models import Country, Type_Of_Server, Server, Language, Searcher import flask import requests import string import random from pprint import pprint from flask_login import login_required, current_user from flask import Blueprint from app.models import users_db main = Blueprint('views', __name__) Country.synhro() Type_Of_Server.synhro() def get_user_name(): try: name = current_user.name return name except: return None @main.route('/profile') def profile(): return 'Profile'