Added simplequery functions
This commit is contained in:
parent
73f15ac4dc
commit
8c6f9252c7
5 changed files with 214 additions and 105 deletions
27
parameciofast/libraries/db/simplequery.py
Normal file
27
parameciofast/libraries/db/simplequery.py
Normal file
|
|
@ -0,0 +1,27 @@
|
||||||
|
|
||||||
|
# A more simple set for make queries
|
||||||
|
|
||||||
|
def insert(model, dict_values, db):
|
||||||
|
|
||||||
|
final_values={}
|
||||||
|
|
||||||
|
for k in model.fields.keys():
|
||||||
|
final_values[k]=model.fields[k].check(dict_values.get(k, ''))
|
||||||
|
|
||||||
|
del final_values[model.name_field_id]
|
||||||
|
|
||||||
|
str_fields="`"+"`, `".join(final_values.keys())+"`"
|
||||||
|
|
||||||
|
str_query='insert into {} ({}) VALUES ({})'.format(model.name, str_fields, ", ".join(['%s']*len(final_values)))
|
||||||
|
|
||||||
|
success=False
|
||||||
|
|
||||||
|
with db.query(str_query, list(final_values.values())) as cursor:
|
||||||
|
|
||||||
|
if cursor.rowcount>0:
|
||||||
|
|
||||||
|
model.last_id=cursor.lastrowid
|
||||||
|
success=True
|
||||||
|
|
||||||
|
return success
|
||||||
|
|
||||||
|
|
@ -1717,3 +1717,4 @@ class QueryModel(WebModel):
|
||||||
self.order_by="ORDER BY "+self.field_quote+self.name+self.field_quote+"."+self.field_quote+"id"+self.field_quote+" ASC"
|
self.order_by="ORDER BY "+self.field_quote+self.name+self.field_quote+"."+self.field_quote+"id"+self.field_quote+" ASC"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -9,11 +9,15 @@ from pydantic import BaseModel, Field
|
||||||
from parameciofast.modules.fastadmin.models.admin import UserAdmin
|
from parameciofast.modules.fastadmin.models.admin import UserAdmin
|
||||||
from parameciofast.libraries.db.webmodel import WebModel
|
from parameciofast.libraries.db.webmodel import WebModel
|
||||||
from parameciofast.libraries.fastutils import ResponseData
|
from parameciofast.libraries.fastutils import ResponseData
|
||||||
|
from parameciofast.libraries.db import simplequery
|
||||||
|
|
||||||
env=env_theme(__file__)
|
env=env_theme(__file__)
|
||||||
t=PTemplate(env, app.url_path_for)
|
t=PTemplate(env, app.url_path_for)
|
||||||
|
|
||||||
useradmin=UserAdmin()
|
usermodel=UserAdmin()
|
||||||
|
#useradmin.create_forms()
|
||||||
|
|
||||||
|
#useradmin.safe_query=True
|
||||||
|
|
||||||
@admin_app.get('/', response_class=HTMLResponse)
|
@admin_app.get('/', response_class=HTMLResponse)
|
||||||
def home_admin(request: Request, paramecio_session: Annotated[str | None, Cookie(description='Cookie for validate into the admin site. The cookie name can change in you settings/config.py')] = None):
|
def home_admin(request: Request, paramecio_session: Annotated[str | None, Cookie(description='Cookie for validate into the admin site. The cookie name can change in you settings/config.py')] = None):
|
||||||
|
|
@ -50,7 +54,7 @@ def signup_admin(request: Request):
|
||||||
num_users=cursor.fetchone()['num_users']
|
num_users=cursor.fetchone()['num_users']
|
||||||
|
|
||||||
if num_users>0:
|
if num_users>0:
|
||||||
return RedirectResponse(app.url_path_for('signup_admin'))
|
return RedirectResponse(app.url_path_for('login_admin'))
|
||||||
|
|
||||||
db.close()
|
db.close()
|
||||||
|
|
||||||
|
|
@ -81,7 +85,7 @@ def check_login_admin(user: UserAdmin, request: Request) -> ResponseData:
|
||||||
|
|
||||||
if result:
|
if result:
|
||||||
|
|
||||||
if useradmin.fields['password'].verify(user.password, result['password']):
|
if usermodel.fields['password'].verify(user.password, result['password']):
|
||||||
|
|
||||||
request.session['login_admin']=True
|
request.session['login_admin']=True
|
||||||
error=0
|
error=0
|
||||||
|
|
@ -101,17 +105,17 @@ class UserSignup(BaseModel):
|
||||||
"json_schema_extra": {
|
"json_schema_extra": {
|
||||||
"examples": [
|
"examples": [
|
||||||
{
|
{
|
||||||
"username": "johnny",
|
"username": "johnny5",
|
||||||
"email": "trial@example.com",
|
"email": "trial@example.com",
|
||||||
"password": "anrandompasswordthatineverused",
|
"password": "arandompasswordthatineverused",
|
||||||
"repeat_password": "anrandompasswordthatineverused",
|
"repeat_password": "arandompasswordthatineverused",
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@admin_app.post('/signup')
|
@admin_app.post('/signup')
|
||||||
def signup_insert_admin(user: Annotated[UserSignup, Body(embed=True)], request: Request) -> ResponseData:
|
def signup_insert_admin(user: UserSignup, request: Request) -> ResponseData:
|
||||||
|
|
||||||
i18n=I18n('admin', I18n.session_lang(request.session))
|
i18n=I18n('admin', I18n.session_lang(request.session))
|
||||||
|
|
||||||
|
|
@ -127,10 +131,16 @@ def signup_insert_admin(user: Annotated[UserSignup, Body(embed=True)], request:
|
||||||
num_users=cursor.fetchone()['num_users']
|
num_users=cursor.fetchone()['num_users']
|
||||||
|
|
||||||
if num_users:
|
if num_users:
|
||||||
return RedirectResponse(app.url_path_for('login_admin'))
|
message="You cannot add new users from here"
|
||||||
else:
|
else:
|
||||||
|
error=0
|
||||||
|
|
||||||
|
if not error:
|
||||||
|
|
||||||
|
if simplequery.insert(usermodel, dict(user), db):
|
||||||
|
error=0
|
||||||
|
message="User added!"
|
||||||
|
|
||||||
pass
|
|
||||||
|
|
||||||
db.close()
|
db.close()
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -28,17 +28,20 @@
|
||||||
${tlang('Login')}
|
${tlang('Login')}
|
||||||
</div>
|
</div>
|
||||||
<div class="card-body">
|
<div class="card-body">
|
||||||
<form method="POST" action="${url_for('login_admin')}" id="login_form">
|
<form method="POST" action="${url_for('login_admin')}" id="login_form" class="needs-validation" novalidate>
|
||||||
<div class="mb-3">
|
<div class="mb-3">
|
||||||
<label for="username_form" class="form-label">${tlang('Username')}</label>
|
<label for="username_form" class="form-label">${tlang('Username')}</label>
|
||||||
<input type="text" class="form-control form-control-lg" id="username_form" name="password" aria-describedby="username" autocomplete="off">
|
<input type="text" class="form-control form-control-lg has-validation" id="username_form" name="password" aria-describedby="username" autocomplete="off" required>
|
||||||
<div class="invalid-feedback">
|
<div class="invalid-feedback">
|
||||||
${tlang('You need a valid username and password')}
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="mb-3">
|
<div class="mb-3">
|
||||||
<label for="password_form" class="form-label">Password</label>
|
<label for="password_form" class="form-label">Password</label>
|
||||||
<input type="password" class="form-control form-control-lg" id="password_form" name="password" autocomplete="off">
|
<input type="password" class="form-control form-control-lg has-validation" id="password_form" name="password" autocomplete="off" required>
|
||||||
|
<div class="invalid-feedback">
|
||||||
|
${tlang('You need a valid username and password')}
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="mb-3 form-check">
|
<div class="mb-3 form-check">
|
||||||
<input type="checkbox" class="form-check-input" id="autologin" name="autologin">
|
<input type="checkbox" class="form-check-input" id="autologin" name="autologin">
|
||||||
|
|
@ -71,69 +74,74 @@
|
||||||
|
|
||||||
$(document).ready( function () {
|
$(document).ready( function () {
|
||||||
|
|
||||||
$('#login_form').submit( function () {
|
$('#login_form').submit( function (event) {
|
||||||
|
|
||||||
$('#loader-div').show();
|
$('#username_form').get(0).setCustomValidity("");
|
||||||
|
$('#password_form').get(0).setCustomValidity("");
|
||||||
|
|
||||||
$('#login_submit').prop('disabled', true);
|
form=document.getElementById('login_form');
|
||||||
|
|
||||||
data_form={'username': $('#username_form').val(), 'password': $('#password_form').val(), 'csrf_token': $("#csrf_token").val(), 'remember_login': 0};
|
error=false;
|
||||||
|
|
||||||
$.ajax({
|
if (!form.checkValidity()) {
|
||||||
url: "${url_for('check_login_admin')}",
|
|
||||||
method: "POST",
|
|
||||||
dataType: "json",
|
|
||||||
contentType : 'application/json',
|
|
||||||
data: JSON.stringify(data_form)
|
|
||||||
}).done(function(data) {
|
|
||||||
|
|
||||||
if(data.error==0)
|
error=true;
|
||||||
{
|
|
||||||
|
|
||||||
//location.reload()
|
}
|
||||||
location.href="${url_for('home_admin')}";
|
|
||||||
|
|
||||||
}
|
event.preventDefault();
|
||||||
else
|
event.stopPropagation();
|
||||||
{
|
|
||||||
|
|
||||||
$('#login_submit').prop('disabled', false);
|
form.classList.add('was-validated');
|
||||||
|
|
||||||
$('#loader-div').hide();
|
if(!error) {
|
||||||
|
|
||||||
// Firefox have a horrible and stupid bug and you need attr for set de new csrf_token
|
$('#loader-div').show();
|
||||||
|
|
||||||
/*$('#csrf_token').attr('value', data.csrf_token);
|
$('#login_submit').prop('disabled', true);
|
||||||
|
|
||||||
$('#loading').hide('slow');
|
data_form={'username': $('#username_form').val(), 'password': $('#password_form').val(), 'csrf_token': $("#csrf_token").val(), 'remember_login': 0};
|
||||||
|
|
||||||
if(data.hasOwnProperty('disable')) {
|
$.ajax({
|
||||||
|
url: "${url_for('check_login_admin')}",
|
||||||
|
method: "POST",
|
||||||
|
dataType: "json",
|
||||||
|
contentType : 'application/json',
|
||||||
|
data: JSON.stringify(data_form),
|
||||||
|
error: function (data) {
|
||||||
|
|
||||||
$('#username_error').html("${_('Error, your user is disabled, you need support of web administration')}");
|
console.log(JSON.stringify(data));
|
||||||
|
$('#loader-div').hide();
|
||||||
|
$('#login_submit').prop('disabled', false);
|
||||||
|
|
||||||
} if(data.hasOwnProperty('you_cannot_login')) {
|
alert('${tlang("Error: please, try again later")}');
|
||||||
|
|
||||||
if(data.you_cannot_login) {
|
},
|
||||||
|
}).done(function(data) {
|
||||||
|
|
||||||
$('#username_error').html("${_('Error, excessive tries, wait some minutes for login again')}");
|
if(data.error==0)
|
||||||
|
{
|
||||||
|
|
||||||
}
|
//location.reload()
|
||||||
else {
|
location.href="${url_for('home_admin')}";
|
||||||
|
|
||||||
$('#username_error').html("${_('Error, wrong username or password')}");
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
else {
|
else
|
||||||
|
{
|
||||||
|
|
||||||
$('#username_error').html("${_('Error, wrong username or password')}");
|
$('#username_form').get(0).setCustomValidity("${tlang('Error: username or password invalid')}");
|
||||||
|
$('#password_form').get(0).setCustomValidity("${tlang('Error: username or password invalid')}");
|
||||||
|
|
||||||
}*/
|
$('#login_submit').prop('disabled', false);
|
||||||
|
|
||||||
}
|
$('#loader-div').hide();
|
||||||
|
|
||||||
});
|
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,33 +5,33 @@
|
||||||
${tlang('Signup')}
|
${tlang('Signup')}
|
||||||
</div>
|
</div>
|
||||||
<div class="card-body">
|
<div class="card-body">
|
||||||
<form method="POST" action="${url_for('signup_insert_admin')}" id="login_form">
|
<form method="POST" action="${url_for('signup_insert_admin')}" id="login_form" class="needs-validation" novalidate>
|
||||||
<div class="mb-3">
|
<div class="mb-3">
|
||||||
<label for="username_form" class="form-label">${tlang('Username')}*</label>
|
<label for="username_form" class="form-label">${tlang('Username')}*</label>
|
||||||
<input type="text" class="form-control form-control-lg" id="username_form" name="password" aria-describedby="username" autocomplete="off">
|
<input type="text" class="form-control form-control-lg has-validation" id="username_form" name="password" aria-describedby="username" autocomplete="off" minlength="4" pattern="\w{4,32}" required>
|
||||||
<div class="invalid-feedback">
|
<div class="invalid-feedback">
|
||||||
${tlang('You need a valid username')}
|
${tlang('You need a valid username')}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="mb-3">
|
<div class="mb-3">
|
||||||
<label for="email_form" class="form-label">${tlang('Email')}*</label>
|
<label for="email_form" class="form-label">${tlang('Email')}*</label>
|
||||||
<input type="email" class="form-control form-control-lg" id="email_form" name="email" autocomplete="off">
|
<input type="email" class="form-control form-control-lg has-validation" id="email_form" name="email" autocomplete="off" minlength="4" required>
|
||||||
<div class="invalid-feedback">
|
<div class="invalid-feedback">
|
||||||
${tlang('You need an email')}
|
${tlang('You need an email')}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="mb-3">
|
<div class="mb-3">
|
||||||
<label for="password_form" class="form-label">Password*</label>
|
<label for="password_form" class="form-label">Password*</label>
|
||||||
<input type="password" class="form-control form-control-lg" id="password_form" name="password" autocomplete="off">
|
<input type="password" class="form-control form-control-lg has-validation" id="password_form" name="password" autocomplete="off" minlength="2" required>
|
||||||
<div class="invalid-feedback">
|
<div class="invalid-feedback">
|
||||||
${tlang('You need a password')}
|
${tlang('You need a password')}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="mb-3">
|
<div class="mb-3">
|
||||||
<label for="repeat_password_form" class="form-label">${tlang('Repeat password')}*</label>
|
<label for="repeat_password_form" class="form-label">${tlang('Repeat password')}*</label>
|
||||||
<input type="repeat_password" class="form-control form-control-lg" id="repeat_password_form" name="repeat_password" autocomplete="off">
|
<input type="password" class="form-control form-control-lg has-validation" id="repeat_password_form" name="repeat_password" autocomplete="off" minlength="2" required>
|
||||||
<div class="invalid-feedback">
|
<div class="invalid-feedback">
|
||||||
${tlang('Password not equal')}
|
${tlang('You need the same password in this field and not empty')}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<button type="submit" id="login_submit" class="btn btn-primary">${tlang('Create user')}</button>
|
<button type="submit" id="login_submit" class="btn btn-primary">${tlang('Create user')}</button>
|
||||||
|
|
@ -42,7 +42,100 @@
|
||||||
<script>
|
<script>
|
||||||
$(document).ready( function () {
|
$(document).ready( function () {
|
||||||
|
|
||||||
$('#login_form').submit( function () {
|
$('#repeat_password_form').on('input',function(e){
|
||||||
|
|
||||||
|
if($('#repeat_password_form').val()!=$('#password_form').val()) {
|
||||||
|
|
||||||
|
console.log('No match');
|
||||||
|
|
||||||
|
$('#repeat_password_form').get(0).setCustomValidity("${tlang('Passwords doesn\'t match')}");
|
||||||
|
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
|
||||||
|
$('#repeat_password_form').get(0).setCustomValidity("");
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
// Fetch all the forms we want to apply custom Bootstrap validation styles to
|
||||||
|
var forms = document.querySelectorAll('.needs-validation');
|
||||||
|
var error=false;
|
||||||
|
|
||||||
|
// Loop over them and prevent submission
|
||||||
|
Array.prototype.slice.call(forms)
|
||||||
|
.forEach(function (form) {
|
||||||
|
form.addEventListener('submit', function (event) {
|
||||||
|
|
||||||
|
error=false;
|
||||||
|
|
||||||
|
if (!form.checkValidity()) {
|
||||||
|
|
||||||
|
error=true;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
event.preventDefault();
|
||||||
|
event.stopPropagation();
|
||||||
|
|
||||||
|
form.classList.add('was-validated');
|
||||||
|
|
||||||
|
var form_data=new FormData(form);
|
||||||
|
|
||||||
|
if(!error) {
|
||||||
|
|
||||||
|
console.log('send submit');
|
||||||
|
|
||||||
|
$('#loader-div').show();
|
||||||
|
|
||||||
|
$('#login_submit').prop('disabled', true);
|
||||||
|
|
||||||
|
data_form={'username': $('#username_form').val(), 'email': $('#email_form').val(), 'password': $('#password_form').val(), 'repeat_password': $('#repeat_password_form').val(), 'csrf_token': $("#csrf_token").val()};
|
||||||
|
|
||||||
|
$.ajax({
|
||||||
|
url: "${url_for('signup_insert_admin')}",
|
||||||
|
method: "POST",
|
||||||
|
dataType: "json",
|
||||||
|
contentType : 'application/json',
|
||||||
|
data: JSON.stringify(data_form),
|
||||||
|
error: function (data) {
|
||||||
|
|
||||||
|
console.log(JSON.stringify(data));
|
||||||
|
$('#loader-div').hide();
|
||||||
|
$('#login_submit').prop('disabled', false);
|
||||||
|
|
||||||
|
alert('${tlang("Error: please, try again later")}');
|
||||||
|
|
||||||
|
},
|
||||||
|
}).done(function(data) {
|
||||||
|
|
||||||
|
if(data.error==0)
|
||||||
|
{
|
||||||
|
|
||||||
|
//location.reload()
|
||||||
|
location.href="${url_for('login_admin')}";
|
||||||
|
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
|
||||||
|
$('#login_submit').prop('disabled', false);
|
||||||
|
|
||||||
|
$('#loader-div').hide();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
/*$('#login_form').submit( function () {
|
||||||
|
|
||||||
$('#loader-div').show();
|
$('#loader-div').show();
|
||||||
|
|
||||||
|
|
@ -79,43 +172,13 @@ $(document).ready( function () {
|
||||||
|
|
||||||
$('#loader-div').hide();
|
$('#loader-div').hide();
|
||||||
|
|
||||||
// Firefox have a horrible and stupid bug and you need attr for set de new csrf_token
|
|
||||||
|
|
||||||
/*$('#csrf_token').attr('value', data.csrf_token);
|
|
||||||
|
|
||||||
$('#loading').hide('slow');
|
|
||||||
|
|
||||||
if(data.hasOwnProperty('disable')) {
|
|
||||||
|
|
||||||
$('#username_error').html("${_('Error, your user is disabled, you need support of web administration')}");
|
|
||||||
|
|
||||||
} if(data.hasOwnProperty('you_cannot_login')) {
|
|
||||||
|
|
||||||
if(data.you_cannot_login) {
|
|
||||||
|
|
||||||
$('#username_error').html("${_('Error, excessive tries, wait some minutes for login again')}");
|
|
||||||
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
|
|
||||||
$('#username_error').html("${_('Error, wrong username or password')}");
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
|
|
||||||
$('#username_error').html("${_('Error, wrong username or password')}");
|
|
||||||
|
|
||||||
}*/
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
});
|
});*/
|
||||||
|
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue