class CountriesZones(OrderedTable): class_id = 'countries-zones' class_title = MSG(u'Countries Zones') class_handler = BaseCountriesZones class_views = ['view', 'add_record'] class_version = '20090923' view = CountriesZones_View() add_record = Table_AddRecord(title=MSG(u'Add a new zone')) form = [ TextWidget('title', title=MSG(u'Country title')), BooleanRadio('has_tax', title=MSG(u'Has TAX ?')) ] @staticmethod def _make_resource(cls, folder, name, *args, **kw): OrderedTable._make_resource(cls, folder, name) table = BaseCountriesZones() zones = [] csv = ro_database.get_handler(get_abspath('data/countries.csv'), CSVFile) for line in csv.get_rows(): zone = unicode(line[1], 'utf-8') if zone not in zones: zones.append(zone) table.add_record({'title': Property(zone, language='fr')}) folder.set_handler(name, table)
def action_on_success(self, resource, context): # XXX Set image as public form = Table_AddRecord._get_form(self, resource, context) path = form['name'] img_resource = resource.get_resource(str(path)) img_resource.set_property('state', 'public') return context.come_back(MSG(u'New record added.'))
class Countries(Table): class_id = 'countries' class_title = MSG(u'Countries') class_handler = BaseCountries class_views = ['view', 'add_record'] view = Countries_View() add_record = Table_AddRecord(title=MSG(u'Add a new country')) form = [ TextWidget('title', title=MSG(u'Country title')), SelectWidget('zone', title=MSG(u'Zone')), BooleanRadio('enabled', title=MSG(u'Enabled')), ] # XXX Enabled means its enabled for delivery not for registration # People from south africa can register on website but not to be delivered # This distinction must be explicit @staticmethod def _make_resource(cls, folder, name, *args, **kw): Table._make_resource(cls, folder, name) # Import CSV with list of countries zones = [] table = BaseCountries() csv = ro_database.get_handler(get_abspath('data/countries.csv'), CSVFile) for line in csv.get_rows(): country = unicode(line[0], 'utf-8') zone = unicode(line[1], 'utf-8') if zone not in zones: zones.append(zone) table.add_record({ 'title': Property(country, language='fr'), 'zone': str(zones.index(zone)), 'enabled': True }) folder.set_handler(name, table)
def _get_form(self, resource, context): form = Table_AddRecord._get_form(self, resource, context) # We check if the image path refer to an Image instance path = form['name'] check_photo(path, resource, 'name') return form
def action_add_record(self, resource, context, form): return Table_AddRecord.action(self, resource, context, form)
def get_namespace(self, resource, context): namespace = Table_AddRecord.get_namespace(self, resource, context) # actions namespace namespace['actions'] = self._get_action_namespace(resource, context) return namespace
def action_add_or_edit(self, resource, context, record): # We add current user record['user'] = context.user.name # Normal action Table_AddRecord.action_add_or_edit(self, resource, context, record)