Ejemplo n.º 1
0
def process_event(data, user_id, method='Create'):
    """
    This functions is called from restful POST service (which gets data from
    Event Create Form submission).
    It creates event on vendor as well as saves in database.
    Data in the arguments is the Data coming from Event creation form submission
    user_id is the id of current logged in user (which we get from session).
    :return: id of event
    :rtype: int
    """
    if data:
        social_network_id = data.get('social_network_id', 0)
        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')
            event_class = get_class(social_network.name.lower(), 'event')
            sn = social_network_class(user_id=user_id)
            event_obj = event_class(user=sn.user,
                                    headers=sn.headers,
                                    social_network=social_network)
        else:
            raise SocialNetworkError('Unable to find social network')
        data['user_id'] = user_id
        if social_network.name.lower() == MEETUP and data.get('organizer_id'):
            raise InvalidUsage(
                'organizer_id is not a valid field in case of meetup')
        event_obj.event_gt_to_sn_mapping(data)

        activity_data = {'username': request.user.name}

        if method == 'Create':
            event_id = event_obj.create_event()

            # Create activity of event object creation
            event_obj = Event.get_by_user_and_event_id(user_id=user_id,
                                                       event_id=event_id)
            activity_data.update({'event_title': event_obj.title})
            add_activity(user_id=user_id,
                         activity_type=Activity.MessageIds.EVENT_CREATE,
                         source_id=event_id,
                         source_table=Event.__tablename__,
                         params=activity_data)
            return event_id
        else:
            event_id = event_obj.update_event()

            # Create activity of event object creation
            event_obj = Event.get_by_user_and_event_id(user_id=user_id,
                                                       event_id=event_id)

            activity_data.update({'event_title': event_obj.title})

            add_activity(user_id=user_id,
                         activity_type=Activity.MessageIds.EVENT_UPDATE,
                         source_id=event_id,
                         source_table=Event.__tablename__,
                         params=activity_data)

            return event_id
    else:
        error_message = 'Data not received from Event Creation/Edit FORM'
        log_error({'user_id': user_id, 'error': error_message})
Ejemplo n.º 2
0
    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)