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"
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -9,11 +9,15 @@ from pydantic import BaseModel, Field
|
|||
from parameciofast.modules.fastadmin.models.admin import UserAdmin
|
||||
from parameciofast.libraries.db.webmodel import WebModel
|
||||
from parameciofast.libraries.fastutils import ResponseData
|
||||
from parameciofast.libraries.db import simplequery
|
||||
|
||||
env=env_theme(__file__)
|
||||
t=PTemplate(env, app.url_path_for)
|
||||
|
||||
useradmin=UserAdmin()
|
||||
usermodel=UserAdmin()
|
||||
#useradmin.create_forms()
|
||||
|
||||
#useradmin.safe_query=True
|
||||
|
||||
@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):
|
||||
|
|
@ -50,7 +54,7 @@ def signup_admin(request: Request):
|
|||
num_users=cursor.fetchone()['num_users']
|
||||
|
||||
if num_users>0:
|
||||
return RedirectResponse(app.url_path_for('signup_admin'))
|
||||
return RedirectResponse(app.url_path_for('login_admin'))
|
||||
|
||||
db.close()
|
||||
|
||||
|
|
@ -81,7 +85,7 @@ def check_login_admin(user: UserAdmin, request: Request) -> ResponseData:
|
|||
|
||||
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
|
||||
error=0
|
||||
|
|
@ -101,17 +105,17 @@ class UserSignup(BaseModel):
|
|||
"json_schema_extra": {
|
||||
"examples": [
|
||||
{
|
||||
"username": "johnny",
|
||||
"username": "johnny5",
|
||||
"email": "trial@example.com",
|
||||
"password": "anrandompasswordthatineverused",
|
||||
"repeat_password": "anrandompasswordthatineverused",
|
||||
"password": "arandompasswordthatineverused",
|
||||
"repeat_password": "arandompasswordthatineverused",
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
@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))
|
||||
|
||||
|
|
@ -127,10 +131,16 @@ def signup_insert_admin(user: Annotated[UserSignup, Body(embed=True)], request:
|
|||
num_users=cursor.fetchone()['num_users']
|
||||
|
||||
if num_users:
|
||||
return RedirectResponse(app.url_path_for('login_admin'))
|
||||
message="You cannot add new users from here"
|
||||
else:
|
||||
error=0
|
||||
|
||||
if not error:
|
||||
|
||||
if simplequery.insert(usermodel, dict(user), db):
|
||||
error=0
|
||||
message="User added!"
|
||||
|
||||
pass
|
||||
|
||||
db.close()
|
||||
|
||||
|
|
|
|||
|
|
@ -28,17 +28,20 @@
|
|||
${tlang('Login')}
|
||||
</div>
|
||||
<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">
|
||||
<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">
|
||||
${tlang('You need a valid username and password')}
|
||||
|
||||
</div>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<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 class="mb-3 form-check">
|
||||
<input type="checkbox" class="form-check-input" id="autologin" name="autologin">
|
||||
|
|
@ -71,7 +74,27 @@
|
|||
|
||||
$(document).ready( function () {
|
||||
|
||||
$('#login_form').submit( function () {
|
||||
$('#login_form').submit( function (event) {
|
||||
|
||||
$('#username_form').get(0).setCustomValidity("");
|
||||
$('#password_form').get(0).setCustomValidity("");
|
||||
|
||||
form=document.getElementById('login_form');
|
||||
|
||||
error=false;
|
||||
|
||||
if (!form.checkValidity()) {
|
||||
|
||||
error=true;
|
||||
|
||||
}
|
||||
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
|
||||
form.classList.add('was-validated');
|
||||
|
||||
if(!error) {
|
||||
|
||||
$('#loader-div').show();
|
||||
|
||||
|
|
@ -84,7 +107,16 @@
|
|||
method: "POST",
|
||||
dataType: "json",
|
||||
contentType : 'application/json',
|
||||
data: JSON.stringify(data_form)
|
||||
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)
|
||||
|
|
@ -97,44 +129,20 @@
|
|||
else
|
||||
{
|
||||
|
||||
$('#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();
|
||||
|
||||
// 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;
|
||||
|
||||
});
|
||||
|
|
|
|||
|
|
@ -5,33 +5,33 @@
|
|||
${tlang('Signup')}
|
||||
</div>
|
||||
<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">
|
||||
<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">
|
||||
${tlang('You need a valid username')}
|
||||
</div>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<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">
|
||||
${tlang('You need an email')}
|
||||
</div>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<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">
|
||||
${tlang('You need a password')}
|
||||
</div>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<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">
|
||||
${tlang('Password not equal')}
|
||||
${tlang('You need the same password in this field and not empty')}
|
||||
</div>
|
||||
</div>
|
||||
<button type="submit" id="login_submit" class="btn btn-primary">${tlang('Create user')}</button>
|
||||
|
|
@ -42,7 +42,100 @@
|
|||
<script>
|
||||
$(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();
|
||||
|
||||
|
|
@ -79,43 +172,13 @@ $(document).ready( function () {
|
|||
|
||||
$('#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;
|
||||
|
||||
});
|
||||
});*/
|
||||
|
||||
});
|
||||
</script>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue