Exemplo n.º 1
0
    def get_module(self, request):
        template = os.path.join(TEMPLATES_PATH, 'textarea.html')

        params = {"label": self.label,
                  'data': self.data}

        return render_module(template, params)
Exemplo n.º 2
0
    def get_module(self, request):
        template = os.path.join(TEMPLATES_PATH, 'textarea.html')

        params = {"label": self.label,
                  'data': self.data}

        return render_module(template, params)
Exemplo n.º 3
0
class Module(object):
    __metaclass__ = ABCMeta

    def __init__(self, scripts=list(), styles=list()):
        self.scripts = scripts
        self.styles = styles

        # Skeleton of the modules
        self.template = os.path.join(settings.SITE_ROOT, 'templates',
                                     'module.html')

    def add_scripts(self, scripts):
        for script in scripts:
            self.scripts.append(script)

    def add_styles(self, styles):
        for style in styles:
            self.styles.append(style)

    def render(self, request):
        if request.method == 'GET':
            if 'resources' in request.GET:
                return self._get_resources()
            else:
                return self._get(request)
        elif request.method == 'POST':
            return self._post(request)
        else:
            raise ModuleError(
                'Only GET and POST methods can be used to communicate with a module.'
            )

    def _get(self, request):
        template_data = {
            'module': '',
            'display': '',
            'result': '',
            'url': request.GET['url']
        }

        try:
            template_data['module'] = self._get_module(request)
            template_data['display'] = self._get_display(request)
            template_data['result'] = sanitize(self._get_result(request))
        except Exception, e:
            raise ModuleError(
                'Something went wrong during module initialization: ' +
                e.message)

        # TODO Add additional checks
        for key, val in template_data.items():
            if val is None:
                raise ModuleError(
                    'Variable ' + key +
                    ' cannot be None. Module initialization cannot be completed.'
                )

        # Apply tags to the template
        html_code = render_module(self.template, template_data)
        return HttpResponse(html_code, status=HTTP_200_OK)
Exemplo n.º 4
0
    def get_module(self, request):
        template = os.path.join(TEMPLATES_PATH, 'popup.html')
        params = {
            "popup_content": self.popup_content,
            "button_label": self.button_label
        }

        return render_module(template, params)
Exemplo n.º 5
0
    def get_module(self, request):
        template = os.path.join(TEMPLATES_PATH, 'autocomplete.html')
        params = {}

        if self.label is not None:
            params.update({"label": self.label})

        return render_module(template, params)
Exemplo n.º 6
0
 def get_module(self, request):
     template = os.path.join(TEMPLATES_PATH, 'input_button.html')
     params = {"button_label": self.button_label}
     if self.label is not None:
         params.update({"label": self.label})
     if self.default_value is not None:
         params.update({"default_value": self.default_value})
     return render_module(template, params)
Exemplo n.º 7
0
    def get_module(self, request):
        template = os.path.join(TEMPLATES_PATH, 'popup.html')
        params = {
            "popup_content": self.popup_content,
            "button_label": self.button_label
        }

        return render_module(template, params)
Exemplo n.º 8
0
 def get_module(self, request):
     template = os.path.join(TEMPLATES_PATH, 'sync_input.html')
     params = {'class': self.modclass}
     if self.label is not None:
         params.update({"label": self.label})
     if self.default_value is not None:
         params.update({"default_value": self.default_value})
     return render_module(template, params)
Exemplo n.º 9
0
    def get_module(self, request):
        template = os.path.join(TEMPLATES_PATH, 'autocomplete.html')
        params = {}

        if self.label is not None:
            params.update({"label": self.label})

        return render_module(template, params)
Exemplo n.º 10
0
 def get_module(self, request):
     template = os.path.join(TEMPLATES_PATH, 'input_button.html')
     params = {"button_label": self.button_label}
     if self.label is not None:
         params.update({"label": self.label})
     if self.default_value is not None:
         params.update({"default_value": self.default_value})
     return render_module(template, params)
Exemplo n.º 11
0
 def get_module(self, request):
     template = os.path.join(TEMPLATES_PATH, 'sync_input.html')
     params = {'class': self.modclass}
     if self.label is not None:
         params.update({"label": self.label})
     if self.default_value is not None:
         params.update({"default_value": self.default_value})
     return render_module(template, params)
Exemplo n.º 12
0
    def get_module(self, request):
        def _create_html_checkbox(input_key, input_value, checked=False):
            input_tag = '<input type="checkbox" '
            if checked:
                input_tag += 'checked '
            input_tag += 'value="' + input_key + '"/> ' + input_value

            return '<span>' + input_tag + '</span>'

        template = os.path.join(TEMPLATES_PATH, 'checkboxes.html')

        # Compute number of items in each columns
        col_nb = 3
        opt_nb = len(self.options)
        max_item_nb = int(ceil(opt_nb / col_nb))

        # Parameters initialization
        params = {
            "column1": "",
            "column2": "",
            "column3": "",
        }
        item_nb = 0
        col_id = 1
        checkboxes_html = ""

        # Filling the parameters
        for key, val in self.options.items():
            if item_nb == max_item_nb:
                params['column' + str(col_id)] = checkboxes_html

                checkboxes_html = ""
                item_nb = 0
                col_id += 1

            checkboxes_html += _create_html_checkbox(
                key, val, checked=(key in self.selected))
            item_nb += 1

        params['column' + str(col_id)] = checkboxes_html

        # params = {"options": checkboxes_html}

        if self.label is not None:
            params.update({"label": self.label})

        return render_module(template, params)
Exemplo n.º 13
0
    def get_module(self, request):
        def _create_html_checkbox(input_key, input_value, checked=False):
            input_tag = '<input type="checkbox" '
            if checked:
                input_tag += 'checked '
            input_tag += 'value="' + input_key + '"/> ' + input_value

            return '<span>' + input_tag + '</span>'

        template = os.path.join(TEMPLATES_PATH, 'checkboxes.html')

        # Compute number of items in each columns
        col_nb = 3
        opt_nb = len(self.options)
        max_item_nb = int(ceil(opt_nb / col_nb))

        # Parameters initialization
        params = {
            "column1": "",
            "column2": "",
            "column3": "",
        }
        item_nb = 0
        col_id = 1
        checkboxes_html = ""

        # Filling the parameters
        for key, val in self.options.items():
            if item_nb == max_item_nb:
                params['column' + str(col_id)] = checkboxes_html

                checkboxes_html = ""
                item_nb = 0
                col_id += 1

            checkboxes_html += _create_html_checkbox(key, val, checked=(key in self.selected))
            item_nb += 1

        params['column' + str(col_id)] = checkboxes_html

        # params = {"options": checkboxes_html}

        if self.label is not None:
            params.update({"label": self.label})

        return render_module(template, params)
Exemplo n.º 14
0
    def get_module(self, request):
        template = os.path.join(TEMPLATES_PATH, 'options.html')
        options_html = ""

        for key, val in self.options.items():
            if self.selected is not None and key == self.selected:
                options_html += "<option value='" + key + "' selected>" + val + "</option>"
            else:
                options_html += "<option value='" + key + "'>" + val + "</option>"

        params = {"options": options_html}

        if self.label is not None:
            params.update({"label": self.label})
            
        if self.disabled is not None:
            params.update({"disabled": self.disabled})

        return render_module(template, params)
Exemplo n.º 15
0
    def get_module(self, request):
        template = os.path.join(TEMPLATES_PATH, 'options.html')
        options_html = ""

        for key, val in self.options.items():
            if self.selected is not None and key == self.selected:
                options_html += "<option value='" + key + "' selected>" + val + "</option>"
            else:
                options_html += "<option value='" + key + "'>" + val + "</option>"

        params = {"options": options_html}

        if self.label is not None:
            params.update({"label": self.label})
            
        if self.disabled is not None:
            params.update({"disabled": self.disabled})

        return render_module(template, params)
Exemplo n.º 16
0
        return HttpResponse(html_code, status=HTTP_200_OK)

    def _post(self, request):
        template_data = {
            'display': '',
            'result': '',
            'url': ''
        }

        try:
            template_data['display'] = self._post_display(request)
            template_data['result'] = sanitize(self._post_result(request))
        except Exception, e:
            raise ModuleError('Something went wrong during module update: ' + e.message)

        html_code = render_module(self.template, template_data)
        
        response_dict = dict()
        response_dict['html'] = html_code

        if hasattr(self, "get_XpathAccessor"):
            response_dict.update(self.get_XpathAccessor())

        return HttpResponse(json.dumps(response_dict))
    
    def _get_resources(self):
        """
        """
        response = {
            'scripts': self.scripts,
            'styles': self.styles
Exemplo n.º 17
0
                options['attributes'] = self._post_result(
                    request)['attributes']
            else:
                options['data'] = post_result

            # TODO Implement this system instead
            # options['content'] = self._get_content(request)
            # options['attributes'] = self._get_attributes(request)

            module_element.update(set__options=options)
            module_element.reload()
        except Exception, e:
            raise ModuleError('Something went wrong during module update: ' +
                              e.message)

        html_code = render_module(self.template, template_data)

        response_dict = dict()
        response_dict['html'] = html_code

        if hasattr(self, "get_XpathAccessor"):
            response_dict.update(self.get_XpathAccessor())

        return HttpResponse(json.dumps(response_dict))

    def _get_resources(self):
        """
        """
        response = {'scripts': self.scripts, 'styles': self.styles}

        return HttpResponse(json.dumps(response), status=HTTP_200_OK)
Exemplo n.º 18
0
class Module(object):
    __metaclass__ = ABCMeta

    def __init__(self, scripts=list(), styles=list()):
        self.scripts = scripts
        self.styles = styles

        # Is the module managing occurences by its own? (False by default)
        self.is_managing_occurences = False

        # Skeleton of the modules
        self.template = os.path.join(settings.SITE_ROOT, 'templates',
                                     'module.html')

    def add_scripts(self, scripts):
        for script in scripts:
            self.scripts.append(script)

    def add_styles(self, styles):
        for style in styles:
            self.styles.append(style)

    def render(self, request):
        if request.method == 'GET':
            if 'resources' in request.GET:
                return self._get_resources()
            elif 'managing_occurences' in request.GET:
                return HttpResponse(json.dumps(self.is_managing_occurences),
                                    HTTP_200_OK)
            else:
                return self._get(request)
        elif request.method == 'POST':
            return self._post(request)
        else:
            raise ModuleError(
                'Only GET and POST methods can be used to communicate with a module.'
            )

    def _get(self, request):
        module_id = request.GET['module_id']
        url = request.GET[
            'url'] if 'url' in request.GET else SchemaElement.objects().get(
                pk=module_id).options['url']
        template_data = {
            'module_id': module_id,
            'module': '',
            'display': '',
            'result': '',
            'url': url
        }

        try:
            template_data['module'] = self._get_module(request)
            template_data['display'] = self._get_display(request)

            result = self._get_result(request)
            template_data['result'] = result

            module_element = SchemaElement.objects.get(
                pk=request.GET['module_id'])
            options = module_element.options

            options['data'] = result
            module_element.update(set__options=options)

            module_element.reload()
        except Exception, e:
            raise ModuleError(
                'Something went wrong during module initialization: ' +
                e.message)

        # TODO Add additional checks
        for key, val in template_data.items():
            if val is None:
                raise ModuleError(
                    'Variable ' + key +
                    ' cannot be None. Module initialization cannot be completed.'
                )

        # Apply tags to the template
        html_code = render_module(self.template, template_data)
        return HttpResponse(html_code, status=HTTP_200_OK)