Skip to content

Commit

Permalink
follow, like and view routes
Browse files Browse the repository at this point in the history
  • Loading branch information
abdullai-t committed Dec 6, 2023
1 parent 96fe8f9 commit a6f8db2
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 2 deletions.
67 changes: 67 additions & 0 deletions src/api/handlers/campaign.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ def registerRoutes(self):

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

self.add("/campaigns.follow", self.add_campaign_follower)
self.add("/campaigns.technology.like", self.add_campaign_technology_like)
# self.add("/campaigns.technology.follow", self.add_campaign_technology_follower)
self.add("/campaigns.technology.view", self.add_campaign_technology_view)


# admin routes
Expand Down Expand Up @@ -556,3 +561,65 @@ def campaign_link_visits_count(self, request):
return MassenergizeResponse(data=res)


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

(self.validator
.expect("campaign_id", str, is_required=True)
.expect("email", str, is_required=True)
.expect("zipcode", str, is_required=True)
.expect("community_id", int, is_required=False)
)
args, err = self.validator.verify(args)
if err:
return err

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



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

(self.validator
.expect("campaign_technology_id", str, is_required=True)
.expect("user_id", int, is_required=False)
.expect("zipcode", str, is_required=False)
.expect("email", str, is_required=False)
.expect("community_id", int, is_required=False)
)
args, err = self.validator.verify(args)
if err:
return err

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


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

(self.validator
.expect("campaign_technology_id", str, is_required=True)
.expect("ip_address", str, is_required=False)
.expect("user_agent", int, is_required=False)
)
args, err = self.validator.verify(args)
if err:
return err

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



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

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


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


def add_campaign_technology_like(self, context, args)-> Tuple[dict, MassEnergizeAPIError]:
res, err = self.store.add_campaign_technology_like(context, args)
if err:
return None, err
return serialize(res, full=True), None
72 changes: 71 additions & 1 deletion 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, CampaignLike, CampaignLink, CampaignManager, CampaignPartner, CampaignTechnology, CampaignTechnologyTestimonial, Comment, Technology
from apps__campaigns.models import Campaign, CampaignAccount, CampaignCommunity, CampaignEvent, CampaignFollow, CampaignLike, CampaignLink, CampaignManager, CampaignPartner, CampaignTechnology, CampaignTechnologyLike, CampaignTechnologyTestimonial, CampaignTechnologyView, 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 @@ -684,3 +684,73 @@ def campaign_link_visits_count(self, context,args):
capture_message(str(e), level="error")
return None, CustomMassenergizeError(e)


def add_campaign_follower(self,context, args):
try:
campaign_id = args.pop("campaign_id",None)
community_id = args.pop("community_id", 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!")

if community_id:
community = Community.objects.filter(id=community_id).first()
args["community"] = community

follower = CampaignFollow.objects.create(campaign=campaign, **args)

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


def add_campaign_technology_like(self,context, args):
try:
campaign_technology_id = args.pop("campaign_technology_id",None)
email = args.pop("email", None)
user_id = args.pop("user_id", None)
community_id = args.pop("community_id", None)

if not campaign_technology_id:
return None, InvalidResourceError()

campaign_technology = CampaignTechnology.objects.filter(id=campaign_technology_id).first()
if not campaign_technology:
return None, CustomMassenergizeError("Campaign technology with id not found!")

like = CampaignTechnologyLike.objects.create(campaign_technology=campaign_technology, **args)

if user_id:
user = UserProfile.objects.filter(id=user_id).first()
like.user = user
elif email:
user= UserProfile.objects.create(email=email)
like.user = user

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



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

campaign_technology = CampaignTechnology.objects.filter(id=campaign_technology_id).first()
if not campaign_technology:
return None, CustomMassenergizeError("Campaign technology with id not found!")

view = CampaignTechnologyView.objects.create(campaign_technology=campaign_technology, **args)
return view, None
except Exception as e:
capture_message(str(e), level="error")
return None, CustomMassenergizeError(e)
2 changes: 1 addition & 1 deletion src/apps__campaigns/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ def full_json(self):

class CampaignTechnologyLike(BaseModel):
campaign_technology = models.ForeignKey(CampaignTechnology, on_delete=models.CASCADE)
user = models.ForeignKey(UserProfile, on_delete=models.CASCADE) #user with email and zipcode
user = models.ForeignKey(UserProfile, on_delete=models.CASCADE)
zipcode = models.CharField(blank=True, null=True, max_length=SHORT_STR_LEN)

def __str__(self):
Expand Down

0 comments on commit a6f8db2

Please sign in to comment.