def main(args):
    write_to_html = write_party_html(args.html_template)
    compose(
        option.cata(write_to_html, lambda: identity)(args.html_output),
        option.cata(post_to_database, lambda: identity)(args.parties_table),
        option.cata(write_party_json, lambda: identity)(args.write_json),
        parse_parties, read_addresses)(args.address_file)
Пример #2
0
 def __init__(self, message: Optional[str]) -> None:
     super().__init__(
         option.cata(
             '{{ "message": "{}" }}'.format,
             lambda: ''
         )(message)
     )
Пример #3
0
    def __post(self, form: RideShareFormData) -> HttpResponse:
        self.__logger.debug(f'Rideshare submitted: {form}')
        try:
            party = self.__parties.modify(
                form.party_id,
                modify_guest(
                    form.guest_id,
                    lambda guest: guest._replace(rideshare=form.rideshare)))
        except ClientError as exc:
            self.__logger.error(
                f'Ride share preference submitted for guest "{form.guest_id}" in party "{form.party_id}", '
                +
                f'but database operation failed with error "{exc}. Form data = {form}'
            )
            return InternalServerError(
                'Something has gone horribly wrong...please call Jesse and let him know!'
            )

        maybe_guest = option.fmap(get_guest(form.guest_id))(party)

        return option.cata(
            lambda guest: TemporaryRedirect(
                pystache.render(self.__thank_you_url,
                                {'firstName': guest.first_name})),
            lambda: self.__internal_error)(maybe_guest)
Пример #4
0
def _template_getter():
    template_bucket = args.template_bucket.split('/')[0]
    templates = boto3.resource('s3').Bucket(template_bucket)
    template_prefix = option.cata(lambda prefix: prefix + '/', lambda: '')(get(
        1, args.template_bucket.split('/'), None))

    return lambda key: templates.Object(template_prefix + key).get()[
        'Body'].read().decode('utf-8')
Пример #5
0
    def __route(self, event):
        method = event[self.METHOD_FIELD].upper()
        query = event.get(self.QUERY_FIELD) or {}
        path = event.get(self.PATH_FIELD) or {}
        maybe_id = path.get('id')

        if method == 'GET':
            return option.cata(self._get,
                               lambda: self._get_many(query))(maybe_id)
        elif method == 'POST':
            body = self.__payload(event)
            return (self._post_many(body)
                    if isinstance(body, list) else self._post(body))
        elif method == 'DELETE':
            return option.cata(self._delete,
                               lambda: self._delete_many(query))(maybe_id)
        else:
            return MethodNotAllowed()
Пример #6
0
def get_credentials(client_secret_file: str,
                    token_storage_file: str,
                    user_id: str) -> Credentials:
    tokens = _get_tokens(token_storage_file)
    credentials = option.cata(
        Credentials,
        lambda: _get_new_credentials(client_secret_file)
    )(tokens.get(user_id))
    _save_tokens(token_storage_file, assoc(tokens, user_id, credentials.token))
    return credentials
Пример #7
0
    def __get(self, event):
        party_id: str = event['partyId']
        guest_id: str = event['guestId']

        maybe_get_context, get_template = option.fmap(lambda party: (partial(
            self.__rsvp_context, party), self.__rsvp_template))(
                self.__parties.get(party_id))

        return option.cata(
            lambda get_context: self.__render(get_template(),
                                              get_context(guest_id)),
            lambda: self.__internal_error)(maybe_get_context)
Пример #8
0
    def _handle(self, event):
        party_id = event['partyId']
        guest_id = event['guestId']

        self.__parties.modify(
            party_id,
            advance_stage(CardClicked)
        )

        return option.cata(
            lambda party: self.__render_invitation(guest_id, party),
            lambda: self.__redirect
        )(self.__parties.get(party_id)).as_json()
Пример #9
0
    def get_all(self) -> Iterable[V]:
        get_more = True
        maybe_last_key = None

        while get_more:
            response = option.cata(
                lambda last_key: self.__table.scan(ExclusiveStartKey=last_key),
                self.__table.scan)(maybe_last_key)

            maybe_last_key = response.get('LastEvaluatedKey')
            get_more = maybe_last_key is not None

            for item in map(self.__val.decode, response.get('Items', [])):
                yield item
Пример #10
0
    def __post(self, event):
        raw_form: str = event['query']
        form = RsvpFormData.parse(raw_form)

        self.__logger.debug(f'RSVP submitted: {form}')

        def set_attending(guest: Guest):
            return modify_guest(
                guest.id, lambda g: g._replace(attending=form.attending.get(
                    g.id, False)))

        try:
            maybe_party = self.__parties.modify(
                form.party_id,
                compose(advance_stage(RsvpSubmitted),
                        lambda p: sequence.foldr(set_attending)(p.guests)(p)))
        except ClientError as exc:
            self.__logger.error(
                f'RSVP submitted for party "{form.party_id}", but database operation failed with error "{exc}. '
                + f'Raw form data = {raw_form}, parsed form data = {form}')
            return InternalServerError(
                'Something has gone horribly wrong...please call Jesse and let him know!'
            )

        def _rideshare_url(party: Party):
            maybe_guest = get_guest(form.guest_id)(party)
            rideshare_query = RideShareQuery(
                local=party.local,
                guest_id=form.guest_id,
                party_id=form.party_id,
                rideshare=option.fmap(lambda guest: guest.rideshare)
                (maybe_guest) or False)
            return pystache.render(self.__rideshare_url_template,
                                   RideShareQueryCodec.encode(rideshare_query))

        def redirect(party: Party):
            return TemporaryRedirect(self.__decline_url if all(
                not guest.attending
                for guest in party.guests) else _rideshare_url(party))

        return option.cata(redirect,
                           lambda: self.__internal_error)(maybe_party)
Пример #11
0
 def __payload(self, event):
     body = json.loads(event['body'])
     return option.cata(partial(compose(list, map), self.__codec.decode),
                        lambda: self.__codec.decode(body))(
                            body.get('items'))