def code_from_vendor(self, vidcode, idcode): #TODO move thid at application level. if vidcode == 'BLB': idcode = idcode.split(' ')[:-1] else: idcode = idcode.split(' ') return slugify('_'.join(idcode).upper())
def get_or_create(self, commit = True, type = None, country = None, default_vendor = None, **kwargs): ''' Override get_or_create. kwargs must contain: code country default_vendor Optional type tags ''' code = kwargs.pop('code',None) if not code: raise ValueError('cannot add data id, code not specified') code = slugify(code.upper()) id = None try: id = self.get(code = code) created = False if default_vendor: default_vendor = convert('vendor', default_vendor) except: isin = kwargs.get('isin',None) if isin: id = self.filter(isin = isin) if id.count() == 1: id = id[0] created = False elif id: return None,False else: id = None if not id: id = None default_vendor = convert('vendor', default_vendor or settings.DEFAULT_VENDOR_FOR_SITE) created = True if default_vendor: default_vendor = default_vendor.id ct, model = self.ctmodel(type) country = convert('country', country) model = model or self.model id = model.objects.create(id, commit = commit, code = code, country = country, default_vendor = default_vendor, **kwargs) return id,created
def keywords(self): key = slugify(self.code.replace('/', ' '), '_') keys = key.split('_') kk = [] if self.parent: keys.extend(self.parent.keywords()) for key in keys: key = key.lower() if key not in kk: kk.append(key) return kk
def keywords(self): key = slugify(self.code.replace('/',' '),'_') keys = key.split('_') kk = [] if self.parent: keys.extend(self.parent.keywords()) for key in keys: key = key.lower() if key not in kk: kk.append(key) return kk
def get_or_create(self, commit=True, type=None, country=None, default_vendor=None, **kwargs): """ Override get_or_create. kwargs must contain: code country default_vendor Optional type tags """ code = kwargs.pop("code", None) if not code: raise ValueError("cannot add data id, code not specified") code = slugify(code.upper()) id = None try: id = self.get(code=code) created = False if default_vendor: default_vendor = convert("vendor", default_vendor) except: isin = kwargs.get("isin", None) if isin: id = self.filter(isin=isin) if id.count() == 1: id = id[0] created = False elif id: return None, False else: id = None if not id: id = None default_vendor = convert("vendor", default_vendor or settings.DEFAULT_VENDOR_FOR_SITE) created = True if default_vendor: default_vendor = default_vendor.id ct, model = self.ctmodel(type) country = convert("country", country) model = model or self.model id = model.objects.create( id, commit=commit, code=code, country=country, default_vendor=default_vendor, **kwargs ) return id, created
def createsingle(self, item, commit): code = item.pop('code',None) idcode, vids = self.vids_from_data(item) if not code: code = idcode else: code = slugify(code).upper() try: id, created = self.model.objects.get_or_create(code = code, commit = commit, **item) assert id , "Assertion Error: The ID was not created Code: %s Item %s" %(code , item) self.add_vids(id,vids) if created: v = 'Created %s' % id.code else: v = 'Modified %s' % id.code return id,created,v except Exception, e: return None,False,str(e)
def createsingle(self, item, commit): code = item.pop('code', None) idcode, vids = self.vids_from_data(item) if not code: code = idcode else: code = slugify(code).upper() try: id, created = self.model.objects.get_or_create(code=code, commit=commit, **item) assert id, "Assertion Error: The ID was not created Code: %s Item %s" % ( code, item) self.add_vids(id, vids) if created: v = 'Created %s' % id.code else: v = 'Modified %s' % id.code return id, created, v except Exception, e: return None, False, str(e)