Example #1
0
    def run(self, context):
        # This code builds leaf categories for selection with complete names, 3.8k of them.
        if not isinstance(self.cfg, dict):
            self.cfg = {}
        update_file_path = self.cfg.get('file', None)
        debug_environment = self.cfg.get('debug_environment', False)
        if not update_file_path:
            raise orm.TerminateAction()
        Category = context.models['24']
        gets = datastore.Query('24', namespace=None, keys_only=True).Run()
        keys = list(gets)
        datastore.Delete(keys)
        categories = []
        put_entities = []
        structure = {}
        with file(update_file_path) as f:
            for line in f:
                if not line.startswith('#'):
                    item = line.replace('\n', '')
                    categories.append(item)
                    full_path = item.split(' > ')
                    current_structure = structure
                    for xi, path in enumerate(full_path):
                        if path not in current_structure:
                            current_structure[path] = {}
                        current_structure = current_structure[path]

        for i, item in enumerate(categories):
            full_path = item.split(' > ')
            path_map = structure
            current = full_path
            parent = current[:-1]
            category = {}
            category['id'] = hashlib.md5(''.join(current)).hexdigest()
            if parent:
                category['parent_record'] = Category.build_key(
                    hashlib.md5(''.join(parent)).hexdigest())
            else:
                category['parent_record'] = None
            category['name'] = ' / '.join(current)
            category['state'] = ['indexable']
            leaf = False
            for path in full_path:
                if path in path_map:
                    path_map = path_map[path]
                if not len(path_map):
                    leaf = True
            if leaf:
                category['state'].append(
                    'visible')  # marks the category as leaf
            category = Category(**category)
            category._use_rule_engine = False
            category._use_record_engine = False
            category._use_memcache = False
            category._use_cache = False
            put_entities.append(category)
        tools.log.debug('Writing %s categories' % len(put_entities))
        orm.put_multi(put_entities)
Example #2
0
 def run(self, context):
     if not isinstance(self.cfg, dict):
         self.cfg = {}
     update_file_path = self.cfg.get('file', None)
     if not update_file_path:
         raise orm.TerminateAction()
     Unit = context.models['17']
     keys = Unit.query(Unit.measurement != 'Currency').fetch(keys_only=True)
     orm.delete_multi(keys)  # delete all currencies
     with file(update_file_path) as f:
         tree = ElementTree.fromstring(f.read())
         root = tree.findall('data')
         measurements = {}
         uoms = []
         for child in root[0]:
             if child.attrib.get('model') == 'product.uom.category':
                 for sub_child in child:
                     name = sub_child.text
                 measurements[child.attrib.get('id')] = name
         for child in root[0]:
             if child.attrib.get('model') == 'product.uom':
                 uom = {'id': child.attrib.get('id')[4:]}
                 uom_data = {}
                 for sub_child in child:
                     uom_data[sub_child.attrib.get('name')] = sub_child
                 rounding = uom_data.get('rounding')
                 digits = uom_data.get('digits')
                 if rounding is not None:
                     rounding = Decimal(eval(rounding.attrib.get('eval')))
                 if digits is not None:
                     digits = long(eval(digits.attrib.get('eval')))
                 if digits is None:
                     digits = 3
                 uom.update({
                     'name':
                     uom_data['name'].text,
                     'active':
                     True,
                     'symbol':
                     uom_data['symbol'].text,
                     'measurement':
                     measurements.get(
                         uom_data['category'].attrib.get('ref')),
                     'factor':
                     Decimal(eval(uom_data['factor'].attrib.get('eval'))),
                     'rate':
                     Decimal(eval(uom_data['rate'].attrib.get('eval'))),
                     'rounding':
                     rounding,
                     'digits':
                     digits
                 })
                 uoms.append(uom)
         put_entities = [Unit(**d) for d in uoms]
         for entity in put_entities:
             entity._use_rule_engine = False
         orm.put_multi(put_entities)
Example #3
0
    def run(self, context):
        AccountCacheGroup = context.models['135']
        delete = context.input.get('delete')
        keys = context.input.get('keys')
        if keys:
            keys = context.input.get('keys')
            if keys:
                keys = zlib.decompress(base64.b64decode(keys)).split(',')
        ids = [
            AccountCacheGroup.build_key(id) for id in context.input.get('ids')
        ]
        groups = orm.get_multi(ids)
        save = []
        active = []
        delete_active = []

        def make_active(k):
            return '%s_active' % k

        for i, group in enumerate(groups):
            changes = False
            if not group:
                changes = True
                group = AccountCacheGroup(id=context.input.get('ids')[i],
                                          keys=[])
            for k in keys:
                if k in group.keys:
                    changes = True
                    if delete:
                        group.keys.remove(k)
                        delete_active.extend(
                            [make_active(kk) for kk in group.keys])
                else:
                    changes = True
                    group.keys.append(k)
                active.extend([make_active(kk) for kk in group.keys])
            if changes:
                save.append(group)
        try:
            orm.put_multi(save)
            tools.mem_delete_multi(delete_active)
            tools.mem_set_multi(dict((k, True) for k in active))
        except RequestTooLargeError as e:  # size of entity exceeded
            if not delete:
                delete_keys = []
                for s in save:
                    delete_keys.extend(s.keys)
                    s.keys = keys
                orm.put_multi(save)
                if delete_keys:
                    tools.mem_delete_multi(delete_keys)
Example #4
0
    def run(self, context):
        if not isinstance(self.cfg, dict):
            self.cfg = {}
        update_file_path = self.cfg.get('file', None)
        if not update_file_path:
            raise orm.TerminateAction()
        Unit = context.models['17']
        keys = Unit.query(Unit.measurement == 'Currency').fetch(keys_only=True)
        orm.delete_multi(keys)  # delete all currencies
        with file(update_file_path) as f:
            tree = ElementTree.fromstring(f.read())
            root = tree.findall('data')
            uoms = []

            def __text(item, key):
                value = item.get(key)
                if value is not None:
                    if value.text == 'None' or value.text is None:
                        return None
                    return str(value.text)
                return value

            def __eval(item, key):
                value = item.get(key)
                if value == 'None':
                    value = None
                if value is not None:
                    evaled = value.attrib.get('eval')
                    if evaled == 'None' or evaled is None:
                        return None
                    return eval(evaled)
                return value

            for child in root[1]:
                if child.attrib.get('model') == 'currency.currency':
                    uom = {'id': child.attrib.get('id')}
                    uom_data = {}
                    for sub_child in child:
                        uom_data[sub_child.attrib.get('name')] = sub_child
                    rounding = uom_data.get('rounding')
                    digits = uom_data.get('digits')
                    grouping = uom_data.get('mon_grouping')
                    if rounding is not None:
                        rounding = Decimal(eval(rounding.attrib.get('eval')))
                    if digits is not None:
                        digits = long(eval(digits.attrib.get('eval')))
                    if grouping is not None:
                        grouping = eval(grouping.text)
                    else:
                        grouping = []
                    if digits is None:
                        digits = 2
                    uom.update({
                        'measurement':
                        'Currency',
                        'name':
                        uom_data['name'].text,
                        'code':
                        uom_data['code'].text,
                        'numeric_code':
                        uom_data['numeric_code'].text,
                        'symbol':
                        uom_data['symbol'].text,
                        'rounding':
                        rounding,
                        'digits':
                        digits,
                        'grouping':
                        grouping,
                        'decimal_separator':
                        __text(uom_data, 'mon_decimal_point'),
                        'thousands_separator':
                        __text(uom_data, 'mon_thousands_sep'),
                        'positive_sign_position':
                        __eval(uom_data, 'p_sign_posn'),
                        'negative_sign_position':
                        __eval(uom_data, 'n_sign_posn'),
                        'positive_sign':
                        __text(uom_data, 'positive_sign'),
                        'negative_sign':
                        __text(uom_data, 'negative_sign'),
                        'positive_currency_symbol_precedes':
                        __eval(uom_data, 'p_cs_precedes'),
                        'negative_currency_symbol_precedes':
                        __eval(uom_data, 'n_cs_precedes'),
                        'positive_separate_by_space':
                        __eval(uom_data, 'p_sep_by_space'),
                        'negative_separate_by_space':
                        __eval(uom_data, 'n_sep_by_space'),
                        'active':
                        True
                    })
                    uoms.append(uom)
            put_entities = [Unit(**d) for d in uoms]
            for entity in put_entities:
                entity._use_rule_engine = False
            orm.put_multi(put_entities)
  def run(self, context):
    if not isinstance(self.cfg, dict):
      self.cfg = {}
    update_file_path = self.cfg.get('file', None)
    debug_environment = self.cfg.get('debug_environment', False)
    if not update_file_path:
      raise orm.TerminateAction()
    Country = context.models['12']
    CountrySubdivision = context.models['13']
    with file(update_file_path) as f:
      tree = ElementTree.fromstring(f.read())
      root = tree.findall('data')
      put_entities = []

      def make_complete_name_for_subdivision(entity, parent_id, process):
        path = entity
        names = []
        while True:
          parent = None
          parent_key = getattr(path, 'parent_record')
          if parent_key:
            parent = process.get(parent_key.urlsafe())
          if not parent:
            names.append(getattr(path, 'name'))
            break
          else:
            names.append(getattr(path, 'name'))
            path = parent
        names.reverse()
        return unicode(' / ').join(names)

      i = 0
      no_regions = {}
      for child in root[1]:
        i += 1
        dic = dict()
        dic['id'] = child.attrib['id']
        for sub_child in child:
          name = sub_child.attrib.get('name')
          if name is None:
            continue
          if sub_child.text:
            dic[name] = sub_child.text
        country = Country(name=dic['name'], id=dic['id'], code=dic['code'], active=True)
        country._use_rule_engine = False
        country._use_record_engine = False
        country._use_memcache = False
        country._use_cache = False
        put_entities.append(country)
        no_regions[country.key] = country
      processed_keys = {}
      processed_ids = {}
      i = 0
      for child in [c for c in root[2]] + [c for c in root[3]]:
        i += 1
        dic = dict()
        dic['id'] = child.attrib['id']
        for sub_child in child:
          name = sub_child.attrib.get('name')
          if name is None:
            continue
          if sub_child.text:
            dic[name] = sub_child.text
          if 'ref' in sub_child.attrib:
            dic[name] = sub_child.attrib['ref']
        country_sub_division_values = dict(name=dic['name'], id=dic['id'], type=dic['type'], code=dic['code'], active=True)
        if 'country' in dic:
          country_key = Country.build_key(dic['country'])
          no_regions.pop(country_key, None)
          country_sub_division_values['parent'] = country_key
        if 'parent' in dic:
          parent = processed_ids.get(dic['parent'])
          if parent:
            country_sub_division_values['parent_record'] = parent.key
        country_sub_division = CountrySubdivision(**country_sub_division_values)
        country_sub_division._use_cache = False
        country_sub_division._use_rule_engine = False
        country_sub_division._use_record_engine = False
        country_sub_division._use_memcache = False
        country_sub_division.complete_name = ''
        if 'parent' in dic:
          country_sub_division.complete_name = make_complete_name_for_subdivision(country_sub_division, dic['parent'], processed_keys)
        processed_keys[country_sub_division.key_urlsafe] = country_sub_division
        processed_ids[dic['id']] = country_sub_division
        country_sub_division._use_rule_engine = False
        put_entities.append(country_sub_division)
      orm.put_multi(put_entities)
      put_entities = []
      for country_key, country in no_regions.iteritems():
        country_sub_division = CountrySubdivision(name=country.name, parent=country_key, id=country_key.id(), type='country', code=country.code, active=True)
        country_sub_division._use_cache = False
        country_sub_division._use_rule_engine = False
        country_sub_division._use_record_engine = False
        country_sub_division._use_memcache = False
        country_sub_division.complete_name = country.name
        put_entities.append(country_sub_division)
      orm.put_multi(put_entities)