Skip to content

Instantly share code, notes, and snippets.

@chapimenge3
Last active March 25, 2024 20:23
Show Gist options
  • Save chapimenge3/af426615d9396c8d9c7f64ee059967ce to your computer and use it in GitHub Desktop.
Save chapimenge3/af426615d9396c8d9c7f64ee059967ce to your computer and use it in GitHub Desktop.
SoleStep Blog Code Snippets
from django.db import models
from django.contrib.auth.models import AbstractUser
class User(AbstractUser):
telegram_id = models.CharField(max_length=255, unique=True)
username = models.CharField(max_length=255)
first_name = models.CharField(max_length=255)
last_name = models.CharField(max_length=255)
phone_number = models.CharField(max_length=255)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def __str__(self):
return self.username
class Meta:
indexes = [
models.Index(fields=['telegram_id'], name='telegram_id_idx'),
]
class Channel(models.Model):
channel_id = models.CharField(max_length=255, unique=True)
name = models.CharField(max_length=255)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def __str__(self):
return self.name
class Meta:
indexes = [
models.Index(fields=['channel_id'], name='channel_id_idx'),
]
from django.contrib.auth import get_user_model
from django.db import models
from django.utils.translation import gettext_lazy as _
User = get_user_model()
class Product(models.Model):
name = models.CharField(max_length=255)
price = models.DecimalField(max_digits=10, decimal_places=2)
currency = models.CharField(max_length=3, default="ETB")
description = models.TextField()
image = models.ImageField(upload_to="products", blank=True, null=True)
tags = models.JSONField(default=list, blank=True, null=True)
# Server defaults
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
created_by = models.ForeignKey(
User, on_delete=models.CASCADE, related_name="products"
)
def __str__(self):
return self.name
class Meta:
ordering = ["-created_at"]
class ShippingAddress(models.Model):
user = models.ForeignKey(
User, on_delete=models.CASCADE, related_name="shipping_addresses"
)
country_code = models.CharField(max_length=3)
state = models.CharField(max_length=255, blank=True, null=True)
city = models.CharField(max_length=255, blank=True, null=True)
street_line1 = models.CharField(max_length=255, blank=True, null=True)
street_line2 = models.CharField(max_length=255, blank=True, null=True)
post_code = models.CharField(max_length=10, blank=True, null=True)
def __str__(self):
return f"{self.user} - {self.city} - {self.post_code}"
class Meta:
ordering = ["-id"]
class ShippingOption(models.Model):
name = models.CharField(max_length=255)
description = models.TextField()
price = models.DecimalField(max_digits=10, decimal_places=2)
currency = models.CharField(max_length=3, default="ETB")
delivery_time = models.CharField(max_length=255, blank=True, null=True)
def __str__(self):
return self.name
class Meta:
ordering = ["-id"]
class Order(models.Model):
class Status(models.TextChoices):
PENDING = "pending", _("Pending")
OUT_FOR_DELIVERY = "out_for_delivery", _("Out for delivery")
COMPLETED = "completed", _("Completed")
CANCELLED = "cancelled", _("Cancelled")
class PaymentMethod(models.TextChoices):
CHAPA = "chapa", _("Chapa")
STRIPE = "stripe", _("Stripe")
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="orders")
product = models.ForeignKey(
Product, on_delete=models.CASCADE, related_name="orders"
)
# user provided data
user_provided_name = models.CharField(max_length=255, blank=True, null=True)
user_provided_phone = models.CharField(max_length=15, blank=True, null=True)
user_provided_email = models.EmailField(blank=True, null=True)
shipping_address = models.ForeignKey(
ShippingAddress, on_delete=models.CASCADE, related_name="orders"
)
shipping_option = models.ForeignKey(
ShippingOption, on_delete=models.CASCADE, related_name="orders"
)
# server defaults and admin controled data
status = models.CharField(
max_length=20, choices=Status.choices, default=Status.PENDING
)
# transaction related data
telegram_payment_id = models.CharField(max_length=255, blank=True, null=True)
provider_payment_charge_id = models.CharField(max_length=255, blank=True, null=True)
payment_method = models.CharField(
max_length=50, choices=PaymentMethod.choices, default=PaymentMethod.CHAPA
)
total_amount = models.DecimalField(
max_digits=10, decimal_places=2, blank=True, null=True
)
currency = models.CharField(max_length=3, blank=True, null=True)
def __str__(self):
return f"{self.user} - {self.product} - {self.status}"
class Meta:
ordering = ["-id"]
def get_status_display(self):
return self.Status(self.status).label
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment