def add(request, dbtype): """ DB create """ u = request.session.get('switched_user', request.user) superuser = request.user form_class = MysqlForm if dbtype == 'mysql' else PgsqlForm if request.method == 'POST': form = form_class(request.POST) orig_dbname = form.data['dbname'] old = form.data form.data._mutable = True form.data['dbname'] = "%s_%s" % (u.parms.prefix(), form.data['dbname']) form.data._mutable = old if form.is_valid(): db_obj = form.save(commit=False) db_obj.owner = u db_obj.save() if dbtype == 'mysql': mr = MySQLRequest(u, u.parms.mysql_machine) elif dbtype == 'pgsql': mr = PostgreSQLRequest(u, u.parms.pgsql_machine) else: return HttpResponseBadRequest(_('Unknown database type')) mr.add_db(db_obj.dbname, form.cleaned_data["password1"]) return HttpResponseRedirect(reverse("db_list", kwargs=dict(dbtype=dbtype))) else: form.data['dbname'] = orig_dbname else: form = form_class() form.owner = u helper = RostiFormHelper() helper.form_action = reverse("db_add", kwargs=dict(dbtype=dbtype)) return render_to_response('universal.html', { "form": form, "form_helper": RostiFormHelper(), "title": _("Create %s database" % dbtype), "u": u, "superuser": superuser, "menu_active": "dbs", }, context_instance=RequestContext(request) )
def add(request, dbtype): """ DB create """ u = request.session.get('switched_user', request.user) superuser = request.user form_class = MysqlForm if dbtype == 'mysql' else PgsqlForm if request.method == 'POST': form = form_class(request.POST) orig_dbname = form.data['dbname'] old = form.data form.data._mutable = True form.data['dbname'] = "%s_%s" % (u.parms.prefix(), form.data['dbname']) form.data._mutable = old if form.is_valid(): db_obj = form.save(commit=False) db_obj.owner = u db_obj.save() if dbtype == 'mysql': mr = MySQLRequest(u, u.parms.mysql_machine) elif dbtype == 'pgsql': mr = PostgreSQLRequest(u, u.parms.pgsql_machine) else: return HttpResponseBadRequest(_('Unknown database type')) mr.add_db(db_obj.dbname, form.cleaned_data["password1"]) return HttpResponseRedirect( reverse("db_list", kwargs=dict(dbtype=dbtype))) else: form.data['dbname'] = orig_dbname else: form = form_class() form.owner = u helper = RostiFormHelper() helper.form_action = reverse("db_add", kwargs=dict(dbtype=dbtype)) return render_to_response('universal.html', { "form": form, "form_helper": RostiFormHelper(), "title": _("Create %s database" % dbtype), "u": u, "superuser": superuser, "menu_active": "dbs", }, context_instance=RequestContext(request))
def passwd(request, dbtype, dbname): u = request.session.get('switched_user', request.user) superuser = request.user if request.method == 'POST': form = PassCheckForm(request.POST) if form.is_valid(): if dbtype == 'mysql': #TODO - raise better exception try: u.mysqldb_set.get(dbname=dbname) except MySQLDB.DoesNotExist: return HttpResponseForbidden( ugettext("Unable to modify chosen database")) else: mr = MySQLRequest(u, u.parms.mysql_machine) elif dbtype == 'pgsql': try: u.pgsql_set.get(dbname=dbname) except PGSQL.DoesNotExist: return HttpResponseForbidden( ugettext("Unable to modify chosen database")) else: mr = PostgreSQLRequest(u, u.parms.pgsql_machine) else: return HttpResponseBadRequest(_('Unknown database type')) mr.passwd_db(dbname, form.cleaned_data["password1"]) messages.add_message(request, messages.SUCCESS, _('Password has been changed')) return HttpResponseRedirect( reverse('db_list', kwargs=dict(dbtype=dbtype))) else: form = PassCheckForm() return render_to_response('simplepasswd.html', { "form": form, "form_helper": RostiFormHelper(), "title": _("Change password for database"), "u": u, "superuser": superuser, "menu_active": "dbs", }, context_instance=RequestContext(request))
def passwd(request, dbtype, dbname): u = request.session.get('switched_user', request.user) superuser = request.user if request.method == 'POST': form = PassCheckForm(request.POST) if form.is_valid(): if dbtype == 'mysql': #TODO - raise better exception try: u.mysqldb_set.get(dbname=dbname) except MySQLDB.DoesNotExist: return HttpResponseForbidden(ugettext("Unable to modify chosen database")) else: mr = MySQLRequest(u, u.parms.mysql_machine) elif dbtype == 'pgsql': try: u.pgsql_set.get(dbname=dbname) except PGSQL.DoesNotExist: return HttpResponseForbidden(ugettext("Unable to modify chosen database")) else: mr = PostgreSQLRequest(u, u.parms.pgsql_machine) else: return HttpResponseBadRequest(_('Unknown database type')) mr.passwd_db(dbname, form.cleaned_data["password1"]) messages.add_message(request, messages.SUCCESS, _('Password has been changed')) return HttpResponseRedirect(reverse('db_list', kwargs=dict(dbtype=dbtype))) else: form = PassCheckForm() return render_to_response('simplepasswd.html', { "form": form, "form_helper": RostiFormHelper(), "title": _("Change password for database"), "u": u, "superuser": superuser, "menu_active": "dbs", }, context_instance=RequestContext(request) )
def rm(request, dbtype): try: object_id = request.POST['object_id'] u = request.session.get('switched_user', request.user) if dbtype == 'mysql': m = u.mysqldb_set.get(pk=object_id) mr = MySQLRequest(u, u.parms.mysql_machine) elif dbtype == 'pgsql': m = u.pgsql_set.get(pk=object_id) mr = PostgreSQLRequest(u, u.parms.pgsql_machine) else: raise Exception(ugettext('Unknown database type')) mr.remove_db(m.dbname) m.delete() return JsonResponse("OK", {1: ugettext("Database was successfuly deleted")}) except Exception, e: return JsonResponse("KO", {1: ugettext("Error during delete database")})