Beispiel #1
0
	def render(self):
		self.req.content.report('header', tags.style(type='text/css')[
			"@import '%s';" % self.req.get_path('assets/progress-bar/style.css')
		])
	
		assets.activate_jquery(self.req)
		
		self.req.content.report('header', tags.script(type='text/javascript',
			src=self.req.get_path('assets/progress-bar/support.js'))[''])
	
		element_id = self.attribs.get('element_id', 'progress-bar')
		attribs = self.attribs.copy()
		attribs.pop('element_id', None)
	
		content = tags.div(_id=element_id, _class='progress-field', **attribs)[[
			tags.div(_class='progress-bar')[''],
			tags.div(_class='progress-text')['']
		]]
		
		if(self.callback_url):
			content += tags.script(type='text/javascript')[
				'waitForCompletion("%s", "%s", %d);' % (element_id, self.callback_url, self.interval)
			]
		else:
			content += tags.script(type='text/javascript')[
				'setProgress("%s", %d, %d);' % (element_id, self.value, self.maxvalue)
			]
		
		return content
Beispiel #2
0
	def theme_select_autocomplete(self, form_id, element):
		assets.activate_jquery(self.req)
		self.req.content.report('header', tags.script(type="text/javascript",
			src=self.req.get_path("/assets/jquery/jquery.autocomplete.js"))[''])
		self.req.content.report('header', tags.script(type="text/javascript",
			src=self.req.get_path("/assets/editable-autocomplete.js"))[''])
		
		self.req.content.report('header', tags.style(type="text/css")[
			"""@import '%s';""" % self.req.get_path('/assets/jquery/jquery.autocomplete.css')])
		
		self.req.content.report('header', tags.script(type="text/javascript")[
			"""
			function formatItem(item, index, totalItems){
				return item[0].replace('<', '&lt;').replace('>', '&gt;')
			}
			"""
		])
		ac_id = '%s-%s-autocomplete' % (form_id, element.name)
		ac_cb_id = '%s-%s-ac-callback' % (form_id, element.name)
		
		options = element.attr('options', [])
		optlist = repr([[v, k] for k, v in options.items()])
		
		prefs = dict(
			autoFill	= 1,
			selectFirst	= 1,
			matchSubset	= 0,
			selectOnly	= 1,
			formatItem	= 'formatItem',
		)
		prefs = ','.join(['%s:%s' % (k, v) for k, v in prefs.items()])
		
		ac_javascript = tags.script(type='text/javascript')[
			'$("#%s").autocompleteArray(%s, {onItemSelect:select_item("%s"), %s});' % (ac_id, optlist, ac_cb_id, prefs)
		]
		
		value = element.attr('value')
		if(value):
			label = options.get(value, None)
			if(label is None):
				label = value
			output = tags.input(type="text", name=element.get_element_name() + '[ac]', id=ac_id, value=label)
			output += tags.input(type="hidden", name=element.get_element_name() + '[value]', id=ac_cb_id, value=value)
		else:
			output = tags.input(type="text", name=element.get_element_name() + '[ac]', id=ac_id)
			output += tags.input(type="hidden", name=element.get_element_name() + '[value]', id=ac_cb_id)
		output += ac_javascript
		
		return output
Beispiel #3
0
	def get_element(self, req, style, storable):
		"""
		@see: L{modu.editable.define.definition.get_element()}
		"""
		mlabel = self.get('flabel', '')
		if(mlabel.find('.') == -1):
			mlabel = 'm.%s' % mlabel
		mlabel = self.get('flabel_sql', mlabel)
		
		where = self.get('fwhere', '')
		
		if(callable(where)):
			where = where(storable)
		elif(isinstance(where, dict)):
			where = sql.build_where(where)
		
		limit = 'LIMIT %d' % self.get('limit_choices', 20)
		
		ntom_query = """SELECT m.%s AS value, %s AS label
						FROM %s m
						INNER JOIN %s n2m ON m.%s = n2m.%s AND n2m.%s = %%s
						%s
						ORDER BY label
						%s""" % (self['fvalue'], q(mlabel),
								q(self['ftable']),
								q(self['ntof']), self.get('fvalue', 'id'),
								self['ntof_f_id'], self['ntof_n_id'],
								where, limit)
		
		store = storable.get_store()
		results = store.pool.runQuery(sql.interp(ntom_query, storable.get_id()))
		
		if(style == 'listing' or self.get('read_only', False)):
			label_value = ', '.join([result['label'] for result in results])
			return form.FormNode(self.name)(type='label', value=label_value)
		
		options = dict([(str(result['value']), result['label']) for result in results])
		
		form_name = '%s-form' % storable.get_table()
		ac_id = '%s-%s-autocomplete' % (form_name, self.name)
		select_id = '%s-foreign-select' % self.name
		ac_url = req.get_path(req.prepath, 'autocomplete', storable.get_table(), self.name) + '?time=' + str(time.time())
		
		hidden_options = ''
		for value in options:
			hidden_options += tags.input(type='hidden', name='%s[%s]' % (form_name, self.name), value=value)
		
		select_frm = form.FormNode('%s-select-view' % self.name)
		select_frm(type='select', options=options, size=self.get('size', 5),
					multiple=None, suffix=hidden_options + '<br/>', attributes={'id':select_id})
		
		prefs = 'autoFill:1, selectFirst:1, matchSubset:0, selectOnly:1, extraParams:{t:%d}, minChars:%d' % (int(time.time()), self.get('min_chars', 3))
		# ac_js = '$(document).ready(function(){$("#%s").autocomplete("%s", {onItemSelect:add_foreign_item("%s", "%s"), %s});});' % (ac_id, ac_url, form_name, self.name, prefs)
		ac_js = """
			$(document).ready(function(){
				$("#%s").autocomplete("%s", {%s});
				$("#%s").result(add_foreign_item("%s", "%s"));
			});
		""" % (ac_id, ac_url, prefs, ac_id, form_name, self.name)
		
		ac_controls = tags.script(type='text/javascript')[ac_js]
		
		ac_field = form.FormNode('%s-autocomplete' % self.name)
		ac_field(type='textfield', weight=10, attributes={'id':ac_id}, suffix=ac_controls)
		
		req.content.report('header', tags.style(type="text/css")[
			"""@import '%s';""" % req.get_path('/assets/jquery/jquery.autocomplete.css')])
		
		assets.activate_jquery(req)
		req.content.report('header', tags.script(type="text/javascript",
			src=req.get_path("/assets/jquery/jquery.autocomplete.js"))[''])
		req.content.report('header', tags.script(type="text/javascript",
			src=req.get_path("/assets/editable-autocomplete.js"))[''])
		
		frm = form.FormNode('%s-ac-fieldset' % self.name)(type='fieldset', style='brief')
		frm[select_frm.name] = select_frm
		frm[ac_field.name] = ac_field
		
		return frm
Beispiel #4
0
	def get_element(self, req, style, storable):
		"""
		@see: L{modu.editable.define.definition.get_element()}
		"""
		form_name = '%s-form' % storable.get_table()
		ac_id = '%s-%s-autocomplete' % (form_name, self.name)
		ac_cb_id = '%s-%s-ac-callback' % (form_name, self.name)
		ac_url = req.get_path(req.prepath, 'autocomplete', storable.get_table(), self.name)
		
		prefs = """
					autoFill:1,
					selectFirst:1,
					matchSubset:0,
					selectOnly:1,
					formatItem:formatItem,
					extraParams:{t:%d}, minChars:%d""" % (int(time.time()), self.get('min_chars', 3))
		#ac_javascript = '$("#%s").autocomplete("%s", '
		#ac_javascript += '{onItemSelect:select_item("%s"), %s});'
		#ac_javascript = ac_javascript % (ac_id, ac_url, ac_cb_id, prefs)
		ac_javascript = '$("#%s").autocomplete("%s", {%s});' % (ac_id, ac_url, prefs)
		ac_javascript += '$("#%s").result(select_item_handler("%s"));' % (ac_id, ac_cb_id)
		ac_javascript = tags.script(type='text/javascript')[ac_javascript]
		
		ac_field = form.FormNode('%s-autocomplete' % self.name)
		ac_field(type='textfield', weight=0, attributes={'id':ac_id}, suffix=ac_javascript)
		
		value_field = form.FormNode(self.name)
		value_field(type='hidden', weight=2, value=getattr(storable, self.get_column_name(), None), attributes={'id':ac_cb_id})
		
		store = storable.get_store()
		
		value = self['fvalue']
		label = self['flabel']
		table = self['ftable']
		
		if(hasattr(storable, self.get_column_name())):
			query = 'SELECT %s FROM %s WHERE %s = %%s' % (q(label), q(table), q(value))
			
			field_value = getattr(storable, self.get_column_name())
			if(field_value is not None):
				results = store.pool.runQuery(sql.interp(query, field_value))
				if(results):
					ac_field(value=results[0][label])
				else:
					value_field(value=0)
			else:
				value_field(value=0)
		
		if(style == 'listing' or self.get('read_only', False)):
			return form.FormNode(self.name)(type='label', value=ac_field.attr('value', ''))
		
		req.content.report('header', tags.style(type="text/css")[
			"""@import '%s';""" % req.get_path('/assets/jquery/jquery.autocomplete.css')])
		
		req.content.report('header', tags.script(type="text/javascript")[
			"""
			function formatItem(item, index, totalItems){
				return item[0].replace('<', '&lt;').replace('>', '&gt;')
			}
			"""
		])
		
		assets.activate_jquery(req)
		req.content.report('header', tags.script(type="text/javascript",
			src=req.get_path("/assets/jquery/jquery.autocomplete.js"))[''])
		req.content.report('header', tags.script(type="text/javascript",
			src=req.get_path("/assets/editable-autocomplete.js"))[''])
		
		frm = form.FormNode('%s-ac-fieldset' % self.name)(type='fieldset', style='brief')
		frm[ac_field.name] = ac_field
		frm[value_field.name] = value_field
		
		return frm
Beispiel #5
0
    def prepare_content(self, req):
        """
		@see: L{modu.web.resource.IContent.prepare_content()}
		"""
        req.content.report(
            "header", tags.style(type="text/css")["@import '%s';" % req.get_path("assets", "admin-styles.css")]
        )

        site_stylesheet = getattr(req.app, "admin_site_stylesheet", None)
        if callable(site_stylesheet):
            site_stylesheet = site_stylesheet(req)
        if site_stylesheet:
            req.content.report("header", tags.style(type="text/css")["@import '%s';" % site_stylesheet])

        user = req["modu.user"]
        if user and user.get_id():
            if req.postpath and req.postpath[0] == "logout":
                req.session.set_user(None)
                if "auth_redirect" in req.session:
                    del req.session["auth_redirect"]
                app.redirect(req.get_path(req.prepath))

            itemdefs = define.get_itemdefs(module_list=self.options.get("itemdef_module", None))

            # get_itemdef_layout adds some data and clones the itemdef
            self.itemdef_layout = define.get_itemdef_layout(req, itemdefs)

            # FIXME: This is inelegant -- we find the current itemdef
            # by using itemdefs.get(req.postpath[1]), so we recreate
            # the itemdef list from the itemdef layout to stop
            # malicious URL access.
            # TODO: Limit the itemdefs by user *first*, then modify
            # get_itemdef_layout() to organize, but not limit.
            if self.itemdef_layout:
                itemdefs = dict(
                    [(itemdef.name, itemdef) for itemdef in reduce(lambda x, y: x + y, self.itemdef_layout.values())]
                )
            else:
                itemdefs = {}

            self.set_slot("itemdef_layout", self.itemdef_layout)

            if len(req.postpath) > 1:
                if req.postpath[0] == "custom":
                    itemdef_name = "/".join(req.postpath[1:])
                    if itemdef_name not in itemdefs:
                        itemdef_name = req.postpath[1]
                else:
                    itemdef_name = req.postpath[1]

                    # we just need to select the right itemdef
                selected_itemdef = itemdefs.get(itemdef_name)

                self.set_slot("selected_itemdef", selected_itemdef)

                if selected_itemdef is not None:
                    configure_store(req, selected_itemdef)

                    if req.postpath[0] == "detail":
                        self.prepare_detail(req, selected_itemdef)
                    elif req.postpath[0] == "autocomplete":
                        self.prepare_autocomplete(req, selected_itemdef)
                    elif req.postpath[0] == "custom":
                        self.prepare_custom(req, selected_itemdef)
                    elif req.postpath[0] in ("listing", "export") and not selected_itemdef.config.get("hidden", False):
                        self.prepare_listing(req, selected_itemdef)
                    else:
                        app.raise404()
                else:
                    app.raise403()
            else:
                default_path = self.options.get("default_path")
                if callable(default_path):
                    default_path = default_path(req)
                if default_path:
                    redirect_path = req.get_path(default_path)
                    app.redirect(redirect_path)

                app.raise404("There is no item list at the path: %s" % req["REQUEST_URI"])
        else:
            self.set_slot("itemdef_layout", None)
            self.set_slot("selected_itemdef", None)
            self.prepare_login(req)