refactor: global codebase refactoring
This commit is contained in:
@@ -22,6 +22,7 @@ class AdvertiserModelTest(TestCase):
|
||||
|
||||
new_id = uuid4()
|
||||
self.advertiser.advertiser_id = new_id
|
||||
|
||||
self.assertEqual(self.advertiser.id, new_id)
|
||||
|
||||
@override_settings(
|
||||
|
||||
@@ -6,7 +6,6 @@ from uuid import UUID
|
||||
|
||||
from django.conf import settings
|
||||
from django.core.cache import cache
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.core.validators import (
|
||||
MaxValueValidator,
|
||||
MinLengthValidator,
|
||||
@@ -20,6 +19,7 @@ from apps.campaign.validators import (
|
||||
CampaignDurationValidator,
|
||||
CampaignLimitsValidator,
|
||||
CampaignReportMessageValidator,
|
||||
CampaignStartDateValidator,
|
||||
CampaignTargetingLocationValidator,
|
||||
)
|
||||
from apps.client.models import Client
|
||||
@@ -100,21 +100,7 @@ class Campaign(BaseModel):
|
||||
CampaignAgeValidator()(self)
|
||||
CampaignDurationValidator()(self)
|
||||
CampaignLimitsValidator()(self)
|
||||
|
||||
current_date = cache.get("current_date", default=0)
|
||||
|
||||
err = "start_date must be greater than the current date."
|
||||
|
||||
try:
|
||||
original = Campaign.objects.get(id=self.id or "")
|
||||
if (
|
||||
original.start_date != self.start_date
|
||||
and self.start_date < current_date
|
||||
):
|
||||
raise ValidationError(err)
|
||||
except Campaign.DoesNotExist:
|
||||
if self.start_date < current_date:
|
||||
raise ValidationError(err) from None
|
||||
CampaignStartDateValidator()(self)
|
||||
|
||||
def save(self, *args: Any, **kwargs: Any) -> None:
|
||||
created = self.pk is None
|
||||
@@ -134,6 +120,20 @@ class Campaign(BaseModel):
|
||||
)
|
||||
cache.set(f"campaign_{self.id}_clicks_count", self.clicks.count())
|
||||
|
||||
def inc_views(self) -> None:
|
||||
try:
|
||||
cache.incr(f"campaign_{self.id}_impressions_count", 1)
|
||||
except ValueError:
|
||||
self.setup_cache()
|
||||
logger.warning("Seems that %s missing caches", self.campaign_id)
|
||||
|
||||
def inc_clicks(self) -> None:
|
||||
try:
|
||||
cache.incr(f"campaign_{self.id}_clicks_count", 1)
|
||||
except ValueError:
|
||||
self.setup_cache()
|
||||
logger.warning("Seems that %s missing caches", self.campaign_id)
|
||||
|
||||
@property
|
||||
def ad_id(self) -> UUID:
|
||||
return self.id
|
||||
@@ -175,13 +175,7 @@ class Campaign(BaseModel):
|
||||
price=self.cost_per_impression,
|
||||
date=cache.get("current_date", default=0),
|
||||
)
|
||||
try:
|
||||
cache.incr(f"campaign_{self.id}_impressions_count", 1)
|
||||
except ValueError:
|
||||
self.setup_cache()
|
||||
logger.warning(
|
||||
"Seems that %s missing caches", self.campaign_id
|
||||
)
|
||||
self.inc_views()
|
||||
except ConflictError:
|
||||
pass
|
||||
|
||||
@@ -198,13 +192,7 @@ class Campaign(BaseModel):
|
||||
price=self.cost_per_click,
|
||||
date=cache.get("current_date", default=0),
|
||||
)
|
||||
try:
|
||||
cache.incr(f"campaign_{self.id}_clicks_count", 1)
|
||||
except ValueError:
|
||||
self.setup_cache()
|
||||
logger.warning(
|
||||
"Seems that %s missing caches", self.campaign_id
|
||||
)
|
||||
self.inc_clicks()
|
||||
except ConflictError:
|
||||
pass
|
||||
|
||||
@@ -235,7 +223,6 @@ class Campaign(BaseModel):
|
||||
total=models.Count("id"),
|
||||
spent=models.Sum("price", default=0.0),
|
||||
)
|
||||
|
||||
clicks = self.clicks.values("date").annotate(
|
||||
total=models.Count("id"),
|
||||
spent=models.Sum("price", default=0.0),
|
||||
|
||||
@@ -1,48 +1,83 @@
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from django.core.cache import cache
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.db.models.fields import Field
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from apps.campaign.models import Campaign, CampaignReport
|
||||
|
||||
|
||||
class CampaignTargetingLocationValidator:
|
||||
def __call__(self, instance: "Campaign") -> None:
|
||||
if instance.location == "":
|
||||
err = {
|
||||
"targeting": {
|
||||
type(
|
||||
instance
|
||||
).location.field.name: Field.default_error_messages[
|
||||
"blank"
|
||||
]
|
||||
}
|
||||
}
|
||||
raise ValidationError(err)
|
||||
|
||||
|
||||
class CampaignAgeValidator:
|
||||
def __call__(self, instance) -> None: # noqa: ANN001
|
||||
def __call__(self, instance: "Campaign") -> None:
|
||||
if (
|
||||
isinstance(instance.age_from, int)
|
||||
and isinstance(instance.age_to, int)
|
||||
and instance.age_from > instance.age_to
|
||||
):
|
||||
err = "age_from can't be greater than age_to"
|
||||
err = "targeting.age_from can't be greater than targeting.age_to."
|
||||
raise ValidationError(err)
|
||||
|
||||
|
||||
class CampaignDurationValidator:
|
||||
def __call__(self, instance) -> None: # noqa: ANN001
|
||||
def __call__(self, instance: "Campaign") -> None:
|
||||
if (
|
||||
isinstance(instance.start_date, int)
|
||||
and isinstance(instance.end_date, int)
|
||||
and instance.start_date > instance.end_date
|
||||
):
|
||||
err = "start_date can't be greater than end_date"
|
||||
err = "start_date can't be greater than end_date."
|
||||
raise ValidationError(err)
|
||||
|
||||
|
||||
class CampaignLimitsValidator:
|
||||
def __call__(self, instance) -> None: # noqa: ANN001
|
||||
def __call__(self, instance: "Campaign") -> None:
|
||||
if (
|
||||
isinstance(instance.impressions_limit, int)
|
||||
and isinstance(instance.clicks_limit, int)
|
||||
and instance.impressions_limit < instance.clicks_limit
|
||||
):
|
||||
err = "clicks_limit can't be greater than impressions_limit"
|
||||
err = "clicks_limit can't be greater than impressions_limit."
|
||||
raise ValidationError(err)
|
||||
|
||||
|
||||
class CampaignTargetingLocationValidator:
|
||||
def __call__(self, instance) -> None: # noqa: ANN001
|
||||
if instance.location == "":
|
||||
err = "targeting.location cannot be blank"
|
||||
raise ValidationError(err)
|
||||
class CampaignStartDateValidator:
|
||||
def __call__(self, instance: "Campaign") -> None:
|
||||
current_date = cache.get("current_date", default=0)
|
||||
err = "start_date must be greater or equal than the current_date."
|
||||
try:
|
||||
original = type(instance).objects.get(id=instance.id or "")
|
||||
if (
|
||||
original.start_date != instance.start_date
|
||||
and instance.start_date < current_date
|
||||
):
|
||||
raise ValidationError(err)
|
||||
except type(instance).DoesNotExist:
|
||||
if instance.start_date < current_date:
|
||||
raise ValidationError(err) from None
|
||||
|
||||
|
||||
class CampaignReportMessageValidator:
|
||||
def __call__(self, instance) -> None: # noqa: ANN001
|
||||
def __call__(self, instance: "CampaignReport") -> None:
|
||||
if instance.message == "":
|
||||
err = "message cannot be blank"
|
||||
err = {
|
||||
instance.message.field.name: Field.default_error_messages[
|
||||
"blank"
|
||||
]
|
||||
}
|
||||
raise ValidationError(err)
|
||||
|
||||
Reference in New Issue
Block a user