Best Python code snippet using avocado_python
json_schema.py
Source:json_schema.py
2import sys3from typing import Any, Dict, List4from dhis2.core.metadata.models import Property, Schema5log = logging.getLogger(__name__)6def handle_reference(dhis2_property: Property, property: Dict[str, Any]):7 if dhis2_property.identifiableObject and not dhis2_property.embeddedObject:8 property["$ref"] = "#/definitions/identifiableObject"9 else:10 property["$ref"] = f"#/definitions/{dhis2_property.name}"11def handle_collection(dhis2_property: Property, property: Dict[str, Any]):12 property["type"] = "array"13 if "REFERENCE" in dhis2_property.itemPropertyType:14 property["item"] = {"$ref": "#/definitions/identifiableObject"}15 else:16 property["item"] = {"$ref": f"#/definitions/{dhis2_property.name}"}17def handle_property(dhis2_property: Property):18 if (not dhis2_property.persisted or not dhis2_property.owner) and not dhis2_property.required:19 return None20 property = {}21 if dhis2_property.description:22 property["description"] = dhis2_property.description23 if "BOOLEAN" in dhis2_property.propertyType:24 property["type"] = "boolean"25 elif "INTEGER" in dhis2_property.propertyType:26 property["type"] = "integer"27 elif "NUMBER" in dhis2_property.propertyType:28 property["type"] = "number"29 elif "EMAIL" in dhis2_property.propertyType:30 property["type"] = "string"31 property["format"] = "email"32 elif "URL" in dhis2_property.propertyType:33 property["type"] = "string"34 property["format"] = "uri"35 elif "DATE" in dhis2_property.propertyType:36 property["type"] = "date-time"37 elif "TEXT" in dhis2_property.propertyType:38 property["type"] = "string"39 elif "PHONENUMBER" in dhis2_property.propertyType:40 property["type"] = "string"41 elif "GEOLOCATION" in dhis2_property.propertyType:42 property["type"] = "string"43 elif "COLOR" in dhis2_property.propertyType:44 property["type"] = "string"45 elif "IDENTIFIER" in dhis2_property.propertyType:46 property["type"] = "string"47 elif "CONSTANT" in dhis2_property.propertyType:48 property["type"] = "string"49 property["enum"] = dhis2_property.constants50 elif "COMPLEX" in dhis2_property.propertyType and (51 dhis2_property.embeddedObject or not dhis2_property.identifiableObject52 ):53 handle_reference(dhis2_property, property)54 elif "REFERENCE" in dhis2_property.propertyType and dhis2_property.identifiableObject:55 handle_reference(dhis2_property, property)56 elif "COLLECTION" in dhis2_property.propertyType:57 handle_collection(dhis2_property, property)58 else:59 print(f"PROPERTY={dhis2_property}", file=sys.stderr)60 return property61def handle_object(dhis2_schema: Schema):62 object_property = {63 "type": "object",64 "properties": {},65 "required": [],66 }67 for dhis2_property in dhis2_schema.properties:68 if property := handle_property(dhis2_property):69 name = dhis2_property.collectionName if dhis2_property.collection else dhis2_property.name...
product.py
Source:product.py
...9from forj.utils.math import expr10class ProductQuerySet(base.QuerySet):11 def from_reference(self, reference):12 for product in self:13 if product.handle_reference(reference):14 return product15 raise exceptions.InvalidProductRef(16 "Product ref {} is not available".format(reference)17 )18class ProductManager(base.Manager):19 def get_queryset(self):20 return ProductQuerySet(self.model)21 @cache_for_request22 def cache(self):23 return list(self.order_by("price", "-condition"))24 def cached_from_reference(self, reference):25 products = self.cache()26 for product in products:27 if product.handle_reference(reference):28 return product29 raise exceptions.InvalidProductRef(30 "Product ref {} is not available".format(reference)31 )32 def from_reference(self, reference):33 return self.order_by("price", "-condition").from_reference(reference)34class Product(base.Model):35 name = models.CharField(max_length=100, verbose_name="Name")36 reference = models.CharField(37 max_length=100, verbose_name="Reference", db_index=True38 )39 description = models.TextField(null=True, verbose_name="Description", blank=True)40 price = AmountField(verbose_name="Price", null=True, blank=True)41 formula = models.CharField(42 max_length=100, verbose_name="Formula", null=True, blank=True43 )44 condition = models.CharField(45 max_length=100, verbose_name="Condition", null=True, blank=True46 )47 currency = models.CharField(48 max_length=3,49 choices=constants.CURRENCY_CHOICES,50 default=settings.DEFAULT_CURRENCY,51 )52 shipping_cost = AmountField(null=True, verbose_name="Shipping cost", default=0)53 tax_cost = AmountField(null=True, verbose_name="Tax cost", default=0)54 objects = ProductManager()55 def __init__(self, *args, **kwargs):56 super().__init__(*args, **kwargs)57 class Meta:58 db_table = "forj_product"59 abstract = False60 def __str__(self):61 return "{}: {}".format(self.name, self.reference)62 def handle_reference(self, reference):63 if self.reference == reference:64 return True65 criteria_set = CriteriaSet.from_reference(reference)66 if criteria_set.is_empty() and self.criteria_set.is_empty():67 return False68 if criteria_set in self.criteria_set and len(criteria_set) == len(69 self.criteria_set70 ):71 if not self.condition:72 return True73 cond = self.condition74 for criteria in criteria_set:75 cond = cond.replace(criteria.name, criteria.value)76 if expr(cond):...
serializers.py
Source:serializers.py
...56 def save(self): 57 reference_type = self.validated_data.pop("reference_type")58 reference_id = self.validated_data.pop("reference_id")59 instance = super().save()60 self.handle_reference(instance, reference_type, reference_id)61 return instance62 @staticmethod63 def handle_reference(payment, reference_type, reference_id):64 if reference_type == "purchase_order":65 order = PurchaseOrder.objects.get(id=reference_id)66 order.payment_id = payment.id67 order.save()68 elif reference_type == "sale_order":69 order = SalesOrder.objects.get(id=reference_id)70 order.payment_id = payment.id71 order.save()72 elif reference_type == "expense":73 expense = Expense.objects.get(id=reference_id)74 expense.payment_id = payment.id75 expense.save()...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!