def importXLS(self, action): """ Create and handle form button.""" # Extract form field values and errors from HTTP request data, errors = self.extractData() if errors: self.status = self.formErrorsMessage return try: io = StringIO(data["xls_file"].data) workbook = xlrd.open_workbook(file_contents=io.read()) except (KeyError, TypeError, xlrd.XLRDError): self.abort_action(action, (_(u'Invalid XLS file'), )) return errors = [] report_error = lambda err: errors.append(err) records = len(import_xls(self.context, workbook, report_error)) if errors: self.abort_action(action, errors) else: self.status = _(u'Imported ${number} items', mapping={u'number': records})
def importXLS(self, action): """ Create and handle form button.""" # Extract form field values and errors from HTTP request data, errors = self.extractData() if errors: self.status = self.formErrorsMessage return try: io = StringIO(data["xls_file"].data) workbook = xlrd.open_workbook(file_contents=io.read()) except (KeyError, TypeError, xlrd.XLRDError): self.abort_action(action, (_(u'Invalid XLS file'),)) return errors = [] report_error = lambda err: errors.append(err) records = len(import_xls(self.context, workbook, report_error)) if errors: self.abort_action(action, errors) else: self.status = _(u'Imported ${number} items', mapping={u'number': records})
class IDirectoryItemBase(IDirectoryRoot): searchable('title') title = TextLine(title=_(u'Name'), ) searchable('description') description = Text(title=_(u'Description'), required=False, default=u'', missing_value=u'')
class IDirectoryItemBase(IDirectoryRoot): """Single entry of a directory. Usually you would not want to directly work with this class. Instead refer to IDirectoryItem below. """ searchable('title') title = TextLine(title=_(u'Name'), ) searchable('description') description = Text(title=_(u'Description'), required=False, default=u'', missing_value=u'') searchable('cat1') cat1 = List( title=_(u'1st Category Name'), description=_(u'Start typing and select a category. ' u'To add a new category write the name and hit enter.'), value_type=TextLine(), required=False, ) searchable('cat2') cat2 = List( title=_(u'2nd Category Name'), description=_(u'Start typing and select a category. ' u'To add a new category write the name and hit enter.'), value_type=TextLine(), required=False, ) searchable('cat3') cat3 = List( title=_(u'3rd Category Name'), description=_(u'Start typing and select a category. ' u'To add a new category write the name and hit enter.'), value_type=TextLine(), required=False, ) searchable('cat4') cat4 = List( title=_(u'4th Category Name'), description=_(u'Start typing and select a category. ' u'To add a new category write the name and hit enter.'), value_type=TextLine(), required=False, )
def get_filter_terms(context, request): """Unpacks the filter terms from a request. For example: url?cat1=One&cat2=Two Will result in: { 'cat1': 'One', 'cat2': 'Two' } """ terms = {} # "Any" is also defined in search.pt empty = (u'', translate(context, request, _(u'Any'))) for key, value in request.items(): if key.startswith('cat'): if isinstance(value, basestring): text = request[key].decode('utf-8') if text not in empty: terms[key] = remove_count(text) else: texts = [item.decode('utf-8') for item in request[key]] terms[key] = [remove_count(item) for item in texts] return terms
def get_map(context): itemfields = ( 'title', 'description', 'cat1', 'cat2', 'cat3', 'cat4', 'coordinates_json', 'absolute_url' ) itemmap = FieldMap() itemmap.root = True itemmap.keyfields = ('title',) itemmap.typename = 'seantis.dir.base.item' itemmap.add_fields(itemfields) itemmap.add_title('coordinates_json', _(u'Coordinates (JSON)')) itemmap.add_title('absolute_url', 'Url') itemmap.mark_readonly('absolute_url') # the acqusition context needs to be intact for absolute_url, which is # not the default itemmap.bind_transformer('absolute_url', lambda obj: obj) add_category_binds(itemmap) try: adapter = component.getAdapter(context, IFieldMapExtender) adapter.extend_import(itemmap) except component.ComponentLookupError: pass return itemmap
def validate_email(value): try: if value: checkEmailAddress(value) except EmailAddressInvalid: raise Invalid(_(u'Invalid email address')) return True
def get_map(context): itemfields = ( 'title', 'description', 'cat1', 'cat2', 'cat3', 'cat4', 'coordinates_json', 'absolute_url' ) itemmap = FieldMap() itemmap.root = True itemmap.keyfields = ('title',) itemmap.typename = 'seantis.dir.base.item' itemmap.add_fields(itemfields) itemmap.add_title('coordinates_json', _(u'Coordinates (JSON)')) itemmap.add_title('absolute_url', 'Url') itemmap.mark_readonly('absolute_url') listwrap = lambda val: ','.join(val) listunwrap = lambda val: [v.strip() for v in val.split(',')] for cat in CATEGORIES: itemmap.bind_wrapper(cat, listwrap) itemmap.bind_unwrapper(cat, listunwrap) try: adapter = component.getAdapter(context, IFieldMapExtender) adapter.extend_import(itemmap) except component.ComponentLookupError: pass return itemmap
def get_map(context): itemfields = ('title', 'description', 'cat1', 'cat2', 'cat3', 'cat4', 'coordinates_json', 'absolute_url') itemmap = FieldMap() itemmap.root = True itemmap.keyfields = ('title', ) itemmap.typename = 'seantis.dir.base.item' itemmap.add_fields(itemfields) itemmap.add_title('coordinates_json', _(u'Coordinates (JSON)')) itemmap.add_title('absolute_url', 'Url') itemmap.mark_readonly('absolute_url') # the acqusition context needs to be intact for absolute_url, which is # not the default itemmap.bind_transformer('absolute_url', lambda obj: obj) add_category_binds(itemmap) try: adapter = component.getAdapter(context, IFieldMapExtender) adapter.extend_import(itemmap) except component.ComponentLookupError: pass return itemmap
def get_map(context): itemfields = ('title', 'description', 'cat1', 'cat2', 'cat3', 'cat4', 'coordinates_json', 'absolute_url') itemmap = FieldMap() itemmap.root = True itemmap.keyfields = ('title', ) itemmap.typename = 'seantis.dir.base.item' itemmap.add_fields(itemfields) itemmap.add_title('coordinates_json', _(u'Coordinates (JSON)')) itemmap.add_title('absolute_url', 'Url') itemmap.mark_readonly('absolute_url') listwrap = lambda val: ','.join(val) listunwrap = lambda val: [v.strip() for v in val.split(',')] for cat in CATEGORIES: itemmap.bind_wrapper(cat, listwrap) itemmap.bind_unwrapper(cat, listunwrap) try: adapter = component.getAdapter(context, IFieldMapExtender) adapter.extend_import(itemmap) except component.ComponentLookupError: pass return itemmap
def verify(obj, fieldmap, error): msgs = [] if not has_requirements(obj, fieldmap): msgs.append(_(u'Not all required fields have a value')) try: fieldmap.interface.validateInvariants(obj) except Exception, e: msgs.append(unicode(e.message))
class DefaultExport(grok.Subscription): grok.context(IDirectoryBase) grok.provides(IExportProvider) id = 'default' title = _(u'Default Export') layer = None description = _( u'The default export contains the basic fields of the ' u'directory, without additional fields defined by client specific ' u'fields. It is the only export which may be also be imported.') @property def url(self): return self.context.absolute_url() + '/export' def export(self, request): pass
def import_xls(directory, workbook, error=lambda e: None): # Get the fieldmap defining hierarchies and the cell/field mapping fieldmap = get_map(directory) # Load the sorted values from the workbook try: values = get_values(workbook, fieldmap) except IndexError: error(_(u'Invalid XLS file')) return dict() # Fill the database if values: return generate_objects(directory, fieldmap, values, error) else: return dict()
class IDirectoryItemCategories(IDirectoryRoot): searchable('cat1') cat1 = List( title=_(u'1st Category Name'), description=_(u'Start typing and select a category. ' u'To add a new category write the name and hit enter.'), value_type=TextLine(), required=False, ) searchable('cat2') cat2 = List( title=_(u'2nd Category Name'), description=_(u'Start typing and select a category. ' u'To add a new category write the name and hit enter.'), value_type=TextLine(), required=False, ) searchable('cat3') cat3 = List( title=_(u'3rd Category Name'), description=_(u'Start typing and select a category. ' u'To add a new category write the name and hit enter.'), value_type=TextLine(), required=False, ) searchable('cat4') cat4 = List( title=_(u'4th Category Name'), description=_(u'Start typing and select a category. ' u'To add a new category write the name and hit enter.'), value_type=TextLine(), required=False, )
class IDirectoryBase(IDirectoryRoot): """Container for all directory items.""" title = TextLine(title=_(u'Name'), ) subtitle = TextLine(title=_(u'Subtitle'), required=False) description = Text(title=_(u'Description'), required=False, default=u'') cat1 = TextLine(title=_(u'1st Category Name'), required=False, default=u'') cat1_suggestions = List( title=_(u'Suggested Values for the 1st Category'), required=False, description=_( u'These values are suggested when typing category values in the ' u'category items, in addition to values found in other items.'), value_type=TextLine(), ) cat2 = TextLine(title=_(u'2nd Category Name'), required=False, default=u'') cat2_suggestions = List( title=_(u'Suggested Values for the 2nd Category'), required=False, description=_( u'These values are suggested when typing category values in the ' u'category items, in addition to values found in other items.'), value_type=TextLine(), ) cat3 = TextLine(title=_(u'3rd Category Name'), required=False, default=u'') cat3_suggestions = List( title=_(u'Suggested Values for the 3rd Category'), required=False, description=_( u'These values are suggested when typing category values in the ' u'category items, in addition to values found in other items.'), value_type=TextLine(), ) cat4 = TextLine(title=_(u'4th Category Name'), required=False, default=u'') cat4_suggestions = List( title=_(u'Suggested Values for the 4th Category'), required=False, description=_( u'These values are suggested when typing category values in the ' u'category items, in addition to values found in other items.'), value_type=TextLine(), ) enable_filter = Bool(title=_(u'Enable filtering'), required=True, default=True) enable_search = Bool(title=_(u'Enable searching'), required=True, default=True) enable_map = Bool(title=_(u'Enable the map'), required=True, default=True) allow_custom_categories = Bool( title=_(u'Allow custom categories'), description=_( u'If custom categories are allowed, items may specify new ' u'categories. If they are not allowed, people can only choose ' u'from the given suggestions.'), required=True, default=True, )
def title(self): return ' '.join((_(u'Exports for'), self.context.title))
from seantis.dir.base import _ from seantis.dir.base.utils import get_current_language from seantis.dir.base.fieldmap import get_map from seantis.dir.base.directory import IDirectoryBase from seantis.dir.base.utils import get_interface_fields class FatalImportError(Exception): """Import error caused by a programming error.""" class IImportDirectorySchema(form.Schema): """ Define fields used on the form """ xls_file = NamedFile(title=_(u"XLS file"), description=u'{xlshelp}') IMPORTHELP = _(u"Import directory items using xls. The xls is expected to have the format defined in the template which you can <a href='#xlstemplate'>download here</a>") class Import(form.Form): label = _(u'Import directory items') fields = field.Fields(IImportDirectorySchema) grok.context(IDirectoryBase) grok.require('cmf.AddPortalContent') grok.name('import') ignoreContext = True def abort_action(self, action, messages): """ Aborts the given actiona and adds the list of messages as error-widgets to the form.
class Import(form.Form): label = _(u'Import directory items') fields = field.Fields(IImportDirectorySchema) grok.context(IDirectoryBase) grok.require('cmf.ManagePortal') grok.name('import') ignoreContext = True def abort_action(self, action, messages): """ Aborts the given actiona and adds the list of messages as error-widgets to the form. """ form = action.form formcontent = form.getContent() request = action.request for msg in messages: args = (Invalid(msg), request, None, None, form, formcontent) err = InvalidErrorViewSnippet(*args) err.update() form.widgets.errors += (err, ) form.status = form.formErrorsMessage transaction.abort() def add_help(self, result): """ Adds an url to the description of the xls import file. Probably not the plone way of doing it, but I have yet to find a working alternative. """ importlink = self.context.absolute_url() + '/export?template=1' language = get_current_language(self.context, self.request) text = translate(IMPORTHELP, target_language=language) help = text.replace('#xlstemplate', importlink) return result.replace('{xlshelp}', help) def render(self, *args, **kwargs): """ Overrides the render function to meddle with the html. """ result = form.Form.render(self, *args, **kwargs) return self.add_help(result) @buttonAndHandler(_(u'Import'), name='import') def importXLS(self, action): """ Create and handle form button.""" # Extract form field values and errors from HTTP request data, errors = self.extractData() if errors: self.status = self.formErrorsMessage return try: io = StringIO(data["xls_file"].data) workbook = xlrd.open_workbook(file_contents=io.read()) except (KeyError, TypeError, xlrd.XLRDError): self.abort_action(action, (_(u'Invalid XLS file'), )) return errors = [] report_error = lambda err: errors.append(err) records = len(import_xls(self.context, workbook, report_error)) if errors: self.abort_action(action, errors) else: self.status = _(u'Imported ${number} items', mapping={u'number': records})
class IImportDirectorySchema(form.Schema): """ Define fields used on the form """ xls_file = NamedFile(title=_(u"XLS file"), description=u'{xlshelp}')
from seantis.dir.base.fieldmap import get_map from seantis.dir.base.directory import IDirectoryBase from seantis.dir.base.utils import get_interface_fields class FatalImportError(Exception): """Import error caused by a programming error.""" class IImportDirectorySchema(form.Schema): """ Define fields used on the form """ xls_file = NamedFile(title=_(u"XLS file"), description=u'{xlshelp}') IMPORTHELP = _( u"Import directory items using xls. The xls is expected to have the format defined in the template which you can <a href='#xlstemplate'>download here</a>" ) class Import(form.Form): label = _(u'Import directory items') fields = field.Fields(IImportDirectorySchema) grok.context(IDirectoryBase) grok.require('cmf.ManagePortal') grok.name('import') ignoreContext = True def abort_action(self, action, messages): """ Aborts the given actiona and adds the list of messages as
class IDirectoryBase(IDirectoryRoot): """Container for all directory items.""" title = TextLine(title=_(u'Name'), ) subtitle = TextLine(title=_(u'Subtitle'), required=False) description = Text(title=_(u'Description'), required=False, default=u'') cat1 = TextLine(title=_(u'1st Category Name'), required=False, default=u'') cat1_suggestions = List( title=_(u'Suggested Values for the 1st Category'), required=False, description=_( u'These values are suggested when typing category values in the ' u'category items, in addition to values found in other items.'), value_type=TextLine(), ) cat2 = TextLine(title=_(u'2nd Category Name'), required=False, default=u'') cat2_suggestions = List( title=_(u'Suggested Values for the 2nd Category'), required=False, description=_( u'These values are suggested when typing category values in the ' u'category items, in addition to values found in other items.'), value_type=TextLine(), ) cat3 = TextLine(title=_(u'3rd Category Name'), required=False, default=u'') cat3_suggestions = List( title=_(u'Suggested Values for the 3rd Category'), required=False, description=_( u'These values are suggested when typing category values in the ' u'category items, in addition to values found in other items.'), value_type=TextLine(), ) cat4 = TextLine(title=_(u'4th Category Name'), required=False, default=u'') cat4_suggestions = List( title=_(u'Suggested Values for the 4th Category'), required=False, description=_( u'These values are suggested when typing category values in the ' u'category items, in addition to values found in other items.'), value_type=TextLine(), ) child_modified = Datetime( title=_(u'Last time a DirectoryItem was modified'), required=False, readonly=True) enable_filter = Bool(title=_(u'Enable filtering'), required=True, default=True) enable_search = Bool(title=_(u'Enable searching'), required=True, default=True)