Advertisement
Guest User

Untitled

a guest
Jul 21st, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 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.py':
  5. full_path = os.path.join(path_to_store_settings, fname)
  6. remove(full_path)
  7.  
  8. database_id = 'bdnueva' # 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': 'ODBC Driver 13 for SQL Server'}
  27. } }
  28. """ % newDatabase
  29.  
  30. file_to_store_settings = os.path.join(path_to_store_settings, 'conexion' + ".py")
  31. conexion = open(file_to_store_settings, 'w')
  32. conexion.write(newDbString)
  33. conexion.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement