You've already forked Promocode-API
mirror of
https://github.com/devitq/Promocode-API.git
synced 2026-05-22 22:07:12 +00:00
feat: added patch promocode, promocode stat, user signup/signin, user profile get/patch, user feed
also bug fixes and improvements
This commit is contained in:
@@ -34,6 +34,7 @@ class BaseModel(models.Model):
|
||||
if include
|
||||
else None,
|
||||
)
|
||||
|
||||
if validate_unique:
|
||||
try:
|
||||
self.validate_unique()
|
||||
|
||||
@@ -1,6 +1,15 @@
|
||||
from django.contrib import admin
|
||||
|
||||
from apps.promo.models import Promocode, PromocodeTarget
|
||||
from apps.promo.models import (
|
||||
Promocode,
|
||||
PromocodeActivation,
|
||||
PromocodeComment,
|
||||
PromocodeLike,
|
||||
PromocodeTarget,
|
||||
)
|
||||
|
||||
admin.site.register(Promocode)
|
||||
admin.site.register(PromocodeTarget)
|
||||
admin.site.register(PromocodeActivation)
|
||||
admin.site.register(PromocodeComment)
|
||||
admin.site.register(PromocodeLike)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
# Generated by Django 5.1.5 on 2025-01-21 14:30
|
||||
# Generated by Django 5.1.5 on 2025-01-23 17:54
|
||||
|
||||
import apps.promo.validators
|
||||
import django.core.validators
|
||||
@@ -25,6 +25,7 @@ class Migration(migrations.Migration):
|
||||
('age_from', models.PositiveSmallIntegerField(blank=True, null=True, validators=[django.core.validators.MinValueValidator(0), django.core.validators.MaxValueValidator(100)])),
|
||||
('age_until', models.PositiveSmallIntegerField(blank=True, null=True, validators=[django.core.validators.MinValueValidator(0), django.core.validators.MaxValueValidator(100)])),
|
||||
('country', django_countries.fields.CountryField(blank=True, max_length=2, null=True)),
|
||||
('country_raw', models.CharField(blank=True, max_length=2, null=True)),
|
||||
('categories', models.JSONField(blank=True, default=list, null=True, validators=[apps.promo.validators.TargetCategoriesValidator()])),
|
||||
],
|
||||
options={
|
||||
@@ -43,7 +44,7 @@ class Migration(migrations.Migration):
|
||||
('mode', models.CharField(choices=[('COMMON', 'Common'), ('UNIQUE', 'Unique')], max_length=6)),
|
||||
('promo_common', models.CharField(blank=True, max_length=30, null=True, validators=[django.core.validators.MinLengthValidator(5)])),
|
||||
('promo_unique', models.JSONField(blank=True, default=list, null=True, validators=[apps.promo.validators.PromocodeUniqueValidator()])),
|
||||
('promo_unique_activated', models.JSONField(blank=True, default=list, editable=False, null=True)),
|
||||
('promo_unique_activated', models.JSONField(blank=True, default=list, null=True)),
|
||||
('created_at', models.DateTimeField(auto_now_add=True)),
|
||||
('business', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='promocodes', to='business.business')),
|
||||
('target', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='promocodes', to='promo.promocodetarget')),
|
||||
@@ -77,4 +78,15 @@ class Migration(migrations.Migration):
|
||||
'abstract': False,
|
||||
},
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='PromocodeLike',
|
||||
fields=[
|
||||
('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
|
||||
('promocode', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='likes', to='promo.promocode')),
|
||||
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='liked_promocodes', to='user.user')),
|
||||
],
|
||||
options={
|
||||
'abstract': False,
|
||||
},
|
||||
),
|
||||
]
|
||||
|
||||
@@ -31,6 +31,7 @@ class PromocodeTarget(BaseModel):
|
||||
validators=[MinValueValidator(0), MaxValueValidator(100)],
|
||||
)
|
||||
country = CountryField(blank=True, null=True)
|
||||
country_raw = models.CharField(max_length=2, blank=True, null=True)
|
||||
categories = models.JSONField(
|
||||
blank=True,
|
||||
null=True,
|
||||
@@ -61,7 +62,11 @@ class Promocode(BaseModel):
|
||||
max_length=300,
|
||||
validators=[MinLengthValidator(10)],
|
||||
)
|
||||
image_url = models.URLField(max_length=350, blank=True, null=True)
|
||||
image_url = models.URLField(
|
||||
max_length=350,
|
||||
blank=True,
|
||||
null=True,
|
||||
)
|
||||
target = models.ForeignKey(
|
||||
PromocodeTarget,
|
||||
on_delete=models.CASCADE,
|
||||
@@ -89,13 +94,21 @@ class Promocode(BaseModel):
|
||||
blank=True,
|
||||
null=True,
|
||||
default=list,
|
||||
editable=False,
|
||||
)
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
|
||||
def __str__(self) -> str:
|
||||
return str(self.id)
|
||||
|
||||
def clean(self) -> None:
|
||||
super().clean()
|
||||
|
||||
if self.image_url == "":
|
||||
err = {
|
||||
"image_url": "Field cannot be blank.",
|
||||
}
|
||||
raise ValidationError(err)
|
||||
|
||||
if self.mode == self.ModeChoices.COMMON:
|
||||
if not self.promo_common:
|
||||
err = {
|
||||
@@ -107,6 +120,11 @@ class Promocode(BaseModel):
|
||||
"promo_unique": "Field must be empty for COMMON mode.",
|
||||
}
|
||||
raise ValidationError(err)
|
||||
if self.max_count < self.activations.count():
|
||||
err = {
|
||||
"max_count": "Activations count is bigger than max_count",
|
||||
}
|
||||
raise ValidationError(err)
|
||||
elif self.mode == self.ModeChoices.UNIQUE:
|
||||
if not self.promo_unique:
|
||||
err = {
|
||||
@@ -118,6 +136,11 @@ class Promocode(BaseModel):
|
||||
"promo_common": "Field must be empty for UNIQUE mode.",
|
||||
}
|
||||
raise ValidationError(err)
|
||||
if self.max_count != 1:
|
||||
err = {
|
||||
"max_count": "Field must be 1 for UNIQUE mode.",
|
||||
}
|
||||
raise ValidationError(err)
|
||||
|
||||
PromocodeDurationValidator()(self)
|
||||
|
||||
@@ -134,6 +157,8 @@ class Promocode(BaseModel):
|
||||
elif self.mode == self.ModeChoices.UNIQUE:
|
||||
is_active_by_mode = len(self.promo_unique) > len(
|
||||
self.promo_unique_activated
|
||||
if self.promo_unique_activated
|
||||
else []
|
||||
)
|
||||
|
||||
return is_active_by_date and is_active_by_mode
|
||||
@@ -152,6 +177,9 @@ class PromocodeActivation(BaseModel):
|
||||
)
|
||||
timestamp = models.DateTimeField(auto_now_add=True)
|
||||
|
||||
def __str__(self) -> str:
|
||||
return f"{self.promocode.id} | {self.user.id}"
|
||||
|
||||
|
||||
class PromocodeComment(BaseModel):
|
||||
promocode = models.ForeignKey(
|
||||
@@ -169,3 +197,15 @@ class PromocodeComment(BaseModel):
|
||||
validators=[MinLengthValidator(10)],
|
||||
)
|
||||
date = models.DateTimeField(auto_now_add=True)
|
||||
|
||||
def __str__(self) -> str:
|
||||
return f"{self.promocode.id} | {self.author.id}"
|
||||
|
||||
|
||||
class PromocodeLike(BaseModel):
|
||||
promocode = models.ForeignKey(
|
||||
Promocode, on_delete=models.CASCADE, related_name="likes"
|
||||
)
|
||||
user = models.ForeignKey(
|
||||
User, on_delete=models.CASCADE, related_name="liked_promocodes"
|
||||
)
|
||||
|
||||
@@ -61,7 +61,7 @@ class PromocodeUniqueValidator(BaseValidator):
|
||||
|
||||
def __call__(self, promocodes: list) -> None:
|
||||
if not isinstance(promocodes, list):
|
||||
err = "unque promocodes must be a list"
|
||||
err = "unique promocodes must be a list"
|
||||
raise ValidationError(err)
|
||||
|
||||
if not (
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
# Generated by Django 5.1.5 on 2025-01-21 11:05
|
||||
# Generated by Django 5.1.5 on 2025-01-23 17:54
|
||||
|
||||
import django.core.validators
|
||||
import django_countries.fields
|
||||
@@ -24,6 +24,7 @@ class Migration(migrations.Migration):
|
||||
('avatar_url', models.URLField(blank=True, max_length=350, null=True)),
|
||||
('age', models.PositiveSmallIntegerField(validators=[django.core.validators.MaxValueValidator(100)])),
|
||||
('country', django_countries.fields.CountryField(max_length=2)),
|
||||
('country_raw', models.CharField(max_length=2)),
|
||||
('password', models.CharField(max_length=60, validators=[django.core.validators.RegexValidator('^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[@$!%*?&])[A-Za-z\\d@$!%*?&]{8,}$')])),
|
||||
('token_version', models.BigIntegerField(default=0)),
|
||||
],
|
||||
|
||||
@@ -2,6 +2,7 @@ from datetime import timedelta
|
||||
|
||||
import jwt
|
||||
from django.conf import settings
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.core.validators import (
|
||||
MaxValueValidator,
|
||||
MinLengthValidator,
|
||||
@@ -28,6 +29,7 @@ class User(BaseModel):
|
||||
avatar_url = models.URLField(max_length=350, blank=True, null=True)
|
||||
age = models.PositiveSmallIntegerField(validators=[MaxValueValidator(100)])
|
||||
country = CountryField(max_length=2)
|
||||
country_raw = models.CharField(max_length=2)
|
||||
password = models.CharField(
|
||||
max_length=60,
|
||||
validators=[
|
||||
@@ -41,6 +43,15 @@ class User(BaseModel):
|
||||
def __str__(self) -> str:
|
||||
return f"{self.surname} {self.name}"
|
||||
|
||||
def clean(self) -> None:
|
||||
super().clean()
|
||||
|
||||
if self.avatar_url == "":
|
||||
err = {
|
||||
"avatar_url": "Field cannot be blank.",
|
||||
}
|
||||
raise ValidationError(err)
|
||||
|
||||
def generate_token(self) -> str:
|
||||
return jwt.encode(
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user