Added new files for modify items

This commit is contained in:
Antonio de la Rosa 2025-02-19 02:00:45 +01:00
parent eb6df1c9ae
commit e9f47b29e4
14 changed files with 570 additions and 10 deletions

View file

@ -0,0 +1,7 @@
def create_form(pmodel, t):
model=dict(pmodel)
return t.load_template('modelform.phtml', model=dict(pmodel))

View file

@ -49,14 +49,14 @@ class DateTimeField(PhangoField):
elif not datetime.obtain_timestamp(value):
self.error=True
self.txt_error=self.error_default+' '+value
self.txt_error=self.error_default+' '+str(value)
return '0000-00-00 00:00:00'
if value==False:
self.error=True
self.txt_error=self.error_default+' '+value
self.txt_error=self.error_default+' '+str(value)
return '0000-00-00 00:00:00'
else:

View file

@ -1,7 +1,7 @@
#!/usr/bin/env python3
from parameciofast.libraries.db.coreforms import BaseForm
#from parameciofast.libraries.mtemplates import standard_t
from parameciofast.libraries.mtemplates import standard_t
from parameciofast.libraries.datetime import format_timedata
class DateForm(BaseForm):
@ -11,7 +11,7 @@ class DateForm(BaseForm):
super().__init__(name, value)
self.yes_time=False
#self.t=standard_t
self.t=standard_t
def form(self):

View file

@ -0,0 +1,272 @@
#!/usr/bin/env python3
"""
Parameciofast is a series of wrappers for FastAPI, mako and others and construct a simple headless cms.
Copyright (C) 2025 Antonio de la Rosa Caballero
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
from parameciofast.libraries.db import corefields
from parameciofast.libraries.db.coreforms import PasswordForm
from parameciofast.libraries.i18n import I18n
from parameciofast.libraries.keyutils import create_key_encrypt
#import sqlite3
# For tests outing the web framework
try:
from settings import config
except:
class config:
pass
"""
framework='flask'
if hasattr(config, 'framework'):
framework=config.framework
if framework=='flask':
from flask import session, request, abort
elif framework=='fastapi':
from parameciofast.libraries.sessions import get_session
pass
"""
# Need unittest
"""Functions and classes for process forms"""
def pass_values_to_form(post, arr_form, yes_error=True, pass_values=True):
"""Function for pass a dict with form values for check using forms dict
Values dict and Forms dict need have the same key. A forms dict is maked of a serie of parameciofast.forms elements, used for check the value.
Args:
post (dict): Dict composed by a series of values. The keys need to be equal to keys of arr_form dict.
arr_form (dict): Dict composed by a series or forms objects. The keys need to be equal to keys of post dict.
yes_error (bool): Show errors in txt_error form variables.
pass_values (bool): Pass default values or values from post dict to arr_form dict items
Returns:
arr_form (dict): Return arr_form dict with checked values from post dict.
"""
if pass_values:
def get_value(key):
return post[key]
else:
def get_value(key):
return arr_form[key].default_value
for key, value in arr_form.items():
post[key]=post.get(key, '')
#if arr_form[key].default_value=='':
arr_form[key].default_value=get_value(key)
if arr_form[key].field==None:
arr_form[key].field=corefields.CharField(key, 255, required=False)
# Recheck value if no set error field
if arr_form[key].field.error == None:
arr_form[key].field.check(post[key])
#arr_form[key].txt_error=""
if arr_form[key].required==True and arr_form[key].field.error==True and yes_error==True:
arr_form[key].txt_error=arr_form[key].field.txt_error
# Reset error on field.
arr_form[key].field.error=None
return arr_form
class CheckForm():
"""Simple class for make similar check to pass_values_to_form. More simple.
"""
def __init__(self):
self.error=0
def check(self, post, arr_form):
"""Simple method for pass a dict with form values for check using forms dict
Values dict and Forms dict need have the same key. A forms dict is maked of a serie of parameciofast.forms elements, used for check the value.
Args:
post (dict): Dict composed by a series of values. The keys need to be equal to keys of arr_form dict.
arr_form (dict): Dict composed by a series or forms objects. The keys need to be equal to keys of post dict.
Returns:
post (dict): Return post dict with values checked
arr_form (dict): Return arr_form with errors and values.
"""
for k in arr_form.keys():
post[k]=post.get(k, '')
if arr_form[k].field==None:
arr_form[k].field=corefields.CharField(k, 255, required=False)
post[k]=arr_form[k].field.check(post[k])
arr_form[k].txt_error=arr_form[k].field.txt_error
if arr_form[k].field.error==True and arr_form[k].required==True:
self.error+=1
return post, arr_form
def check_form(post, arr_form, sufix_form='_error'):
"""Function for make check form, passing errors to extra dict called error_form. Also returns an bool variable setting error.
Args:
post (dict): Dict composed by a series of values. The keys need to be equal to keys of arr_form dict.
arr_form (dict): Dict composed by a series or forms objects. The keys need to be equal to keys of post dict.
sufix_form (str): Define the sufix of error_form keys
Returns:
error (bool): Return False if not errors in checking, if errors return True
error_form (dict): A dict containing the errors in form fields.
post (dict): Sanitized values
arr_form (dict): arr_form with errors and values.
"""
error=0
error_form={}
for k in arr_form.keys():
post[k]=post.get(k, '')
if arr_form[k].field==None:
arr_form[k].field=corefields.CharField(k, 255, required=False)
post[k]=arr_form[k].field.check(post[k])
arr_form[k].txt_error=arr_form[k].field.txt_error
if arr_form[k].field.error==True and arr_form[k].required==True:
error_form['#'+k+sufix_form]=arr_form[k].txt_error
error+=1
return error, error_form, post, arr_form
def show_form(post, arr_form, t, session, yes_error=True, pass_values=True, modelform_tpl='forms/modelform.phtml'):
"""Function for generate a html form from a template
Args:
post (dict): Dict composed by a series of values. The keys need to be equal to keys of arr_form dict.
arr_form (dict): Dict composed by a series of forms objects. The keys need to be equal to keys of post dict.
t (PTemplate): Object used for load template for form
yes_error (bool): Show errors in txt_error form variables.
pass_values (bool): Pass default values or values from post dict to arr_form dict items
modelform_tpl (str): Path for the template that generate the html form. By default is parameciofast/libraries/templates/forms/modelform.phtml
Returns:
template (str): An html string with the generated form.
"""
# Create csrf_token in session
generate_csrf(session)
if pass_values==True:
pass_values_to_form(post, arr_form, yes_error, pass_values)
return t.load_template(modelform_tpl, session=session, forms=arr_form, csrf_token=csrf_token)
def extract_post(post, fields):
"""Helper function for create a simple array from other using fields list for filter
Args:
post (dict): A dict with keys and values to filter.
fields (list): A list with keys to validate.
"""
return {k:v for k,v in post.items() if k in fields}
#Simple Function for add repeat_password form to user model
def set_extra_forms_user(user_admin):
"""Helper function for add extraforms to UserModel form, not for general use
Args:
user_admin (UserModel): Instance of UserModel object for modify forms and fields
"""
user_admin.fields['password'].required=True
user_admin.fields['email'].required=True
user_admin.create_forms(['username', 'email', 'password'])
user_admin.forms['repeat_password']=PasswordForm('repeat_password', '')
user_admin.forms['repeat_password'].required=True
user_admin.forms['repeat_password'].label=_('Repeat Password')
def csrf_token(session, token_id='csrf_token'):
"""Function for generate a csrf token html hide form using flask sessions
Args:
token_id (str): Name of the html hide form
Returns:
html (str): Return html input hidden with csrf token saved in session
"""
#s=get_session()
if not 'csrf_token' in session:
session['csrf_token']=create_key_encrypt()
return '<input type="hidden" name="csrf_token" class="csrf_token" id="'+token_id+'" value="'+session['csrf_token']+'" />'
def generate_csrf(session):
"""Function for generate a csrf token in a variable
Returns:
csrf_token (str): csrf token value
"""
if not 'csrf_token' in session:
session['csrf_token']=create_key_encrypt()
return session['csrf_token']
def check_csrf(session, name_csrf_token='csrf_token'):
csrf_token=session.get('csrf_token', '')
if csrf_token=='' or csrf_token!=request.form.get(name_csrf_token):
abort(403)

View file

@ -276,7 +276,7 @@ def add_css_home_local(file_css, module):
pass
#env=env_theme(__file__)
env=env_theme(__file__)
#standard_t=PTemplate(env)
standard_t=PTemplate(env)

View file

@ -0,0 +1,33 @@
${add_js_home_local('jquery.min.js', 'admin')}
${add_js_home_local('spectrum.js', 'admin')}
${add_css_home_local('spectrum.css', 'admin')}
<%
form.default_value=str(hex(int(form.default_value))).replace('0x', '')
c=len(form.default_value)
if(c<6):
repeat=6-c
form.default_value=('0'*repeat)+form.default_value
form.default_value='#'+form.default_value
%>
<input type='text' id="${form.name_field_id}_field_form" name="${form.name}" value="${form.default_value}" />
<script>
$("#${form.name_field_id}_field_form").spectrum({
allowEmpty: true
});
$("#${form.name_field_id}_field_form").closest('form').submit( function () {
var t=$("#${form.name_field_id}_field_form").spectrum("get");
$('#${form.name_field_id}_field_form').val(t.toHexString());
});
</script>

View file

@ -0,0 +1,71 @@
${add_js('jquery.min.js', 'admin')}
<input type="number" min="1" max="31" name="${form}_day" id="time_${form}_day" class="form_day" value="${d}" size="2" maxlength="2"/>
<input type="number" min="1" max="12" name="${form}_month" id="time_${form}_month" class="form_month" value="${m}" size="2" maxlength="2"/>
<input type="number" name="${form}_year" id="time_${form}_year" class="form_year" value="${y}" size="4" maxlength="4"/>
% if yes_time==True:
<input type="text" min="0" max="23" name="${form}_hour" id="time_${form}_hour" class="form_hour" value="${h}" size="2" maxlength="2"/>
<input type="text" min="0" max="60" name="${form}_minute" id="time_${form}_minute" class="form_minute" value="${min}" size="2" maxlength="2"/>
<input type="text" min="0" max="60" name="${form}_second" id="time_${form}_second" class="form_second" value="${s}" size="2" maxlength="2"/>
% endif
<input type="hidden" name="${form}" id="time_${form}" value="" />
<script language="javascript">
$(document).submit(function () {
year=$("#time_${form}_year").val().toString();
month=$("#time_${form}_month").val().toString();
day=$("#time_${form}_day").val().toString();
year=add_extra_length(year, 4);
month=add_extra_length(month, 2);
day=add_extra_length(day, 2);
final_time=year+month+day
% if yes_time==True:
hour=$("#time_${form}_hour").val().toString();
minute=$("#time_${form}_minute").val().toString();
second=$("#time_${form}_second").val().toString();
hour=add_extra_length(hour, 2);
minute=add_extra_length(minute, 2);
second=add_extra_length(second, 2);
final_time+=final_time;
% else:
final_time+='000000';
% endif
$("#time_${form}").val(final_time);
});
function add_extra_length(str_number, total_length)
{
if(str_number.length<total_length)
{
c=total_length-str_number.length;
extra_number='';
for(x=0;x<c;x++)
{
extra_number+='0';
}
str_number=extra_number+str_number;
}
return str_number;
}
</script>

View file

@ -0,0 +1,12 @@
<%
import os
name_file=os.path.basename(form.default_value)
%>
<input type="file" name="${form.name}_file" id="${form.name}_file_field_form" />
% if name_file:
${name_file}
% endif
<input type="hidden" name="${form.name}" id="${form.name}_field_form" value="${form.default_value}"/>

View file

@ -0,0 +1,73 @@
<%
choose=''
%>
<span id="${name_form}_languages_form" style="display:inline-block;">
<%def name="select_lang(i18n, lang_selected)">
% if i18n==lang_selected:
<%
return "choose_flag"
%>
% else:
<%
return "no_choose_flag"
%>
% endif
</%def>
<%def name="hide_lang(i18n, lang_selected)">
% if i18n!=lang_selected:
style="display:none;"
% endif
</%def>
<input type="hidden" name="${real_name_form}" id="${name_form}_i18n_value" value="" />
% if lang_selected!=None:
% for i18n in arr_i18n:
${form.change_name(name_form+'_'+i18n)}
<%
form.default_value=default_value[i18n]
%>
<span id="${name_form}_${i18n}_switch" class="${name_form}_i18n_form">${form.form()|n}</span> <a class="choose_flag ${name_form}_i18n_flag lang_form" id="${name_form}_${i18n}_element" href="#"><img src="${make_media_url('images/languages/'+i18n+'.png', 'admin')}" alt="${name_form}_${i18n}"></a>
% endfor
<i class="fa fa-globe all_languages tooltip" aria-hidden="true" style="display:none;cursor:pointer;" data-tooltip-content="#tooltip_${name_form}_content"></i>
<div class="tooltip_templates" style="display:none;"><div id="tooltip_${name_form}_content">${_('All lenguages activated, if you change to multilanguage, the default language will be ')}${lang_selected}</div></div>
% endif
% if len(arr_i18n)==1:
<script>
$("#${name_form}_languages_form").find('.${name_form}_i18n_flag').hide();
$("#${name_form}_languages_form").find('.all_languages').show();
</script>
% endif
</span>
<script>
//In submit convert in json
$(".${name_form}_i18n_flag").closest('form').submit( function () {
arr_i18n={}
$(".${name_form}_i18n_form").each(function () {
var lang=$(this).attr('id').replace('${name_form}_','').replace('_switch', '');
var child_input=$('#${name_form}_'+lang+'_form');
arr_i18n[lang]=child_input.val();
});
document.getElementById("${name_form}_i18n_value").value=JSON.stringify(arr_i18n);
return true;
});
</script>

View file

@ -0,0 +1,27 @@
<%def name="check_required(required)">
% if required:
${'*'} \
% endif
</%def>
<%def name="help(help, name)">
% if help:
<i class="fa fa-question-circle tooltip" data-tooltip-content="#tooltip_${name}_content" style="cursor:pointer;"></i>
<!--<div class="tooltip_templates" style="display:none;"><div id="tooltip_${name}_content">${help|n}</div></div>-->
% endif
</%def>
<%def name="help_tooltip(help, name)">
% if help:
<div class="tooltip_templates" style="display:none;"><div id="tooltip_${name}_content">${help|n}</div></div>
% endif
</%def>
<div class="form">
% for form in forms.values():
% if form.type!='hidden':
<p><label>${form.label} ${check_required(form.required)} ${help(form.help, form.name)}</label>${form.form()|n} <span class="error" id="${form.name}_error">${form.txt_error}</span></p>
${help_tooltip(form.help, form.name)}
% else:
${form.form()|n}
% endif
% endfor
${csrf_token(session)|n}
</div>

View file

@ -0,0 +1,22 @@
<p>
<textarea name="${form.name}" id="${form.name_field_id}">
${form.default_value}
</textarea>
</p>
<script>
tinymce.init({
selector: '#${form.name_field_id}',
height: 500,
menubar: true,
plugins: 'code print preview searchreplace autolink directionality visualblocks visualchars fullscreen image link media template codesample table charmap hr pagebreak nonbreaking anchor insertdatetime advlist lists textcolor wordcount imagetools contextmenu colorpicker textpattern',
toolbar: 'formatselect | fontsizeselect bold italic underline strikethrough forecolor backcolor | link | alignleft aligncenter alignright alignjustify | numlist bullist outdent indent | removeformat',
fontsize_formats: "8pt 10pt 12pt 14pt 18pt 24pt 36pt 48pt 64pt 100pt 120pt",
content_css: '//www.tinymce.com/css/codepen.min.css',
setup: function (editor) {
editor.on('change', function () {
tinymce.triggerSave();
});
}
});
</script>
${add_js('tinymce/tinymce.min.js', 'admin')}

View file

@ -0,0 +1,18 @@
<%inherit file="../layout.phtml"/>
<%block name="content">
<form method="post" id="edit_item">
${edit_form|n}
<p><input type="submit" value="${tlang('Save')}" />
</form>
</%block>
<%block name="jscript_block">
<script>
$('#edit_item').submit( function () {
return false;
});
</script>
</%block>

View file

@ -18,6 +18,8 @@ url_app=config.apps['admin'][2]
admin_app=FastAPI(docs_url="/docs", openapi_url="/docs/openapi.json", title='Paramecio Admin', version='0.9')
# The plugins are added inverse.
@admin_app.middleware('http')
async def db_connect(request: Request, call_next):

View file

@ -10,7 +10,8 @@ from parameciofast.fast import app
import os
from pydantic import BaseModel
from parameciofast.libraries.lists import SimpleList
from parameciofast.libraries.formsutils import show_form
from parameciofast.libraries.db.coreforms import SelectForm
env=env_theme(__file__)
@ -23,7 +24,7 @@ tpl_path=os.path.dirname(__file__).replace('/admin', '')+'/templates/admin'
if t.env.directories[1]!=tpl_path:
t.env.directories.insert(1, tpl_path)
modules_admin.append(['menu_users', 'people-circle', True])
#modules_admin.append(['menu_users', 'people-circle', True])
modules_admin.append(['fastadmin_users', 'people-circle'])
@ -53,8 +54,30 @@ def fastadmin_users_edit(item_id: int, request: Request):
db=request.state.db
#return t.load_template('edit_users.phtml', title=i18n.tlang('Edit admin users'), tlang=i18n.tlang, url_for=app.url_path_for, module_selected='fastadmin_users', session=request.session)
return ""
session=request.session
users=UserAdmin(db)
users.fields['dark_theme'].name_form=SelectForm
users.fields['disabled'].name_form=SelectForm
users.fields['double_auth'].name_form=SelectForm
users.create_forms()
users.forms['dark_theme'].arr_select={0: _('Light theme'), 1: _('Dark theme')}
users.forms['disabled'].arr_select={0: _('No'), 1: _('Yes')}
users.forms['double_auth'].arr_select={0: _('No'), 1: _('Yes')}
arr_user=users.select_a_row(item_id)
fields=['username', 'password', 'email', 'lang', 'disabled', 'double_auth', 'dark_theme']
forms={k:v for k,v in users.forms.items() if k in fields}
edit_form=show_form(arr_user, forms, t, session, yes_error=False, pass_values=True, modelform_tpl='forms/modelform.phtml')
return t.load_template('utils/edit.phtml', title=i18n.tlang('Edit admin users'), tlang=i18n.tlang, url_for=app.url_path_for, module_selected='fastadmin_users', session=request.session, edit_form=edit_form)
#return ""