class RoomPage(Page): @reinvokable def __init__( self, room, unread_data, **kwargs ): self.room = room self.unread_data = unread_data super(RoomPage, self).__init__(**kwargs) header = Fragment(template='forum/room-header.html') table = Messages() hr = html.hr() refresh = html.script( 'start_subscription_refresh_subpage();', include=lambda request, **_: request.user_agent.is_mobile, ) def own_evaluate_parameters(self): return dict( page=self, room=self.room, unread_data=self.unread_data, unread_identifier=self.unread_data.unread_identifier, title=self.room.name, time=self.unread_data.unread2_time or self.unread_data.user_time, # TODO: handle this in UnreadData? is_subscribed=is_subscribed, )
class SubmitPage(Page): heading = html.h1('TriOptima URL Blessalizer') form = SubmitForm() set_focus = html.script( mark_safe('document.getElementById("id_url").focus();', )) admin = html.p(html.a('Admin', attrs__href='/entries'), ' (requires login)')
def entries(request): return Page(parts=dict( admin_table=EntryAdminTable(), approve_table=EntryApproveTable(), unapprove_table=EntryUnapproveTable(), helper_script=html.script( mark_safe(''' function copyToClipBoard(url) { const temp = document.createElement('textarea'); temp.value = url; document.body.appendChild(temp); temp.select(); document.execCommand('copy'); document.body.removeChild(temp); } ''')), ))
class LoginPage(Page): styling = html.style(""" .extra_links, .form { margin-left: auto; margin-right: auto; width: 710px; } """) form = LoginForm() links = html.div( attrs__class__extra_links=True, attrs__style={'text-align': 'right'}, children__create_account=html.a('Create account', attrs__href='/create-account/'), children__p=html.div(), children__forgot_passsword=html.a('Forgot your password?', attrs__href='/forgot-password/'), ) set_focus = html.script( mark_safe('document.getElementById("id_username").focus();'))
def live_edit_view(request, view, args, kwargs): view = get_wrapped_view(view) # Read the old code try: # view is a function based view filename = view.__globals__['__file__'] except AttributeError: # view is an iommi class from iommi.debug import filename_and_line_num_from_part filename, _ = filename_and_line_num_from_part(view) with open(filename) as f: entire_file = f.read() ast_of_entire_file = parso.parse(entire_file) is_unix_line_endings = '\r\n' not in entire_file ast_of_old_code = find_view(view, ast_of_entire_file) assert ast_of_old_code is not None flow_direction = request.GET.get('_iommi_live_edit') or 'column' assert flow_direction in ('column', 'row') if request.method == 'POST': try: code = request.POST['data'].replace('\t', ' ') if is_unix_line_endings: code = code.replace('\r\n', '\n') final_result = dangerous_execute_code(code, request, view, args, kwargs) if orig_reload is not None: # A little monkey patch dance to avoid one reload of the runserver when it's just us writing the code to disk # This only works in django 2.2+ def restore_auto_reload(filename): from django.utils import autoreload print('Skipped reload') autoreload.trigger_reload = orig_reload autoreload.trigger_reload = restore_auto_reload if isinstance(view, Part): ast_of_new_code = find_node(name=view.__class__.__name__, node=parso.parse(code), node_type='classdef') else: ast_of_new_code = find_node(name=view.__name__, node=parso.parse(code), node_type='funcdef') ast_of_old_code.children[:] = ast_of_new_code.children new_code = ast_of_entire_file.get_code() with open(filename, 'w') as f: f.write(new_code) return final_result except Exception as e: import traceback traceback.print_exc() error = str(e) if not error: error = str(e.__class__) return HttpResponse(json.dumps(dict(error=error))) return LiveEditPage( title='iommi live edit', h_tag__include=False, assets__code_editor=Asset.js( attrs=dict( src='https://cdnjs.cloudflare.com/ajax/libs/ace/1.4.12/ace.js', integrity= 'sha512-GZ1RIgZaSc8rnco/8CXfRdCpDxRCphenIiZ2ztLy3XQfCbQUSCuk8IudvNHxkRA3oUg6q0qejgN/qqyG1duv5Q==', crossorigin='anonymous', ), after=-1, ), assets__live_edit_page_custom=Asset( tag='style', text=''' .container { padding: 0 !important; margin: 0 !important; max-width: 100%; } html, body { height: 100%; margin: 0; } .container { display: flex; flex-flow: <<flow_direction>>; height: 100%; } .container iframe { flex: 1 1 auto; } .container #editor { flex: 2 1 auto; } '''.replace('<<flow_direction>>', flow_direction), ), parts__result=html.iframe(attrs__id='result'), parts__editor=html.div( ast_of_old_code.get_code(), attrs__id='editor', ), parts__script=html.script( mark_safe(''' function iommi_debounce(func, wait) { let timeout; return (...args) => { const fn = () => func.apply(this, args); clearTimeout(timeout); timeout = setTimeout(() => fn(), wait); }; } var editor = ace.edit("editor"); editor.setTheme("ace/theme/cobalt"); editor.session.setMode("ace/mode/python"); editor.setShowPrintMargin(false); async function update() { let form_data = new FormData(); form_data.append('data', editor.getValue()); let response = await fetch('', { method: 'POST', body: form_data }); let foo = await response.json(); if (foo.page) { // TODO: get scroll position and restore it document.getElementById('result').srcdoc = foo.page; } } function foo() { iommi_debounce(update, 200)(); } editor.session.on('change', foo); editor.setFontSize(14); editor.session.setUseWrapMode(true); foo(); ''')), )
class ChangePasswordPage(Page): form = ChangePasswordForm() set_focus = html.script(mark_safe( 'document.getElementById("id_current_password").focus();', ))
class LoginPage(Page): form = LoginForm() set_focus = html.script(mark_safe( 'document.getElementById("id_username").focus();', ))
from iommi.style_font_awesome_4 import font_awesome_4 from iommi.style_base import base semantic_ui_base = Style( base, root__assets=dict( css=html.link(attrs=dict( rel='stylesheet', href= 'https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.4.1/semantic.min.css', integrity='sha256-9mbkOfVho3ZPXfM7W8sV2SndrGDuh7wuyLjtsWeTI1Q=', crossorigin='anonymous', ), ), js=html.script(attrs=dict( src= 'https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.4.1/semantic.min.js', integrity='sha256-t8GepnyPmw9t+foMh3mKNvcorqNHamSKtKRxxpUEgFI=', crossorigin='anonymous', ), ), ), Container=dict( tag='div', attrs__class={ 'ui': True, 'main': True, 'container': True, }, ), Form=dict( attrs__class=dict( ui=True, form=True,