def append_stitches(self, stitches): if not self.contrast_stitches: return for conf in stitches: try: self.cache_builder.create_cache( conf, ('image', ), resolution=self.resolution, field_types={'image': STITCHES}) cache = conf.cache.get(source_field='image', resolution=self.resolution) except Exception as e: print(e.message) continue stitches_conf = ImageConf.for_cache(cache) ct = ContentType.objects.get_for_model(conf.content_object) relation = compose.StitchColor.objects.get(content_type_id=ct.id) element = relation.element_id element_info = first(lambda s: s['element'] == element, self.contrast_stitches) image = Image.open(cache.file.path) if image.mode == 'L' and element_info: color = dictionaries.StitchColor.objects.get( pk=element_info["color"]) back = Image.new('RGB', image.size, hex_to_rgb(color.color)) back.putalpha(image) stitches_conf.image = back if conf.type == compose.StitchesSource.STITCHES_TYPE.under: self.lower_stitches.append(stitches_conf) else: self.upper_stitches.append(stitches_conf)
def import_contrast_stitch(self, instance, element, color): if instance.marked_new: stitch = None else: stitch = first(lambda cs: cs.element.title == element, instance.contrast_stitches.all()) if color: element = first(lambda e: e['title'] == element, self.stitch_elements) color = first(lambda c: c['title'] == color, self.stitch_colors) if stitch is None: stitch = ContrastStitch(shirt=instance, element_id=element['id'], color_id=color['id']) stitch.save() elif stitch is not None: stitch.delete()
def _fabric_price(self, fabric, collection): fabric_price_filter = lambda x: x.fabric_category_id == fabric.category_id fabric_price = first(fabric_price_filter, collection.storehouse.prices.all()) if fabric_price is not None: return fabric_price.price return 0
def import_contrast_detail(self, instance, element, fabric_code): if instance.marked_new: detail = None else: detail = first(lambda cd: cd.element == element, instance.contrast_details.all()) if fabric_code: fabric = first(lambda f: f.code == fabric_code, self.fabrics) assert fabric is not None, 'Could find fabric with the following fabric code: %s' % fabric_code if detail is None: detail = ContrastDetails(shirt=instance, element=element, fabric=fabric) # ignore pricing for now detail.ignore_signals = True detail.save() elif detail is not None: detail.delete()
def save_instance(self, instance, dry_run=False): self.before_save_instance(instance, dry_run) if not dry_run: instance.save() if instance.residuals_set is not None: for country, amount in instance.residuals_set.iteritems(): if country == 'Fabric': continue try: amount = float(amount) except (ValueError, TypeError): amount = 0 storehouse = first(lambda s: s.country == country, self.storehouses) residual = first(lambda x: x.storehouse == storehouse, instance.residuals.all()) if residual is None: residual = FabricResidual.objects.create( fabric=instance, storehouse=storehouse) residual.amount = amount residual.save() self.after_save_instance(instance, dry_run)
def append_contrasting_part(self, conf, model, elements): if self.contrast_details is None: return self.append_model(model) self.cache_builder.create_cache(model, ('uv', 'light', 'ao'), resolution=self.resolution) if not self.collection.contrast_details: return self.append_solid_contrasting_part( model, self.collection.white_fabric) part_keys = set(dict(elements).keys()) part_details = [ k for k in self.contrast_details if k['element'] in part_keys ] fabrics = {d['fabric'] for d in part_details} if not part_details: return self.append_model(model) if len(part_details) == len(part_keys) and len(fabrics) == 1: return self.append_solid_contrasting_part(model, fabrics.pop()) self.append_model(model) detail_masks = [] for detail in part_details: mask = conf.masks.filter(element=detail['element'], projection=self.projection).first() if mask: self.cache_builder.create_cache(mask, ('mask', ), resolution=self.resolution) detail_masks.append((detail, mask)) if detail_masks: self.append_granular_contrasting_part(model, detail_masks) # for external cuff part there's no mask: we just replace whole manget else: cuff_element = first( lambda x: x['element'] == ContrastDetails.CUFF_ELEMENTS. cuff_outer, part_details) if cuff_element: self.append_solid_contrasting_part(model, cuff_element['fabric'])
def get_other_address(self): return first( lambda x: x.type == CustomerData.ADDRESS_TYPE.other_address, self.customer_data.all())
def get_residual(self, fabric, storehouse): residual_predicate = lambda residual: residual.storehouse == storehouse residual = first(residual_predicate, fabric.residuals.all()) return residual.amount if residual else None
def get_price(self, obj): return first(lambda x: x["fabric_category"] == obj.category_id, obj.cached_collection.prices, {}).get('price')