def upload_envelopes(envelope_dir: str, resource_bucket: str, envelope_prefix: str, parties: Store[str, Party]) -> None: client = boto3.client('s3') for envelope in listdir(envelope_dir): envelope_file = path.join(envelope_dir, envelope) if path.isfile(envelope_file) and _is_image(envelope_file): maybe_party = excepts(ClientError, parties.get)(path.splitext( path.basename(envelope_file))[0]) envelope_key = envelope_prefix + ( '/' if not envelope_prefix.endswith('/') else '') + envelope client.upload_file(envelope_file, resource_bucket, envelope_key) option.fmap(lambda party: client.put_object_tagging( Bucket=resource_bucket, Key=envelope_key, Tagging={ 'TagSet': [{ 'Key': 'party', 'Value': ''.join( filter(lambda c: c in string.ascii_letters, party. title))[:256] }] }))(maybe_party)
def _modify_guest(party: Party) -> Optional[Guest]: maybe_guest = get_guest(guest_id)(party) new_guest = option.fmap(modify)(maybe_guest) return compose( set_guest (new_guest), remove_guest(guest_id ) )(party)
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)
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 __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)
def _handle(self, event): maybe_respondent = option.fmap( lambda first_name: {'firstName': first_name})( event.get('firstName')) return Ok( pystache.render( self.__get_template(), valfilter( option.not_none, { 'respondent': maybe_respondent, 'homepageUrl': self.__homepage_url }))).as_json()
def guest(first_name: str, maybe_username: Optional[str] = None, id: Optional[str] = None) -> Guest: return Guest( id = id or 'id', first_name = first_name, last_name = 'Doe', email = option.fmap(lambda username: EmailAddress( username = username, hostname = 'doe.com' ) )(maybe_username), attending = False, rideshare = True )
def modify(self, key: K, transform: Callable[[V], V]) -> Optional[V]: value = option.fmap(transform)(self.get(key)) if value: self.put(value) return value
def get(self, key: K) -> Optional[V]: return option.fmap(self.__val.decode)( self.__table.get_item(Key=self.__encode_key(key)).get('Item'))