Index

Graphene

  1. Queries
  2. migrations
  3. UserModel
  4. User Schema
  5. cors
  6. static
  7. headers
  8. payumoney
  9. imageupload
  10. Total count

imageupload

class JewelleryCategory(models.Model):
    """Basic categories of Jewellery"""
    category_name = models.CharField(max_length=255, unique=True)
    category_url = models.CharField(max_length=255, unique=True)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    def __str__(self):
        """Return the category name as a string."""
        return self.category_name

class JewelleryType(models.Model):
    """Type of Jewellery"""
    jewellery_type_name = models.CharField(max_length=255, unique=True)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    def __str__(self):
        """Return the jewellery type as a string."""
        return self.jewellery_type

class JewelleryProduct(models.Model):
    """Jewellery Product"""
    product_name = models.CharField(max_length=255)
    description = models.TextField()
    jewellery_category = models.ForeignKey(
        JewelleryCategory,
        on_delete=models.CASCADE,
        related_name="jewellerycategory_keywords",
        related_query_name="jewellerycategory_keyword",
    )
    jewellery_type = models.ForeignKey(
        JewelleryType,
        on_delete=models.CASCADE,
        related_name="jewellerytype_keywords",
        related_query_name="jewellerytype_keyword",
        null=True,
        blank=True
    )
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    def __str__(self):
        """Return the jewellery product name."""
        return self.product_name



class JewelleryProductImage(models.Model):
    """Jewellery Product Image"""
    product = models.ForeignKey(
        JewelleryProduct, 
        on_delete=models.CASCADE,
        related_name="jewellery_images"
        )
    image = models.ImageField(upload_to="images")
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    def __str__(self):
        """Return the jewellery product name."""
        return self.product.product_name





from graphql_relay.node.node import from_global_id



class CreateProduct(AuthMutation, relay.ClientIDMutation):
    permission_classes = (AllowAny, )
    jewellery_product = graphene.Field(JewelleryProductNode)
    jewellery_product_image = graphene.Field(JewelleryProductImageNode)
    success = graphene.String()

    class Input:
        product_name = graphene.String(required=True)
        description = graphene.String(required=True)
        jewellery_category =graphene.ID(required=True)
        jewellery_type = graphene.ID()

    @classmethod
    def mutate_and_get_payload(cls, root, info, **input):
        if cls.has_permission(root, info, input):
            product_name = input.get('product_name')
            description = input.get('description')
            jewellery_category_qid = input.get('jewellery_category')
            jewellery_category_id = from_global_id(jewellery_category_qid)[1]
            print(jewellery_category_id)
            j_category_obj = JewelleryCategory.objects.get(pk = jewellery_category_id)
            jewellery_type_qid = input.get('jewellery_type'None)
            files = info.context.FILES['imageItem']
            jewellery_type_id = ""
            if jewellery_type_qid is not None :
                jewellery_type_id = from_global_id(jewellery_type_qid)[1]
                j_type_obj = JewelleryType.objects.get(pk = jewellery_type_id)
                productobj = JewelleryProduct(product_name=product_name, description=description, jewellery_category=j_category_obj, jewellery_type=j_type_obj)
                productobj.save()
            else:
                productobj = JewelleryProduct(product_name=product_name, description=description, jewellery_category=j_category_obj)
                productobj.save()
                print(productobj)


            imgobj = JewelleryProductImage(product=productobj, image=files)
            imgobj.save()

            return CreateProduct(jewellery_product=productobj, jewellery_product_image=imgobj, success=True)
        else:
            raise Exception('Permission Denied')

class ProductMutation(ObjectType):
    create_product = CreateProduct.Field()


images/486-1.png

images/486-2.png

query



mutation {
    createProduct(
        input:{productName: "Sakhura Kada", description: "Beautiful Kada", jewelleryCategory:"SmV3ZWxsZXJ5Q2F0ZWdvcnlOb2RlOjE="}
    ) {
     jewelleryProduct {
      productName
      description
       
    }
     jewelleryProductImage {
  
      image
    }

    }
  }