feat(social media): added api for post to social media
This commit is contained in:
@@ -24,7 +24,7 @@ urlpatterns = [
|
||||
path('principal/add/', views.PrincipalCreateOrUpdateView.as_view(), name="principal_add"),
|
||||
path('principal/edit/<int:pk>', views.PrincipalCreateOrUpdateView.as_view(), name="principal_edit"),
|
||||
# path('principal/delete/<int:pk>', views.PrincipalDeleteView.as_view(), name="principal_delete"),
|
||||
path('principal/resource/permission/edit/<int:pk>/', views.PrincipalResourcePermissionEditView.as_view(),
|
||||
path('principal/resource/permission/edit/<int:pk>/', views.PrincipalResourcePermissionEditView.as_view(),
|
||||
name="principal_resource_permission_edit"),
|
||||
|
||||
path('principal/group/link/', views.PrincipalGroupLinkListView.as_view(), name="principal_group_link_list"),
|
||||
|
||||
@@ -1127,7 +1127,7 @@ class CustomerImportView(LoginRequiredMixin, generic.View):
|
||||
register_complete=True,
|
||||
principal_type=principal_type,
|
||||
business_name=business_name,
|
||||
phone_no=str(phone_no),
|
||||
phone_no=phone_no,
|
||||
address_line1=address,
|
||||
city=region,
|
||||
country=country,
|
||||
|
||||
@@ -141,4 +141,6 @@ urlpatterns = [
|
||||
views.EventListView.as_view(),
|
||||
name="event_filter",
|
||||
),
|
||||
|
||||
path("post-to-social-media/<int:id>/", views.SocialMediaPostView.as_view(), name="social_media_post")
|
||||
]
|
||||
|
||||
@@ -13,7 +13,7 @@ from django.db.models import Q, Count
|
||||
from taggit.models import Tag
|
||||
from django.utils.dateparse import parse_date
|
||||
from goodtimes import services
|
||||
from goodtimes.services import GoogleMapsservice
|
||||
from goodtimes.services import FacebookAPI, FacebookPoster, GoogleMapsservice, InstagramAPI, InstagramPoster, TwitterAPI, TwitterPoster
|
||||
from goodtimes.utils import ApiResponse, CapacityError
|
||||
from rest_framework.permissions import IsAuthenticated
|
||||
from rest_framework_simplejwt.authentication import JWTAuthentication
|
||||
@@ -1124,4 +1124,80 @@ class EventListView(generics.ListAPIView):
|
||||
return self.get_paginated_response(serializer.data)
|
||||
|
||||
serializer = self.get_serializer(queryset, many=True)
|
||||
return ApiResponse.success(message=constants.SUCCESS, data=serializer.data)
|
||||
return ApiResponse.success(message=constants.SUCCESS, data=serializer.data)
|
||||
|
||||
|
||||
from rest_framework.response import Response
|
||||
class SocialMediaPostView(APIView):
|
||||
def get(self, request, *args, **kwargs):
|
||||
platform = request.query_params.get("platform", "")
|
||||
event_id = kwargs.get("id")
|
||||
print(platform, event_id)
|
||||
errors = []
|
||||
success_messages = []
|
||||
|
||||
try:
|
||||
event = Event.objects.get(id=event_id)
|
||||
except Event.DoesNotExist:
|
||||
errors.append("Event does not exist")
|
||||
return Response({
|
||||
'message': "Error in posting to social media",
|
||||
'errors': errors,
|
||||
'success_messages': success_messages
|
||||
}, status=400)
|
||||
|
||||
if not event.active:
|
||||
errors.append("Event is not active")
|
||||
return Response({
|
||||
'message': "Error in posting to social media",
|
||||
'errors': errors,
|
||||
'success_messages': success_messages
|
||||
}, status=400)
|
||||
|
||||
caption = f"{event.title}\nDuration: {event.start_date} to {event.end_date}\nAddress: {event.venue.address}"
|
||||
|
||||
if platform in ['instagram', 'facebook', 'twitter', 'all']:
|
||||
if platform in ['twitter', 'all']:
|
||||
image_url = event.image.path
|
||||
twitter_api = TwitterAPI()
|
||||
twitter_poster = TwitterPoster(twitter_api)
|
||||
result = twitter_poster.post_image_with_caption(image_url, caption)
|
||||
if result['success']:
|
||||
success_messages.append("Posted to Twitter successfully")
|
||||
else:
|
||||
errors.append("Fail to post on Twitter")
|
||||
|
||||
image_url = request.build_absolute_uri(event.image.url)
|
||||
if platform in ['facebook', 'all']:
|
||||
facebook_api = FacebookAPI()
|
||||
facebook_poster = FacebookPoster(facebook_api)
|
||||
result = facebook_poster.post_photo(image_url, caption)
|
||||
if result["success"]:
|
||||
success_messages.append("Posted to Facebook successfully")
|
||||
else:
|
||||
errors.append("Fail to post on Facebook")
|
||||
|
||||
if platform in ['instagram', 'all']:
|
||||
instagram_api = InstagramAPI()
|
||||
instagram_poster = InstagramPoster(instagram_api)
|
||||
result = instagram_poster.post_image_with_caption(image_url, caption)
|
||||
if result["success"]:
|
||||
success_messages.append("Posted to Instagram successfully")
|
||||
else:
|
||||
errors.append("Fail to post on Instagram")
|
||||
|
||||
if not errors:
|
||||
return Response({'message': 'Post Successful', 'errors': errors, 'success_messages': success_messages})
|
||||
|
||||
if errors and success_messages:
|
||||
return Response({
|
||||
'message': 'Some posts succeeded while others failed',
|
||||
'errors': errors,
|
||||
'success_messages': success_messages
|
||||
}, status=200)
|
||||
|
||||
return Response({
|
||||
'message': 'Error in posting to social media',
|
||||
'errors': errors,
|
||||
'success_messages': success_messages
|
||||
}, status=400)
|
||||
@@ -60,7 +60,7 @@
|
||||
</div>
|
||||
{% endif %}
|
||||
{% if field.errors %}
|
||||
<div id="{{ field.id_for_label }}_Feedback" class="invalid-feedback">
|
||||
<div id="{{ field.id_for_label }}-error" class="error invalid-feedback">
|
||||
{% for error in field.errors %}
|
||||
<p>{{ error }}</p>
|
||||
{% endfor %}
|
||||
|
||||
Reference in New Issue
Block a user