def config_backup(request, action): main_config_qc = main_config.objects.all() upstream_config_qc = upstream_config.objects.all() proxy_config_qc = proxy_config.objects.all() if action == "export": try: m_config = serializers.serialize('json', main_config_qc) u_config = serializers.serialize('json', upstream_config_qc) p_config = serializers.serialize('json', proxy_config_qc) config = { "main_config": m_config, "upstream_config": u_config, "proxy_config": p_config } print(config) content = {"flag": "Success", "context": config} except Exception as e: content = {"flag": "Error", "context": str(e)} elif action == "import": try: post = json.loads(request.body.decode('utf-8')) m_config = post['main_config'] u_config = post['upstream_config'] p_config = post['proxy_config'] main_config_qc.delete() upstream_config_qc.delete() proxy_config_qc.delete() for obj in serializers.deserialize('json', m_config): obj.save() for obj in serializers.deserialize('json', u_config): obj.save() for obj in serializers.deserialize('json', p_config): obj.save() reload_config() content = {"flag": "Success"} except Exception as e: content = {"flag": "Error", "content": str(e)} return HttpResponse(json.dumps(content))
content = {"flag": "Success", "context": config} except Exception, e: content = {"flag": "Error", "context": str(e)} elif action == "import": try: post = json.loads(request.body) m_config = post['main_config'] u_config = post['upstream_config'] p_config = post['proxy_config'] main_config_qc.delete() upstream_config_qc.delete() proxy_config_qc.delete() for obj in serializers.deserialize("json", m_config): obj.save() for obj in serializers.deserialize("json", u_config): obj.save() for obj in serializers.deserialize("json", p_config): obj.save() reload_config() content = {"flag": "Success"} except Exception, e: content = {"flag": "Error", "context": str(e)} return HttpResponse(json.dumps(content))
def import_config(config): try: main_config_qc = main_config.objects.all() system_config_qc = system_settings.objects.all() proxy_config_qc = proxy_config.objects.all() upstream_config_qc = upstream_config.objects.all() m_config = config['main_config'] s_config = config['system_config'] p_config = config['proxy_config'] u_config = config['upstream_config'] config_count = 0 if m_config.get('config', False): if hashlib.sha1(serializers.serialize( 'json', main_config_qc)).hexdigest() == m_config.get('sha1'): logger.info('main config no change!') else: if hashlib.sha1(m_config.get( 'config')).hexdigest() == m_config.get('sha1'): logger.info('import main config started...') main_config_qc.delete() for obj in serializers.deserialize("json", m_config.get('config')): obj.save() reload_config("main") logger.info('import main config finished!') config_count += 1 else: return False if s_config.get('config', False): if hashlib.sha1(serializers.serialize( 'json', system_config_qc)).hexdigest() == s_config.get('sha1'): logger.info('system config no change!') else: if hashlib.sha1(s_config.get( 'config')).hexdigest() == s_config.get('sha1'): logger.info('import system config started...') system_config_qc.delete() for obj in serializers.deserialize("json", s_config.get('config')): obj.save() logger.info('import system config finished!') config_count += 1 else: return False if p_config.get('config', False) and u_config.get('config', False): if hashlib.sha1(serializers.serialize( 'json', proxy_config_qc)).hexdigest() == p_config.get( 'sha1') and hashlib.sha1( serializers.serialize('json', upstream_config_qc) ).hexdigest() == u_config.get('sha1'): logger.info('proxy config and upstream config no change!') else: if hashlib.sha1( p_config.get('config')).hexdigest() == p_config.get( 'sha1') and hashlib.sha1(u_config.get( 'config')).hexdigest() == u_config.get('sha1'): logger.info('import upstream config started...') upstream_config_qc.delete() for obj in serializers.deserialize("json", u_config.get('config')): obj.save() logger.info('import upstream config finished!') logger.info('import proxy config started...') proxy_config_qc.delete() for obj in serializers.deserialize("json", p_config.get('config')): obj.save() logger.info('import proxy config finished!') reload_config("proxy") config_count += 1 else: return False if config_count: logger.info('config import finished.') else: logger.info('all config no change! config import finished.') return True except Exception, e: logger.error(str(e)) return False
content = { "flag":"Success", "context": config } except Exception,e: content = { "flag":"Error","context":str(e) } elif action == "import": try: post = json.loads(request.body) m_config = post['main_config'] u_config = post['upstream_config'] p_config = post['proxy_config'] main_config_qc.delete() upstream_config_qc.delete() proxy_config_qc.delete() for obj in serializers.deserialize("json", m_config): obj.save() for obj in serializers.deserialize("json", u_config): obj.save() for obj in serializers.deserialize("json", p_config): obj.save() reload_config() content = { "flag":"Success" } except Exception,e: content = { "flag":"Error","context":str(e) } return HttpResponse(json.dumps(content))
2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home') Including another URLconf 1. Import the include() function: from django.conf.urls import url, include 2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls')) """ from django.conf.urls import url, include from django.contrib.staticfiles.urls import staticfiles_urlpatterns from django.views.generic.base import RedirectView from django.conf import settings from .views import logout_view, login_view, create_superuser import os if os.path.exists(settings.BASE_DIR + '/db/' + 'db.sqlite3'): try: from nginx.views import reload_config reload_config("main") print("reload main config ok") reload_config("proxy") print("reload proxy config ok") except: print("reload config error") urlpatterns = [ url(r'^login/$', login_view), url(r'^logout/$', logout_view), url(r'^superuser/$', create_superuser), url(r'^dashboard/', include('dashboard.urls')), url(r'^main/', include('main.urls')), url(r'^proxy/', include('proxy.urls')), url(r'^settings/', include('settings.urls')), url(r'^api/', include('api.urls')),