<s> from django . conf import settings <EOL> from django . template import RequestContext <EOL> from django . template . loader import render_to_string <EOL> from django . contrib . contenttypes . models import ContentType <EOL> from django . http import Http404 <EOL> from djangorestframework . response import Response , ErrorResponse <EOL> from djangorestframework import status <EOL> from ionyweb . utils import ContentTypeAccessor as CTA <EOL> from ionyweb . plugin . models import PluginRelation <EOL> from ionyweb . website . rendering import RenderingContext <EOL> from ionyweb . administration . views import IsAdminView <EOL> from ionyweb . administration . utils import ( MESSAGES , <EOL> check_object_html_id , <EOL> check_placeholder_html_id , <EOL> is_page_placeholder_html_id ) <EOL> from ionyweb . plugin . conf import PLUGINS_CATEGORIES , PLUGINS_LIST <EOL> class PluginsView ( IsAdminView ) : <EOL> """<STR_LIT>""" <EOL> def get ( self , request ) : <EOL> """<STR_LIT>""" <EOL> html = render_to_string ( '<STR_LIT>' , <EOL> { '<STR_LIT>' : PLUGINS_LIST , <EOL> '<STR_LIT>' : PLUGINS_CATEGORIES } , <EOL> context_instance = RequestContext ( request ) ) <EOL> response = Response ( status . HTTP_200_OK , { "<STR_LIT:html>" : html } ) <EOL> return self . render ( response ) <EOL> class PluginsByCategoryView ( IsAdminView ) : <EOL> """<STR_LIT>""" <EOL> def get ( self , request , slug ) : <EOL> list_plugins = PLUGINS_LIST [ slug ] <EOL> html = render_to_string ( '<STR_LIT>' , <EOL> { '<STR_LIT>' : list_plugins } , <EOL> context_instance = RequestContext ( request ) ) <EOL> response = Response ( status . HTTP_200_OK , { "<STR_LIT:html>" : html } ) <EOL> return self . render ( response ) <EOL> class PluginsDescription ( IsAdminView ) : <EOL> def get ( self , request , id ) : <EOL> plugin = ContentType . objects . get ( id = id ) <EOL> info = plugin . model_class ( ) . get_description ( ) <EOL> html = render_to_string ( '<STR_LIT>' , <EOL> { '<STR_LIT:info>' : info , <EOL> '<STR_LIT:id>' : id } , <EOL> context_instance = RequestContext ( request ) ) <EOL> response = Response ( status . HTTP_200_OK , { "<STR_LIT:html>" : html } ) <EOL> return self . render ( response ) <EOL> class PluginView ( IsAdminView ) : <EOL> """<STR_LIT>""" <EOL> def get ( self , request , relation_html_id = None ) : <EOL> """<STR_LIT>""" <EOL> if relation_html_id is not None : <EOL> pk = check_object_html_id ( relation_html_id ) [ <NUM_LIT:1> ] <EOL> try : <EOL> obj = PluginRelation . objects . filter ( <EOL> pages__website__exact = request . website , <EOL> id__exact = pk ) [ <NUM_LIT:0> ] <EOL> except IndexError : <EOL> raise Http404 <EOL> plugin = obj . content_object <EOL> PluginFormClass = plugin . get_admin_form ( ) <EOL> form = PluginFormClass ( instance = plugin ) <EOL> content = render_to_string ( '<STR_LIT>' , <EOL> { '<STR_LIT>' : form , <EOL> '<STR_LIT>' : plugin , <EOL> '<STR_LIT>' : relation_html_id } , <EOL> context_instance = RequestContext ( request ) ) <EOL> response = Response ( status . HTTP_200_OK , { '<STR_LIT:html>' : content , } ) <EOL> return self . render ( response ) <EOL> placeholder_id = request . GET . get ( '<STR_LIT>' , None ) <EOL> plugin_type = request . GET . get ( '<STR_LIT>' , None ) <EOL> if placeholder_id and plugin_type : <EOL> check_placeholder_html_id ( placeholder_id ) <EOL> try : <EOL> plugin_ct = CTA ( ) . get_for_pk ( plugin_type ) <EOL> PluginClass = plugin_ct . model_class ( ) <EOL> PluginFormClass = PluginClass ( ) . get_admin_form ( ) <EOL> plugin_form = PluginFormClass ( ) <EOL> content = render_to_string ( '<STR_LIT>' , <EOL> { '<STR_LIT>' : plugin_form , <EOL> '<STR_LIT>' : placeholder_id , <EOL> '<STR_LIT>' : plugin_type , } , <EOL> context_instance = RequestContext ( request ) ) <EOL> response = Response ( status . HTTP_200_OK , { '<STR_LIT:html>' : content , } ) <EOL> return self . render ( response ) <EOL> except ContentType . DoesNotExist : <EOL> raise ErrorResponse ( status . HTTP_400_BAD_REQUEST , <EOL> { '<STR_LIT>' : MESSAGES . get ( '<STR_LIT>' , "<STR_LIT>" ) } ) <EOL> else : <EOL> raise ErrorResponse ( status . HTTP_400_BAD_REQUEST , <EOL> { '<STR_LIT>' : MESSAGES . get ( '<STR_LIT>' , "<STR_LIT>" ) } ) <EOL> def put ( self , request ) : <EOL> """<STR_LIT>""" <EOL> request . PUT = self . DATA . copy ( ) <EOL> placeholder_html_id = request . PUT . get ( '<STR_LIT>' , None ) <EOL> plugin_type = request . PUT . get ( '<STR_LIT>' , None ) <EOL> if placeholder_html_id and plugin_type : <EOL> placeholder_slug_items = check_placeholder_html_id ( placeholder_html_id ) <EOL> layout_section_slug = placeholder_slug_items [ <NUM_LIT:0> ] <EOL> try : <EOL> content_type = CTA ( ) . get_for_pk ( plugin_type ) <EOL> except ContentType . DoesNotExist : <EOL> raise ErrorResponse ( status . HTTP_400_BAD_REQUEST , <EOL> { '<STR_LIT>' : MESSAGES . get ( '<STR_LIT>' , "<STR_LIT>" ) } ) <EOL> PluginClass = content_type . model_class ( ) <EOL> plugin = PluginClass ( ) <EOL> PluginFormClass = plugin . get_admin_form ( ) <EOL> form = PluginFormClass ( request . PUT , instance = plugin ) <EOL> if form . is_valid ( ) : <EOL> new_plugin = form . save ( ) <EOL> display_on_new_pages = ( not is_page_placeholder_html_id ( placeholder_html_id ) ) <EOL> relation = PluginRelation . objects . create ( content_object = new_plugin , <EOL> placeholder_slug = placeholder_html_id , <EOL> display_on_new_pages = display_on_new_pages ) <EOL> relation . pages . add ( request . page ) <EOL> if display_on_new_pages : <EOL> for page in request . website . pages . all ( ) : <EOL> relation . pages . add ( page ) <EOL> try : <EOL> last_relation = PluginRelation . objects . filter ( <EOL> pages = request . page , <EOL> placeholder_slug = placeholder_html_id ) . order_by ( '<STR_LIT>' ) [ <NUM_LIT:0> ] <EOL> plugin_order = last_relation . plugin_order + <NUM_LIT:10> <EOL> except IndexError : <EOL> plugin_order = <NUM_LIT:0> <EOL> relation . plugin_order = plugin_order <EOL> relation . save ( ) <EOL> rendering_context = RenderingContext ( request ) <EOL> plugin_html_medias = rendering_context . get_html_medias_for_plugin_relation ( relation ) <EOL> html_rendering = rendering_context . get_html_layout ( layout_section_slug ) <EOL> response = Response ( status . HTTP_200_OK , <EOL> { '<STR_LIT>' : MESSAGES . get ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT:html>' : html_rendering , <EOL> '<STR_LIT>' : layout_section_slug , <EOL> '<STR_LIT>' : plugin_html_medias } ) <EOL> return self . render ( response ) <EOL> else : <EOL> html = render_to_string ( '<STR_LIT>' , <EOL> { '<STR_LIT>' : form , <EOL> '<STR_LIT>' : placeholder_html_id , <EOL> '<STR_LIT>' : plugin_type } , <EOL> context_instance = RequestContext ( request ) ) <EOL> raise ErrorResponse ( status . HTTP_400_BAD_REQUEST , <EOL> { '<STR_LIT>' : MESSAGES . get ( '<STR_LIT>' , "<STR_LIT>" ) , <EOL> '<STR_LIT:html>' : html } ) <EOL> else : <EOL> raise ErrorResponse ( status . HTTP_400_BAD_REQUEST , <EOL> { '<STR_LIT>' : MESSAGES . get ( '<STR_LIT>' , "<STR_LIT>" ) } ) <EOL> def post ( self , request , relation_html_id ) : <EOL> """<STR_LIT>""" <EOL> pk = check_object_html_id ( relation_html_id ) [ <NUM_LIT:1> ] <EOL> try : <EOL> plugin_relation = PluginRelation . objects . filter ( <EOL> pages__website__exact = request . website , <EOL> id__exact = pk ) [ <NUM_LIT:0> ] <EOL> except IndexError : <EOL> raise Http404 <EOL> plugin = plugin_relation . content_object <EOL> PluginFormClass = plugin . get_admin_form ( ) <EOL> form = PluginFormClass ( request . POST , instance = plugin ) <EOL> if form . is_valid ( ) : <EOL> plugin = form . save ( ) <EOL> placeholder_slug_items = check_placeholder_html_id ( <EOL> plugin_relation . placeholder_slug ) <EOL> layout_section_slug = placeholder_slug_items [ <NUM_LIT:0> ] <EOL> rendering_context = RenderingContext ( request ) <EOL> html_rendering = rendering_context . get_html_layout ( layout_section_slug ) <EOL> response = Response ( status . HTTP_200_OK , <EOL> { "<STR_LIT>" : MESSAGES . get ( '<STR_LIT>' , "<STR_LIT>" ) , <EOL> '<STR_LIT:html>' : html_rendering , <EOL> '<STR_LIT>' : layout_section_slug } ) <EOL> return self . render ( response ) <EOL> else : <EOL> html = render_to_string ( '<STR_LIT>' , <EOL> { '<STR_LIT>' : form , <EOL> '<STR_LIT>' : plugin , <EOL> '<STR_LIT>' : relation_html_id } , <EOL> context_instance = RequestContext ( request ) ) <EOL> raise ErrorResponse ( status . HTTP_400_BAD_REQUEST , <EOL> { '<STR_LIT>' : MESSAGES . get ( '<STR_LIT>' , "<STR_LIT>" ) , <EOL> '<STR_LIT:html>' : html } ) <EOL> def delete ( self , request , relation_html_id ) : <EOL> """<STR_LIT>""" <EOL> pk = check_object_html_id ( relation_html_id ) [ <NUM_LIT:1> ] <EOL> try : <EOL> obj = PluginRelation . objects . filter ( pages__website__exact = request . website , <EOL> id__exact = pk ) [ <NUM_LIT:0> ] <EOL> except IndexError : <EOL> raise Http404 <EOL> obj . delete ( ) <EOL> response = Response ( status . HTTP_200_OK , <EOL> { "<STR_LIT>" : MESSAGES . get ( '<STR_LIT>' , '<STR_LIT>' ) } ) <EOL> return self . render ( response ) <EOL> class PluginRelationView ( IsAdminView ) : <EOL> """<STR_LIT>""" <EOL> def post ( self , request ) : <EOL> """<STR_LIT>""" <EOL> placeholder_html_id = request . POST . get ( '<STR_LIT>' , None ) <EOL> plugins_order = request . POST . getlist ( '<STR_LIT>' ) <EOL> if placeholder_html_id and plugins_order : <EOL> check_placeholder_html_id ( placeholder_html_id , <EOL> extras_id = [ settings . HTML_ID_PLACEHOLDER_CLIPBOARD , ] ) <EOL> i = <NUM_LIT:0> <EOL> for plugin_id in plugins_order : <EOL> plugin_type , relation_id = check_object_html_id ( plugin_id , <EOL> types = [ settings . SLUG_PLUGIN , <EOL> settings . SLUG_APP ] ) <EOL> if plugin_type == settings . SLUG_APP : <EOL> if placeholder_html_id == settings . HTML_ID_PLACEHOLDER_CLIPBOARD : <EOL> raise ErrorResponse ( status . HTTP_400_BAD_REQUEST , <EOL> { '<STR_LIT>' : MESSAGES . get ( '<STR_LIT>' , "<STR_LIT>" ) } ) <EOL> obj = request . page <EOL> else : <EOL> try : <EOL> obj = PluginRelation . objects . filter ( pages__website__exact = request . website , <EOL> id__exact = relation_id ) [ <NUM_LIT:0> ] <EOL> if not ( is_page_placeholder_html_id ( placeholder_html_id ) or <EOL> placeholder_html_id == settings . HTML_ID_PLACEHOLDER_CLIPBOARD ) : <EOL> obj . pages . add ( * request . website . pages . all ( ) ) <EOL> if not obj . display_on_new_pages : <EOL> obj . display_on_new_pages = True <EOL> obj . save ( ) <EOL> else : <EOL> obj . pages . clear ( ) <EOL> obj . pages . add ( request . page ) <EOL> if obj . display_on_new_pages : <EOL> obj . display_on_new_pages = False <EOL> obj . save ( ) <EOL> except IndexError : <EOL> raise ErrorResponse ( status . HTTP_400_BAD_REQUEST , <EOL> { '<STR_LIT>' : MESSAGES . get ( '<STR_LIT>' , "<STR_LIT>" ) } ) <EOL> obj . plugin_order = i <EOL> if plugin_type == settings . SLUG_APP : <EOL> if i > <NUM_LIT:5> : <EOL> obj . plugin_order -= <NUM_LIT:5> <EOL> else : <EOL> i = i + <NUM_LIT:10> <EOL> obj . placeholder_slug = placeholder_html_id <EOL> obj . save ( ) <EOL> response = Response ( status . HTTP_200_OK , <EOL> { "<STR_LIT>" : MESSAGES . get ( '<STR_LIT>' , '<STR_LIT>' ) } ) <EOL> return self . render ( response ) <EOL> else : <EOL> raise ErrorResponse ( status . HTTP_400_BAD_REQUEST , <EOL> { '<STR_LIT>' : MESSAGES . get ( '<STR_LIT>' , "<STR_LIT>" ) } ) </s>
