Esempio n. 1
0
def checkin(request):
    context = request["context"]
    entities = request["entities"]
    session = Session.objects.get(conv_id=request["session_id"])
    person = session.person

    check_intent(entities, session)

    short_code = value(entities, session, "short_code")

    try:
        event_settings = EventSettings.objects.get(short_code=short_code)
    except ObjectDoesNotExist as err:
        return finish(session, context, "not_found")

    checkin_enabled = event_settings.checkin_enabled
    checkin_message = event_settings.checkin_message
    event = event_settings.event

    if not checkin_enabled:
        return finish(session, context, "not_found")

    now = datetime.now(timezone.utc)
    cutoff = event.start + (event.end - event.start) / 2
    if now < event.start:
        return finish(session, context, "early")
    elif now > cutoff:
        checkin_exists = Checkin.objects.filter(
            person=person,
            event=event_settings.event
        ).exists()
        if checkin_exists:
            return finish(session, context, "duplicate")
        else:
            return finish(session, context, "late")

    checkin_obj, is_new_checkin = Checkin.objects.get_or_create(
        person=person,
        event=event_settings.event
    )

    if not is_new_checkin:
        return finish(session, context, "duplicate")

    if checkin_message:
        send_sms.delay(person.id, checkin_message.id)
    return finish(session, context, "success")
Esempio n. 2
0
def photo_consent(request):
    context = request["context"]
    entities = request["entities"]
    session = Session.objects.get(conv_id=request["session_id"])
    person = session.person

    check_intent(entities, session)

    yes_no = value(entities, session, "yes_no")

    if yes_no == "yes":
        person.photo_consent = True
        person.save()
        return finish(session, context, "granted")
    else:
        person.photo_consent = False
        person.save()
        return finish(session, context, "denied")
Esempio n. 3
0
def classification(request):
    context = request["context"]
    entities = request["entities"]
    session = Session.objects.get(conv_id=request["session_id"])
    person = session.person

    check_intent(entities, session)

    classification = value(entities, session, "classification")

    if not classification:
        return finish(session, context, "failure", False)

    person.classification = classification
    try:
        person.save()
    except DataError as err:
        return finish(session, context, "failure", False)

    return finish(session, context, "success", False)
Esempio n. 4
0
def email(request):
    context = request["context"]
    entities = request["entities"]
    session = Session.objects.get(conv_id=request["session_id"])
    person = session.person

    check_intent(entities, session)

    email = value(entities, session, "email")

    if not email:
        return finish(session, context, "failure", False)

    person.email = email
    try:
        person.save()
    except DataError as err:
        return finish(session, context, "failure", False)

    return finish(session, context, "success", False)
Esempio n. 5
0
def rsvp(request):
    context = request["context"]
    entities = request["entities"]
    session = Session.objects.get(conv_id=request["session_id"])
    person = session.person

    log_debug(person)
    check_intent(entities, session)

    rsvp_type = value(entities, session, "rsvp_type")
    event_summary = value(entities, session, "event")

    if not event_summary:
        return finish(session, context, "not_found", False)

    start = datetime_value(entities, session)
    if start:
        end = start + timedelta(days=1)
        if end < datetime.now(timezone.utc):
            return finish(session, context, "not_found", False)

    try:
        if start:
            event = Event.objects.get(
                summary__icontains=event_summary,
                start__range=[start, end]
            )
        else:
            event = Event.objects.get(
                summary__icontains=event_summary,
                start__gte=datetime.now(timezone.utc)
            )
    except (ObjectDoesNotExist, MultipleObjectsReturned) as err:
        return finish(session, context, "not_found", False)

    event_settings = event.settings
    rsvp_enabled = event_settings.rsvp_enabled
    rsvp_limit = event_settings.rsvp_limit
    rsvp_message = event_settings.rsvp_message
    rsvp_count = RSVP.objects.filter(event=event).count()
    rsvp_exists = RSVP.objects.filter(event=event, person=person).exists()

    if rsvp_type == "count":

        if not rsvp_enabled:
            return finish(session, context, "disabled")
        elif rsvp_count == 0:
            return finish(session, context, "none")
        elif rsvp_count == 1:
            return finish(session, context, "single")
        else:
            return finish(session, context, "count", val=rsvp_count)

    elif rsvp_type == "rsvp" and not rsvp_exists:

        if not rsvp_enabled:
            return finish(session, context, "disabled")
        elif event.start <= datetime.now(timezone.utc):
            return finish(session, context, "full")
        elif rsvp_limit is not None and rsvp_count >= rsvp_limit:
            return finish(session, context, "full")
        else:
            RSVP(event=event, person=person).save()
            context["summary"] = event.summary
            start = event.start.astimezone(timezone(-timedelta(hours=5)))
            context["time"] = datetime.strftime(start, "%a, %b %d, %I:%M %p")
            if rsvp_message:
                send_sms.delay(person.id, rsvp_message.id)
            return finish(session, context, "location", val=event.location)

    elif rsvp_type == "rsvp" and rsvp_exists:
        return finish(session, context, "rsvp_dup")
    elif rsvp_type == "unrsvp" and rsvp_exists:
        RSVP.objects.filter(event=event, person=person).delete()
        return finish(session, context, "unrsvpd")
    else:
        return finish(session, context, "unrsvp_dup")