-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] Meetup rsvp integration #12
Open
ghost
wants to merge
2
commits into
pydelhi:develop
Choose a base branch
from
unknown repository
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
"""Contains celery tasks to migrate conference attendees from different | ||
ticketing platforms.""" | ||
|
||
from __future__ import absolute_import, unicode_literals | ||
|
||
from django.db.models import Max | ||
from celery import shared_task | ||
|
||
from .models import User | ||
from .utils import call_explara_and_fetch_data, process_explara_data_and_populate_db | ||
from .utils import call_meetup_and_fetch_data, process_meetup_data_and_populate_db | ||
|
||
@shared_task(name='registration.tasks.sync_database_with_explara') | ||
def sync_database_with_explara(EXPLARA_EVENT_ID): | ||
"""Syncs all new conference attendees from explara with the | ||
application's database. | ||
|
||
Args: | ||
- EXPLARA_EVENT_ID: str. Event ID for the explara event. | ||
|
||
Returns: | ||
- None | ||
""" | ||
|
||
# For having multiple paginated calls to Explara till all the data is | ||
# synced with the database | ||
while True: | ||
max_ticket_id = User.objects.all().aggregate(Max('ticket_id'))["ticket_id__max"] | ||
|
||
if not max_ticket_id: | ||
max_ticket_id = 0 | ||
|
||
data = call_explara_and_fetch_data(EXPLARA_EVENT_ID, max_ticket_id) | ||
|
||
if data["status"] != 'success': | ||
print("Error from explara: ") | ||
print(data) | ||
break | ||
|
||
if not data["attendee"]: | ||
print("No attendees left now") | ||
break | ||
|
||
attendee_order_list = data['attendee'] | ||
|
||
process_explara_data_and_populate_db(attendee_order_list) | ||
|
||
|
||
@shared_task(name='registration.tasks.rsvp_from_meetup') | ||
def get_rsvp_from_meetup(MEETUP_EVENT_ID): | ||
""" | ||
Gets the rsvp list from meetup.com for a particular event. | ||
|
||
Reference meetup doc : | ||
- https://www.meetup.com/meetup_api/docs/2/rsvps/ | ||
|
||
Args: | ||
- MEETUP_EVENT_ID : str. Event id of the group's event. | ||
|
||
Returns: | ||
- None | ||
""" | ||
|
||
# The event id is hardcoded for now. | ||
# The api doc says events are paginated | ||
|
||
data = call_meetup_and_fetch_data(MEETUP_EVENT_ID) | ||
|
||
if data["status"] != "success": | ||
print("Could not fetch data from meetup") | ||
print(data) | ||
|
||
meetup_rsvp_list = data["results"] | ||
|
||
process_meetup_data_and_populate_db(meetup_rsvp_list) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
import requests | ||
|
||
from confluence.settings import EXPLARA_API_KEY, EXPLARA_ATTENDEE_LIST_URL | ||
from confluence.settings import MEETUP_API_KEY, MEETUP_RSVP_URL | ||
from .models import User | ||
|
||
|
||
def call_explara_and_fetch_data(EXPLARA_EVENT_ID, max_ticket_id): | ||
"""Syncs all new conference attendees from Explara with the | ||
application's database. | ||
|
||
Args: | ||
- EXPLARA_EVENT_ID: str. Event ID for the Explara event. | ||
- max_ticket_id: int. ticket_id till which Explara data is already | ||
synced with the db. | ||
|
||
Returns: | ||
- Attendees data: dict. Response in JSON format as fetched from Explara. | ||
""" | ||
headers = { | ||
'Content-Type': 'application/x-www-form-urlencoded', | ||
'Authorization': "Bearer %s" % EXPLARA_API_KEY | ||
} | ||
|
||
payload = { | ||
'eventId': EXPLARA_EVENT_ID, | ||
'fromRecord': int(max_ticket_id), | ||
'toRecord': int(max_ticket_id) + 50 | ||
} | ||
|
||
response = requests.post( | ||
EXPLARA_ATTENDEE_LIST_URL, | ||
data=payload, | ||
headers=headers | ||
) | ||
|
||
return response.json() | ||
|
||
|
||
def process_explara_data_and_populate_db(attendee_order_list): | ||
"""Syncs all new conference attendees from explara with the | ||
application's database. | ||
|
||
Args: | ||
- attendee_order_list: list. Attendees list as fetched from Explara's API. | ||
|
||
Returns: | ||
- None. | ||
""" | ||
for order in attendee_order_list: | ||
tickets = order['attendee'] | ||
for ticket in tickets: | ||
print(ticket) | ||
try: | ||
name, email = ticket['name'], ticket['email'] | ||
ticket_no = ticket['ticketNo'] | ||
name_list = name.split(' ') | ||
first_name, last_name = name_list[0], name_list[-1] | ||
username = 'explara' + str(ticket_no) | ||
tshirt_size = ticket['details']['T-shirt size'] | ||
contact_no = ticket['details']['Contact Number'] | ||
if len(contact_no) > 10: | ||
contact_no = contact_no[1:] | ||
except KeyError as e: | ||
print("Error in decoding data") | ||
print(e) | ||
continue | ||
|
||
# username is intentionally kept as ticket_no so there | ||
# aren't any chances of DB integrity error of failing UNIQUE | ||
# constraint on username | ||
try: | ||
User.objects.create( | ||
username=username, | ||
first_name=first_name, | ||
last_name=last_name, | ||
email=email, | ||
ticket_id=ticket_no, | ||
tshirt_size=tshirt_size, | ||
contact=contact_no | ||
) | ||
except Exception as e: | ||
print("Cannot create User because: " + str(e)) | ||
print("Ticket details for failed user creation entry: ") | ||
print(ticket) | ||
continue | ||
|
||
|
||
def call_meetup_and_fetch_data(MEETUP_EVENT_ID): | ||
""" | ||
GETs all the meetup rsvp's for a particular event. | ||
|
||
Args: | ||
- MEETUP_EVENT_ID: str. Event ID for the Explara event. | ||
|
||
Returns: | ||
- meetup_rsvp_list data: dict. Response in JSON format as fetched from Meetup. | ||
""" | ||
headers = { | ||
'Content-Type': 'application/x-www-form-urlencoded', | ||
'Authorization': "Bearer %s" % MEETUP_API_KEY | ||
} | ||
|
||
payload = { | ||
'event_id': MEETUP_EVENT_ID, | ||
'rsvp' : 'yes' | ||
} | ||
|
||
response = requests.post( | ||
MEETUP_RSVP_URL, | ||
data=payload, | ||
headers=headers | ||
) | ||
|
||
return response.json() | ||
|
||
|
||
def process_meetup_data_and_populate_db(meetup_rsvp_list): | ||
""" | ||
Syncs the rsvp list with meetup | ||
|
||
Args: | ||
- meetup_rscp_list : list. | ||
|
||
Returns: | ||
- None | ||
""" | ||
|
||
for rsvp in meetup_rsvp_list: | ||
members = rsvp['member'] | ||
for member in members: | ||
try: | ||
member_id, member_name = member['member_id'], member_name | ||
name_list = member_name.split(' ') | ||
first_name, last_name = name_list[0], name_list[-1] | ||
except KeyError as e: | ||
print("Error decoding data") | ||
print(e) | ||
continue | ||
try: | ||
User.objects.create( | ||
member_id=member_id, | ||
first_name=first_name, | ||
last_name=last_name | ||
) | ||
except Exception as e: | ||
print("Cannot create User %s because: ", member_name) | ||
print(str(e)) | ||
continue | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please use separate branch and use reference from explara integration PR to get this done?