コード例 #1
0
def test_group_event_exists(group_1: Group, group_2: Group):
    # init group models
    group_1.save()
    group_2.save()

    # init event
    search_event: Event = Event(
        meetup_id=0,
        created=datetime.now(),
        time=datetime.now(),
        name="",
        link="",
        date_in_series_pattern=False,
    )

    # test when event does not exists
    assert group_1.event_exists(
        event_meetup_id=search_event.meetup_id) is False

    # test with existing event
    group_1.add_event(search_event)
    group_1.save()
    assert group_1.event_exists(event_meetup_id=search_event.meetup_id) is True

    # test with saved event in wrong group
    assert group_2.event_exists(
        event_meetup_id=search_event.meetup_id) is False
def get_event_from_response(response: dict, group: Group) -> Event:
    """
    parse json response and return an Event

    Arguments:
        response {dict} -- meetup api response in a dict
        group {Group} -- Group where the event should be added later

    Raises:
        EventAlreadyExists: Raise when a event with the same meetup_id already in the group exists

    Returns:
        Event -- New Event wich can be added to the group
    """

    # if event already exists, raise
    try:
        if group.event_exists(event_meetup_id=response["id"]):
            raise EventAlreadyExists("Event {} already exists on {}!".format(
                response["id"], group.name))
    except KeyError:
        raise InvalidResponse("No Event in Response!")

    date_in_series_pattern: bool = False
    if "date_in_series_pattern" in response:
        date_in_series_pattern = response["date_in_series_pattern"]

    event: Event = Event(
        meetup_id=response["id"],
        time=datetime.fromtimestamp(response["time"] / 1000),
        name=response["name"],
        link=response["link"],
        date_in_series_pattern=date_in_series_pattern,
    )

    # add optional fields
    if "attendance_count" in response:
        event.attendance_count = response["attendance_count"]
    if "attendance_sample" in response:
        event.attendance_sample = response["attendance_sample"]
    if "attendee_sample" in response:
        event.attendee_sample = response["attendee_sample"]
    if "created" in response:
        event.created = datetime.fromtimestamp(response["created"] / 1000)
    if "description" in response:
        event.description = response["description"]
    if "duration" in response:
        event.duration = response["duration"]
    if "fee" in response:
        event.fee_accepts = response["fee"]["accepts"]
        event.fee_amount = response["fee"]["amount"]
        event.fee_currency = response["fee"]["currency"]
        event.fee_description = response["fee"]["description"]
        event.fee_label = response["fee"]["label"]
    if "how_to_find_us" in response:
        event.how_to_find_us = response["how_to_find_us"]
    if "status" in response:
        event.status = response["status"]
    if "utc_offset" in response:
        event.utc_offset = response["utc_offset"] / 1000
    if "updated" in response:
        event.updated = datetime.fromtimestamp(response["updated"] / 1000)
    if "venue" in response:
        event = get_venue_from_response(response=response["venue"],
                                        event=event)
    if "venue_visibility" in response:
        event.venue_visibility = response["venue_visibility"]
    if "visibility" in response:
        event.visibility = response["visibility"]

    return event