Skip to content

Commit

Permalink
campaign link generation and visit count
Browse files Browse the repository at this point in the history
  • Loading branch information
abdullai-t committed Dec 6, 2023
1 parent 9a4b9a6 commit 96fe8f9
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 3 deletions.
45 changes: 44 additions & 1 deletion src/api/handlers/campaign.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ def registerRoutes(self):
self.add("/campaigns.managers.list", self.list_campaign_managers)
self.add("/campaigns.communities.list", self.list_campaign_communities)
self.add("/campaigns.events.list", self.list_campaign_events)



self.add("/campaigns.links.generate", self.generate_campaign_links)
self.add("/campaigns.links.visits.count", self.campaign_link_visits_count)


# admin routes
Expand Down Expand Up @@ -512,4 +517,42 @@ def remove_campaign_event(self, request):
return MassenergizeResponse(data=res)



def generate_campaign_links(self, request):
context: Context = request.context
args: dict = context.args

(self.validator
.expect("campaign_id", str, is_required=True)
.expect("url", str, is_required=True)
.expect("utm_source", str, is_required=False)
.expect("utm_medium", str, is_required=False)
.expect("utm_campaign", str, is_required=False)
.expect("utm_content", str, is_required=False)
)
args, err = self.validator.verify(args)
if err:
return err

res, err = self.service.generate_campaign_links(context, args)
if err:
return err
return MassenergizeResponse(data=res)


def campaign_link_visits_count(self, request):
context: Context = request.context
args: dict = context.args

(self.validator
.expect("link_id", str, is_required=True)
)
args, err = self.validator.verify(args)
if err:
return err

res, err = self.service.campaign_link_visits_count(context, args)
if err:
return err
return MassenergizeResponse(data=res)


15 changes: 15 additions & 0 deletions src/api/services/campaign.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,3 +197,18 @@ def remove_campaign_event(self, context, args) -> Tuple[dict, MassEnergizeAPIErr
if err:
return None, err
return serialize(res, full=True), None


def generate_campaign_links(self, context, args) -> Tuple[dict, MassEnergizeAPIError]:
res, err = self.store.generate_campaign_links(context, args)
if err:
return None, err
return serialize(res, full=True), None


def campaign_link_visits_count(self, context, args) -> Tuple[dict, MassEnergizeAPIError]:
res, err = self.store.campaign_link_visits_count(context, args)
if err:
return None, err
return serialize(res, full=True), None

45 changes: 43 additions & 2 deletions src/api/store/campaign.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from _main_.utils.footage.FootageConstants import FootageConstants
from _main_.utils.footage.spy import Spy
from api.tests.common import RESET
from apps__campaigns.models import Campaign, CampaignAccount, CampaignCommunity, CampaignEvent, CampaignManager, CampaignPartner, CampaignTechnology, CampaignTechnologyTestimonial, Comment, Technology
from apps__campaigns.models import Campaign, CampaignAccount, CampaignCommunity, CampaignEvent, CampaignLike, CampaignLink, CampaignManager, CampaignPartner, CampaignTechnology, CampaignTechnologyTestimonial, Comment, Technology
from database.models import Community, Event, Testimonial, UserProfile, Media
from _main_.utils.massenergize_errors import MassEnergizeAPIError, InvalidResourceError, NotAuthorizedError, CustomMassenergizeError
from _main_.utils.context import Context
Expand Down Expand Up @@ -640,6 +640,47 @@ def remove_campaign_event(self, context: Context, args):
return None, CustomMassenergizeError(e)



def generate_campaign_link(self, context: Context, args):
try:
campaign_id = args.pop("campaign_id",None)
utm_source = args.pop("utm_source", None)
utm_medium = args.pop("utm_medium", None)
utm_campaign = args.pop("utm_campaign", None)
utm_content = args.pop("utm_content", None)
url = args.pop("url", None)
email = args.pop("email", None)

if not campaign_id:
return None, InvalidResourceError()

campaign = Campaign.objects.filter(id=campaign_id).first()
if not campaign:
return None, CustomMassenergizeError("Campaign with id not found!")

campaign_link = CampaignLink.objects.create(campaign=campaign, email=email, url=url, utm_source=utm_source, utm_medium=utm_medium, utm_campaign=utm_campaign)

generated_link = f"{url}?utm_source={utm_source}&utm_medium={utm_medium}&utm_campaign={utm_campaign}&utm_content={utm_content}&campaign_like_id={campaign_link.id}"

return {"link":generated_link} , None
except Exception as e:
capture_message(str(e), level="error")
return None, CustomMassenergizeError(e)




def campaign_link_visits_count(self, context,args):
try:
campaign_link_id = args.pop("campaign_link_id",None)
if not campaign_link_id:
return None, InvalidResourceError()

campaign_link = CampaignLink.objects.filter(id=campaign_link_id).first()
if campaign_link:
campaign_link.increase_count()

return campaign_link, None
except Exception as e:
capture_message(str(e), level="error")
return None, CustomMassenergizeError(e)

33 changes: 33 additions & 0 deletions src/apps__campaigns/migrations/0006_auto_20231206_1155.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Generated by Django 3.1.14 on 2023-12-06 11:55

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('apps__campaigns', '0005_auto_20231206_0926'),
]

operations = [
migrations.AddField(
model_name='campaignlink',
name='url',
field=models.CharField(blank=True, max_length=10000, null=True),
),
migrations.AddField(
model_name='campaignlink',
name='utm_campaign',
field=models.CharField(blank=True, max_length=100, null=True),
),
migrations.AddField(
model_name='campaignlink',
name='utm_medium',
field=models.CharField(blank=True, max_length=100, null=True),
),
migrations.AddField(
model_name='campaignlink',
name='utm_source',
field=models.CharField(blank=True, max_length=100, null=True),
),
]
4 changes: 4 additions & 0 deletions src/apps__campaigns/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,11 @@ def full_json(self):
class CampaignLink(BaseModel):
campaign = models.ForeignKey(Campaign, on_delete=models.CASCADE)
visits = models.PositiveBigIntegerField(default=0)
url = models.CharField(max_length=LONG_STR_LEN, blank=True, null=True)
email = models.EmailField(blank=True, null=True)
utm_source = models.CharField(blank=True, null=True, max_length=SHORT_STR_LEN)
utm_medium = models.CharField(blank=True, null=True, max_length=SHORT_STR_LEN)
utm_campaign = models.CharField(blank=True, null=True, max_length=SHORT_STR_LEN)

def __str__(self):
return f"{self.email} - {self.visits}"
Expand Down

0 comments on commit 96fe8f9

Please sign in to comment.