def delete(self): """ Deletes multiple social network whose ids are given in list in request data. :Example: social_network_ids = { 'ids': [1,2,3] } headers = { 'Authorization': 'Bearer <access_token>', 'Content-Type': 'application/json' } data = json.dumps(social_network_ids) response = requests.post( API_URL + '/social-network/', data=data, headers=headers, ) .. Response:: { 'message': '3 social networks have been deleted successfully' } .. HTTP Status:: 200 (Resource Deleted) 207 (Not all deleted) 400 (Bad request) 500 (Internal Server Error) """ # get event_ids for events to be deleted req_data = get_valid_json_data(request) social_network_ids = req_data['social_network_ids'] \ if 'social_network_ids' in req_data and isinstance( req_data['social_network_ids'], list) else [] total_deleted = 0 total_not_deleted = 0 if social_network_ids: for sn_id in social_network_ids: try: if SocialNetwork.get_by_id(sn_id): SocialNetwork.delete(sn_id) total_deleted += 1 except Exception as e: total_not_deleted += 1 logger.info( 'Unable to delete social network with ID: %s\nError: %s' % (sn_id, e.message)) if total_not_deleted: return dict(message='Unable to delete %s social networks' % total_not_deleted, deleted=total_deleted, not_deleted=total_not_deleted), codes.MULTI_STATUS elif total_deleted: return dict(message='%s social networks deleted successfully' % total_deleted) raise InvalidUsage('Bad request, include social work ids as list data', error_code=codes.BAD_REQUEST)
def post(self): """ This endpoint triggers an event synchronizer(Celery task) against a post request with valid data Example: { "social_network_id": 1 } :return: { "message": "Your events are being updated } """ data = get_valid_json_data(request) social_network_id = data.get('social_network_id') # Required if not social_network_id: raise InvalidUsage('Please provide social_network_id') if not isinstance(social_network_id, (int, long)): raise InvalidUsage("Invalid social_network_id type") user_credentials = UserSocialNetworkCredential.get_by_user_and_social_network_id( request.user.id, social_network_id) if user_credentials: sync_events.delay(user_credentials) return ApiResponse({'message': 'Your events are being updated'}) raise ResourceNotFound( "User credentials not found for user_id: %d and social_network_id: %d" % (request.user.id, social_network_id))
def delete(self): """ This endpoint deletes one or more organizer owned by this user. :Example: organizers_ids = { 'ids': [1,2,3] } headers = { 'Authorization': 'Bearer <access_token>', 'Content-Type': 'application/json' } data = json.dumps(organizers_ids) response = requests.post( API_URL + '/event-organizers/', data=data, headers=headers, ) .. Response:: { 'message': '3 event organizers have been deleted successfully' } .. HTTP Status:: 200 (Resource Deleted) 207 (Not all deleted) 400 (Bad request) 500 (Internal Server Error) """ deleted, not_deleted = [], [] # Get json data from request req_data = get_valid_json_data(request) organizer_ids = req_data['ids'] if 'ids' in req_data and isinstance( req_data['ids'], list) else [] # If no organizer id is given, return 400 (Bad request) if organizer_ids: for _id in organizer_ids: organizer = EventOrganizer.get_by_user_id_organizer_id( request.user.id, _id) if organizer: EventOrganizer.delete(_id) deleted.append(_id) else: not_deleted.append(_id) if not not_deleted: return dict( message='%s event organizer(s) deleted successfully' % len(deleted)) return dict(message='Unable to delete %s event organizer(s)' % len(not_deleted), deleted=deleted, not_deleted=not_deleted), 207 else: raise InvalidUsage('Bad request, include ids as list data', error_code=400)
def delete(self): """ This endpoint deletes venues specified in list in request data. :Example: venue_ids = { 'ids': [1,2,3] } headers = { 'Authorization': 'Bearer <access_token>', 'Content-Type': 'application/json' } data = json.dumps(venue_ids) response = requests.post( API_URL + '/venues/', data=data, headers=headers, ) .. Response:: { 'message': '3 Venues have been deleted successfully' } .. HTTP Status:: 200 (Resource Deleted) 207 (Not all deleted) 400 (Bad request) 500 (Internal Server Error) """ user_id = request.user.id deleted, not_deleted = [], [] req_data = get_valid_json_data(request) venue_ids = req_data['ids'] if 'ids' in req_data and isinstance( req_data['ids'], list) else [] if venue_ids: for _id in venue_ids: venue = Venue.get_by_user_id_venue_id(user_id, _id) if venue: Venue.delete(_id) deleted.append(_id) else: not_deleted.append(_id) if not not_deleted: return dict(message='%s Venue(s) deleted successfully' % len(deleted)) return dict(message='Unable to delete %s venue(s)' % len(not_deleted), deleted=deleted, not_deleted=not_deleted), 207 else: raise InvalidUsage('Bad request, include ids as list data')
def post(self): """ Creates an event organizer for this user. :Example: organizer_data = { "name": "Zohaib Ijaz", "email": "*****@*****.**", "about": "I am a software engineer" } headers = { 'Authorization': 'Bearer <access_token>', 'Content-Type': 'application/json' } data = json.dumps(organizer_data) response = requests.post( API_URL + '/event-organizers/', data=data, headers=headers, ) .. Response:: { "message" : 'Event organizer created successfully' 'id' : 123 } .. HTTP Status:: 201 (Resource Created) 500 (Internal Server Error) """ organizer_data = get_valid_json_data(request) user_id = request.user.id social_network = SocialNetwork.get_by_name('Eventbrite') eventbrite = Eventbrite(user_id, social_network_id=social_network.id) organizer_id = eventbrite.create_event_organizer(organizer_data) organizer_data['social_network_organizer_id'] = organizer_id organizer_data['social_network_id'] = social_network.id organizer_data['user_id'] = user_id organizer = EventOrganizer(**organizer_data) EventOrganizer.save(organizer) headers = { 'Location': '{url}/{id}'.format(url=SocialNetworkApi.EVENT_ORGANIZERS, id=organizer.id) } return ApiResponse(dict( messsage='Event organizer created successfully', id=organizer.id), status=201, headers=headers)
def put(self, venue_id): """ Updates a venue for current user :param venue_id: id of the requested venue :Example: venue_data = { "address_line_2": "", "city": "Cupertino", "address_line_1": "Infinite Loop", "social_network_id": 13, "country": "us", "zip_code": "95014", "longitude": 0, "social_network_venue_id": "15570022", "state": "CA", "latitude": 0, "id": 1 } headers = { 'Authorization': 'Bearer <access_token>', 'Content-Type': 'application/json' } data = json.dumps(venue_data) response = requests.post( API_URL + '/venues/1', data=data, headers=headers, ) .. Response:: { 'message': 'Venue updated successfully' } .. HTTP Status:: 200 (Resource Updated) 500 (Internal Server Error) """ user_id = request.user.id venue_data = get_valid_json_data(request) venue = Venue.get_by_user_id_venue_id(user_id, venue_id) if venue: venue_data['user_id'] = user_id venue.update(**venue_data) return dict(message='Venue updated successfully') else: raise ResourceNotFound('Venue not found')
def post(self, **kwargs): """ This method takes data to create social network in database. :Example: social_network = { "name": 'Github', "url": 'http://www.github.com/', "apiUrl": "http://www.github.com/v1/api/", "clientKey": "client_key_here", "secretKey": "client_secret_goes_here", "redirectUri": "http://gettalent.com", "authUrl": "http://www.github.com/auth", } headers = { 'Authorization': 'Bearer <access_token>', 'Content-Type': 'application/json' } data = json.dumps(social_network) response = requests.post( API_URL + '/social-network/', data=data, headers=headers, ) .. Response:: { id: 123232 } .. HTTP Status:: 201 (Resource Created) 500 (Internal Server Error) 401 (Unauthorized to access getTalent) :return: id of created event """ # get json post request data sn_data = get_valid_json_data(request) social_network = SocialNetwork(**sn_data) SocialNetwork.save(social_network) headers = { 'Location': '{url}/{id}'.format(url=SocialNetworkApi.SOCIAL_NETWORKS, id=social_network.id) } response = ApiResponse(dict(id=social_network.id), status=201, headers=headers) return response
def delete(self): """ Deletes multiple event whose ids are given in list in request data. :return: :Example: event_ids = { 'ids': [1,2,3] } headers = { 'Authorization': 'Bearer <access_token>', 'Content-Type': 'application/json' } data = json.dumps(event_ids) response = requests.post( API_URL + '/events/', data=data, headers=headers, ) .. Response:: { 'message': '3 Events have been deleted successfully' } .. Status:: 200 (Resource deleted) 207 (Not all deleted) 400 (Bad request) 500 (Internal Server Error) """ # get event_ids for events to be deleted req_data = get_valid_json_data(request) event_ids = req_data['ids'] if 'ids' in req_data and isinstance( req_data['ids'], list) else [] # check if event_ids list is not empty if event_ids: deleted, not_deleted = delete_events(request.user.id, event_ids) if not not_deleted: return dict(message='%s Events deleted successfully' % len(deleted)) return dict(message='Unable to delete %s events' % len(not_deleted), deleted=deleted, not_deleted=not_deleted), 207 raise InvalidUsage('Bad request, include event_ids as list data')
def post(self, organizer_id): """ Updates an event organizer for current user. :Example: organizer_data = { 'id': 1, "name": "My Organizer", "email": "*****@*****.**", "about": "He arranges events" } headers = { 'Authorization': 'Bearer <access_token>', 'Content-Type': 'application/json' } data = json.dumps(organizer_data) response = requests.post( API_URL + '/event-organizers/1', data=data, headers=headers, ) .. Response:: { "message" : 'Event organizer updated successfully' } .. HTTP Status:: 200 (Resource Updated) 500 (Internal Server Error) """ user_id = request.user.id organizer_data = get_valid_json_data(request) event_organizer = EventOrganizer.get_by_user_id_organizer_id( user_id, organizer_id) if event_organizer: organizer_data['user_id'] = user_id event_organizer.update(**organizer_data) return dict(message='Organizer updated successfully') else: raise ResourceNotFound("Organizer not found")
def put(self, event_id): """ Updates event in getTalent's database and on corresponding social network. :param event_id: id of event on getTalent database :Example: event_data = { "organizer_id": 1, "venue_id": 2, "title": "Test Event", "description": "Test Event Description", "registration_instruction": "Just Come", "end_datetime": "30 Oct, 2015 04:51 pm", "group_url_name": "QC-Python-Learning", "social_network_id": 18, "timezone": "Asia/Karachi", "cost": 0, "start_datetime": "25 Oct, 2015 04:50 pm", "currency": "USD", "social_network_group_id": 18837246, "max_attendees": 10 } headers = { 'Authorization': 'Bearer <access_token>', 'Content-Type': 'application/json' } data = json.dumps(event_data) event_id = event_data['id'] response = requests.post( API_URL + '/events/' + str(event_id)', data=data, headers=headers, ) .. Response:: No Content .. Status:: 200 (Resource Modified) 500 (Internal Server Error) 401 (Unauthorized to access getTalent) 403 (Forbidden: Can not update specified event) .. Error codes (returned in response's body): In case of internal server error, response contains error code which can be 4052 (Unable to determine Social Network) 4053 (Some Required event fields are missing) 4055 (Event not created) 4056 (Event not Published on Social Network) 4058 (Event venue not created on Social Network) 4059 (Tickets for event not created) 4060 (Event was not save in getTalent database) 4061 (User credentials of user for Social Network not found) 4062 (No implementation for specified Social Network) 4064 (Invalid datetime for event) 4065 (Specified Venue not found in database) 4066 (Access token for Social Network has expired) """ event_data = get_valid_json_data(request) # check whether given event_id exists for this user event = Event.get_by_user_id_event_id_social_network_event_id( request.user.id, event_id, event_data['social_network_event_id']) if not event: raise ResourceNotFound('Event not found') process_event(event_data, request.user.id, method='Update') return dict(message='Event updated successfully')
def post(self): """ This method takes data to create event in local database as well as on corresponding social network. :Example: event_data = { "organizer_id": 1, "venue_id": 2, "is_deleted_from_vendor": 1, "title": "Test Event", "description": "Test Event Description", "registration_instruction": "Just Come", "end_datetime": "30 Oct, 2015 04:51 pm", "group_url_name": "QC-Python-Learning", "social_network_id": 18, "timezone": "Asia/Karachi", "cost": 0, "start_datetime": "25 Oct, 2015 04:50 pm", "currency": "USD", "social_network_group_id": 18837246, "max_attendees": 10 } headers = { 'Authorization': 'Bearer <access_token>', 'Content-Type': 'application/json' } data = json.dumps(event_data) response = requests.post( API_URL + '/events/', data=data, headers=headers, ) .. Response:: { id: 123232 } .. Status:: 201 (Resource Created) 500 (Internal Server Error) 401 (Unauthorized to access getTalent) .. Error codes: In case of internal server error, response contains error code which can be 4052 (Unable to determine Social Network) 4053 (Some required event fields are missing) 4055 (Event not created) 4056 (Event not Published on Social Network) 4058 (Event venue not created on Social Network) 4059 (Tickets for event not created) 4060 (Event was not saved in getTalent database) 4061 (User credentials of user for Social Network not found) 4062 (No implementation for specified Social Network) 4064 (Invalid datetime for event) 4065 (Specified Venue not found in database) 4066 (Access token for Social Network has expired) :return: id of created event """ # get json post request data event_data = get_valid_json_data(request) gt_event_id = process_event(event_data, request.user.id) headers = { 'Location': '{url}/{id}'.format(url=SocialNetworkApi.EVENT_ORGANIZERS, id=gt_event_id) } return ApiResponse(dict(id=gt_event_id), headers=headers, status=201)
def post(self): """ Creates a venue for this user :Example: venue_data = { "zip_code": "95014", "social_network_id": 13, "address_line_2": "", "address_line_1": "Infinite Loop", "latitude": 0, "longitude": 0, "state": "CA", "city": "Cupertino", "country": "us" } headers = { 'Authorization': 'Bearer <access_token>', 'Content-Type': 'application/json' } data = json.dumps(venue_data) response = requests.post( API_URL + '/venues/', data=data, headers=headers, ) .. Response:: { "message" : 'Venue created successfully' 'id' : 123 } .. HTTP Status:: 201 (Resource Created) 500 (Internal Server Error) """ user_id = request.user.id venue_data = get_valid_json_data(request) mandatory_input_data = [ 'address_line_1', 'city', 'country', 'state', 'social_network_id' ] # gets fields which are missing missing_items = [ key for key in mandatory_input_data if not venue_data.get(key) ] if missing_items: raise InvalidUsage("Mandatory Input Missing: %s" % missing_items, error_code=custom_codes.MISSING_REQUIRED_FIELDS) social_network_id = venue_data['social_network_id'] social_network_venue_id = venue_data.get('social_network_venue_id') if social_network_venue_id: venue = Venue.get_by_user_id_and_social_network_venue_id( user_id, social_network_venue_id) if venue: raise InvalidUsage( 'Venue already exists in getTalent database', error_code=VENUE_EXISTS_IN_GT_DATABASE) venue_data['user_id'] = user_id venue = SocialNetworkBase.save_venue(venue_data) else: social_network = SocialNetwork.get(social_network_id) if social_network: # creating class object for respective social network social_network_class = get_class(social_network.name.lower(), 'social_network') social_network = social_network_class(user_id=user_id) else: raise InvalidUsage( 'Unable to find social network with given id: %s' % social_network_id) venue = social_network.add_venue_to_sn(venue_data) headers = { 'Location': '{url}/{id}'.format(url=SocialNetworkApi.VENUES, id=venue.id) } return ApiResponse(dict(message='Venue created successfully', id=venue.id), status=201, headers=headers)
def post(self, social_network_id): """ Adds credentials for user for given social network. Gets data from POST request which contains 'code' and 'social_credentials' :param args: :param kwargs: :return: :Example: data = { 'code': '32432ffd2s8fd23e8saq123ds6a3da21221 } headers = { 'Authorization': 'Bearer <access_token>', 'Content-Type': 'application/json' } data = json.dumps(data) response = requests.post( API_URL + '/social-networks/13/user/credentials', data=data, headers=headers, ) .. Response:: { "message" : 'User credentials for social network were added successfully' } .. HTTP Status:: 201 (Resource Updated) 404 (Social Network not found) 500 (Internal Server Error) """ user_id = request.user.id # Get json request data req_data = get_valid_json_data(request) code = req_data['code'] social_network = SocialNetwork.get_by_id(social_network_id) # if social network does not exists, send failure message if social_network: # Get social network specific Social Network class social_network_class = get_class(social_network.name, 'social_network') credentials = social_network_class( user_id, social_network.id, validate_credentials=False).connect(code) logger.info( 'User(id:%s) has been connected successfully with %s. We are going to import events now.' % (user_id, social_network.name)) events_count = Event.enable_events(user_id, social_network.id) logger.info( 'User (id: %s) has been connected to %s and his %s events have been enabled' % (user_id, social_network.name, events_count)) sync_events.delay(credentials) return dict( message='User credentials added successfully'), codes.CREATED else: raise ResourceNotFound('Social Network not found')