Guest User

Untitled

a guest
Aug 15th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. def save_db_settings_to_file(form):
  2. path_to_store_settings = os.path.join(settings.BASE_DIR, 'SystemProyect')
  3. for fname in os.listdir(path_to_store_settings):
  4. if fname=='conexion.cfg':
  5. full_path = os.path.join(path_to_store_settings, fname)
  6. remove(full_path)
  7.  
  8. database_id = 'db_app2' # just something unique
  9. newDatabase = {}
  10. newDatabase["id"] = database_id
  11. newDatabase['ENGINE'] = 'sql_server.pyodbc'
  12. newDatabase['NAME'] = form.data['database']
  13. newDatabase['USER'] = form.data['usuario_sql']
  14. newDatabase['PASSWORD'] = form.data['password_sql']
  15. newDatabase['HOST'] = form.data['servidor_sql']
  16. newDatabase['PORT'] = ''
  17. settings.DATABASES[database_id] = newDatabase
  18.  
  19. newDbString = """DATABASES={'%(id)s':{
  20. 'ENGINE': '%(ENGINE)s', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
  21. 'NAME': '%(NAME)s', # Or path to database file if using sqlite3.
  22. 'USER': '%(USER)s', # Not used with sqlite3.
  23. 'PASSWORD': '%(PASSWORD)s', # Not used with sqlite3.
  24. 'HOST': '%(HOST)s', # Set to empty string for localhost. Not used with sqlite3.
  25. 'PORT': '', # Set to empty string for default. Not used with sqlite3.
  26. 'OPTIONS':"{'driver': 'SQL Server Native Client 10.0',}"
  27. } }
  28. """% newDatabase
  29. file_to_store_settings = os.path.join(path_to_store_settings, 'conexion' + ".py")
  30. salvar=open(file_to_store_settings,'w')
  31. salvar.write(newDbString)
  32. salvar.close()
  33.  
  34. import os
  35. path_to_store_settings = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'SystemProyect')
  36. for fname in os.listdir(path_to_store_settings):
  37. if fname == 'conexion.py':
  38. full_path = os.path.join(path_to_store_settings, fname)
  39. with open(full_path) as fobj:
  40. archivo_inicio = fobj.read()
  41. exec(archivo_inicio)
  42.  
  43. DATABASES = {
  44. 'versat': {
  45. 'ENGINE': 'sql_server.pyodbc', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
  46. 'NAME': 'eicma', # Or path to database file if using sqlite3.
  47. 'USER': 'tony', # Not used with sqlite3.
  48. 'PASSWORD': 'tony', # Not used with sqlite3.
  49. 'HOST': 'localhost', # Set to empty string for localhost. Not used with sqlite3.
  50. 'PORT': '', # Set to empty string for default. Not used with sqlite3.
  51. 'OPTIONS': "{'driver': 'SQL Server Native Client 10.0',}"
  52. }
  53. }
Add Comment
Please, Sign In to add comment