118 lines
3.2 KiB
Python
118 lines
3.2 KiB
Python
import os
|
|
from cuchulu.libraries.generate_admin_class import GenerateAdminClass
|
|
#from cuchulu.libraries.db.adminutils import make_admin_url
|
|
#from paramecio.citoplasma.urls import make_url
|
|
from cuchulu.libraries.i18n import I18n
|
|
from settings import config
|
|
from modules.pages2.models import pages
|
|
|
|
from cuchulu.libraries.db.coreforms import BaseForm
|
|
from cuchulu.modules.admin import admin_app, t as admin_t
|
|
#from flask import url_for, g
|
|
from cuchulu.libraries.urls import url_for
|
|
from cuchulu.libraries.mtemplates import env_theme, PTemplate
|
|
from bottle import request
|
|
from cuchulu.libraries.urls import make_media_url
|
|
try:
|
|
import ujson as json
|
|
except:
|
|
import json
|
|
|
|
env=env_theme(__file__)
|
|
|
|
t=PTemplate(env)
|
|
|
|
t.env.directories=admin_t.env.directories
|
|
|
|
t.env.directories.insert(1, os.path.dirname(__file__).replace('/admin', '')+'/templates/admin')
|
|
|
|
class TextEditorJsForm(BaseForm):
|
|
"""Form for html texts, based in tinycme javascript library"""
|
|
|
|
def __init__(self, name, value, t_add=None):
|
|
"""
|
|
Args:
|
|
name (str): The html name for this form
|
|
value (str): The default value of this html form.
|
|
t_add (PTemplate): If you want change the standard html form, use other template loader
|
|
"""
|
|
|
|
super().__init__(name, value)
|
|
|
|
self.t=t_add
|
|
|
|
if t_add==None:
|
|
self.t=t
|
|
|
|
#if type(self.default_value)==dict:
|
|
|
|
# self.default_value=json.dumps(value)
|
|
|
|
def form(self):
|
|
|
|
return self.t.load_template('editorjsform.phtml', form=self)
|
|
|
|
@admin_app.get('/admin/pages2', name='admin_app.admin_pages2')
|
|
@admin_app.post('/admin/pages2', name='admin_app.admin_pages2')
|
|
def admin_pages2(db=True):
|
|
|
|
#t=admin_t
|
|
|
|
conn=db
|
|
|
|
page=pages.Page2(conn)
|
|
|
|
page.enctype=True
|
|
|
|
page.fields['slugify'].name_form=BaseForm
|
|
|
|
page.fields['text'].name_form=TextEditorJsForm
|
|
|
|
#page.fields['text'].extra_parameters[0].t=t
|
|
|
|
#url=make_admin_url('pages')
|
|
url=url_for('admin_app.admin_pages2')
|
|
|
|
admin=GenerateAdminClass(page, url, t)
|
|
|
|
admin.list.fields_showed=['id', 'title', 'slugify']
|
|
|
|
form_admin=admin.show()
|
|
|
|
#return admin.show()
|
|
if type(form_admin).__name__=='str':
|
|
|
|
return t.load_template('content.phtml', title=I18n.lang('pages', 'pages', 'Pages'), contents=form_admin, path_module='admin_app.admin_pages2')
|
|
else:
|
|
|
|
return form_admin
|
|
|
|
|
|
@admin_app.post('/pages/upload_image', name="admin_app.pages2_upload_image")
|
|
def pages2_upload_image():
|
|
|
|
success=0
|
|
|
|
new_url=''
|
|
|
|
if 'image' in request.files:
|
|
|
|
file=request.files['image']
|
|
|
|
images_dir='./modules/pages2/media/images/editor'
|
|
|
|
if not os.path.isdir(images_dir):
|
|
#os.mkdir(images_dir)
|
|
pathlib.Path(images_dir).mkdir(0o755, True)
|
|
|
|
filename=file.filename
|
|
|
|
if not os.path.isfile(images_dir+'/'+filename):
|
|
file.save(os.path.join(images_dir, filename))
|
|
|
|
success=1
|
|
|
|
new_url=make_media_url('images/editor/'+filename, 'pages2')
|
|
|
|
return {'success': success, 'file': {'url': new_url}}
|
|
|