def delete(self, venue_id): """ This endpoint deletes one organizer owned by this user. :param int | long venue_id: venue id :Example: headers = { 'Authorization': 'Bearer <access_token>', 'Content-Type': 'application/json' } response = requests.delete( API_URL + '/organizers/1', headers=headers, ) .. Response:: { 'message': 'Organizer has been deleted successfully' } .. HTTP Status:: 200 (Resource Deleted) 404 (Not found) 500 (Internal Server Error) """ organizer = EventOrganizer.get_by_user_id_organizer_id( request.user.id, venue_id) if organizer: EventOrganizer.delete(organizer) return dict(message='Organizer has been deleted successfully') else: raise ResourceNotFound("Organizer not found")
def test_post_with_same_organizer_name(self, token_first, user_first, test_eventbrite_credentials): """ Send POST request with valid event organizer data and response should be 201 but when we will try to create organizer with same name again, API will raise InvalidUsage 400 error. """ name = "organizer_%s" % fake.uuid4(), event_organizer = { "user_id": user_first['id'], "name": name, "email": "*****@*****.**", "about": "He is a testing engineer" } response = requests.post(SocialNetworkApiUrl.EVENT_ORGANIZERS, data=json.dumps(event_organizer), headers=get_headers(token_first)) logger.info(response.text) assert response.status_code == codes.CREATED, 'Status should be Ok, Resource created (201)' assert 'Location' in response.headers response = response.json() event_organizer_id = response['id'] assert event_organizer_id > 0 # Now try to create organizer with same name. It will raise 400 (InvalidUsage) response = requests.post(SocialNetworkApiUrl.EVENT_ORGANIZERS, data=json.dumps(event_organizer), headers=get_headers(token_first)) logger.info(response.text) assert response.status_code == codes.BAD, 'Status should be 400, InvalidUsage' EventOrganizer.session.commit() event_organizer = EventOrganizer.get_by_id(event_organizer_id) assert event_organizer, 'Event organizer not found in db' EventOrganizer.delete(event_organizer.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 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 resolve_organizer(self, args, request, info): """ Resolves current user's created organizer object specified by given id. """ organizer_id = args.get('id') event_organizer = EventOrganizer.get_by_user_id_organizer_id( request.user.id, organizer_id) return event_organizer
def test_post_with_valid_token(self, token_first, user_first, test_eventbrite_credentials): """ Send POST request with valid event organizer data and response should be 201 (id in response content) """ event_organizer = { "user_id": user_first['id'], "name": "organizer_%s" % fake.uuid4(), "email": "*****@*****.**", "about": "He is a testing engineer" } response = requests.post(SocialNetworkApiUrl.EVENT_ORGANIZERS, data=json.dumps(event_organizer), headers=get_headers(token_first)) logger.info(response.text) assert response.status_code == codes.CREATED, 'Status should be Ok, Resource created (201)' assert 'Location' in response.headers response = response.json() assert response['id'] > 0 EventOrganizer.session.commit() event_organizer = EventOrganizer.get_by_id(response['id']) assert event_organizer, 'Event organizer created successfully in db' EventOrganizer.delete(event_organizer.id)
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 get(self, organizer_id): """ This action returns an organizer (given by id) created by current user. :param organizer_id: id of organizer to be returned :Example: headers = {'Authorization': 'Bearer <access_token>'} organizer_id = 1 response = requests.get( API_URL + '/event-organizers/' + str(organizer_id), headers=headers ) .. Response:: { "event_organizer": { "id": 1 "user_id": 1, "name": "Zohaib Ijaz", "email": "*****@*****.**", "about": "I am a software engineer" } } .. HTTP Status:: 200 (OK) 404 (Resource not found) 500 (Internal Server Error) """ user_id = request.user.id event_organizer = EventOrganizer.get_by_user_id_organizer_id( user_id, organizer_id) if not event_organizer: raise ResourceNotFound('Event organizer not found') event_organizer = event_organizer.to_json() return dict(event_organizer=event_organizer)
def event_sn_to_gt_mapping(self, event): """ We take event's data from social network's API and map its fields to getTalent database fields. Finally we return Event's object to save/update record in getTalent database. We also issue some calls to get updated venue and organizer information. :param event: data from Facebook API. :type event: dictionary :exception Exception: It raises exception if there is an error getting data from API. :return: event: Event object :rtype event: common.models.event.Event """ venue = None location = None venue_id = None organizer = None organizer_id = None assert event is not None owner = event.get('owner') if event.get('place'): venue = event.get('place') location = venue['location'] try: organizer = self.graph.get_object('v2.4/' + owner['id']) organizer = organizer.get('data') except facebook.GraphAPIError: logger.exception( 'event_sn_to_gt_mapping: Getting data of organizer. ' 'user_id: %s, social_network_event_id: %s' % (self.user.id, event['id'])) raise if owner or organizer: organizer_data = dict( user_id=self.user.id, name=owner['name'] if owner and owner.has_key('name') else '', email=organizer['email'] if organizer and organizer.has_key('email') else '', about='') organizer_in_db = EventOrganizer.get_by_user_id_and_name( self.user.id, owner['name'] if owner and owner.has_key('name') else '') if organizer_in_db: organizer_in_db.update(**organizer_data) organizer_id = organizer_in_db.id else: organizer_instance = EventOrganizer(**organizer_data) EventOrganizer.save(organizer_instance) organizer_id = organizer_instance.id if venue: venue_data = dict( social_network_venue_id=venue['id'], user_id=self.user.id, address_line_1=location['street'] if location.has_key('street') else '', address_line_2='', city=location['city'].title() if location.has_key('city') else '', state='', zip_code=location['zip'] if location.has_key('zip') else None, country=location['country'].title() if location.has_key('country') else '', longitude=float(location['longitude']) if location.has_key('longitude') else 0, latitude=float(location['latitude']) if location.has_key('latitude') else 0, ) venue_in_db = Venue.get_by_user_id_and_social_network_venue_id( self.user.id, venue['id']) if venue_in_db: venue_in_db.update(**venue_data) venue_id = venue_in_db.id else: venue = Venue(**venue_data) Venue.save(venue) venue_id = venue.id try: event = Event(social_network_event_id=event['id'], title=event['name'], description=event.get('description', ''), social_network_id=self.social_network.id, user_id=self.user.id, organizer_id=organizer_id, venue_id=venue_id, social_network_group_id=0, start_datetime=event.get('start_time'), end_datetime=event.get('end_time'), timezone=event.get('timezone'), registration_instruction='', cost=0, currency='', max_attendees=event['attending_count'] + event['maybe_count'] + event['noreply_count'] if (event and event.has_key('attending_count') and event.has_key('maybe_count') and event.has_key('noreply_count')) else '') except: logger.exception('event_sn_to_gt_mapping: user_id: %s, ' 'social_network_event_id: %s' % (self.user.id, event['id'])) else: return event