Ejemplo n.º 1
0
 def display_name(self):
     if self is None:
         return 'Unknown'
     full_country = names.get_country_name(self.country_code)
     if self.country_code in ['US']:
         city_name = '%s, %s, %s' % (self.ascii_name, self.admin1_code, full_country)
     else:
         city_name = '%s, %s' % (self.ascii_name, full_country)
     return city_name
    def render_page(self, message=None, error=None):
        total_time = 10

        self.display['country_codes'] = sorted(country_dialing_codes.mapping.items())
        self.display['android_url'] = mobile.ANDROID_URL
        self.display['ios_url'] = mobile.IOS_URL
        self.display['total_time'] = total_time
        if message:
            self.display['messages'] = [message]
        if error:
            self.display['errors'] = [error]
        self.display['suppress_promos'] = True
        if self.request.get('prefix'):
            self.display['prefix'] = self.request.get('prefix')
        else:
            iso3166_country = self.request.headers.get("X-AppEngine-Country")
            full_country = names.get_country_name(iso3166_country)
            self.display['prefix'] = country_dialing_codes.mapping.get(full_country, '')
        self.display['phone'] = self.request.get('phone')
        self.render_template('mobile_apps')
Ejemplo n.º 3
0
    def get_location_from_headers(self, city=True):
        address = None
        ip = ips.get_remote_ip(self.request)
        try:
            address = ip_geolocation.get_location_string_for(ip, city=city)
        except:
            logging.exception('Failure to geolocate IP %s, falling back on old-school resolution', ip)
        if not address:
            from dancedeets.loc import names
            iso3166_country = self.request.headers.get("X-AppEngine-Country")
            full_country = names.get_country_name(iso3166_country)

            location_components = []
            if city:
                location_components.append(self.request.headers.get("X-AppEngine-City"))
            if full_country in ['United States', 'Canada']:
                location_components.append(self.request.headers.get("X-AppEngine-Region"))
            if full_country != 'ZZ':
                location_components.append(full_country)
            location = ', '.join(x for x in location_components if x and x != '?')
            address = location
        return address
Ejemplo n.º 4
0
def email_for_user(user, fbl, should_send=True):
    if not user.send_email:
        raise NoEmailException('User has send_email==False')
    email_address = user.email
    if not email_address:
        raise NoEmailException('User does not have an email')

    if user.weekly_email_send_date and user.weekly_email_send_date > datetime.datetime.now(
    ) - datetime.timedelta(days=3):
        message = "Skipping user %s (%s) because last weekly email was sent on %s" % (
            user.fb_uid, user.full_name, user.weekly_email_send_date)
        logging.warning(message)
        # Sent the weekly email too recently, let's abort
        raise NoEmailException(message)
    fb_user = fbl.fetched_data(fb_api.LookupUser, fbl.fb_uid)
    if not 'profile' in fb_user:
        raise NoEmailException('Could not find LookupUser: %s', fb_user)

    user_location = user.location
    if not user_location:
        raise NoEmailException('User does not have location')

    d = datetime.date.today()
    start_time = d - datetime.timedelta(
        days=d.weekday())  # round down to last monday
    end_time = start_time + datetime.timedelta(days=8)
    data = {
        'location': user_location,
        'distance': user.distance_in_km(),
        'distance_units': 'km',
        'start': start_time,
        'end': end_time,
    }
    form = search_base.SearchForm(data=data)

    geocode = None
    distance = None
    if form.location.data:
        try:
            geocode, distance = search_base.get_geocode_with_distance(form)
        except Exception as e:
            raise NoEmailException('Could not normalize user location: %s: %s',
                                   data, e)

    try:
        search_query = form.build_query(start_end_query=True)
    except:
        logging.error('Error looking up user location for user %s, form: %s',
                      user.fb_uid, form)
        raise
    search_results = search.Search(search_query).get_search_results()
    # Don't send email...
    if not search_results:
        raise NoEmailException('No search results for user')

    need_full_event = False
    json_search_response = api_format.build_search_results_api(
        form,
        search_query,
        search_results, (2, 0),
        need_full_event,
        geocode,
        distance,
        skip_people=True)
    locale = user.locale or 'en_US'
    email_unsubscribe_url = 'https://www.dancedeets.com/user/unsubscribe?email=%s' % urllib.quote(
        email_address)
    props = {
        'user': {
            'userName': user.first_name or user.full_name or '',
            'city': user.city_name,
            'countryName': names.get_country_name(user.location_country),
        },
        'response': json_search_response,
        'currentLocale': locale.replace('_', '-'),
        'mobileIosUrl': mobile.IOS_URL,
        'mobileAndroidUrl': mobile.ANDROID_URL,
        'emailPreferencesUrl': email_unsubscribe_url,
    }
    email_template = 'weeklyMail.js'
    response = render_server.render_jsx(email_template,
                                        props,
                                        static_html=True)
    if response.error:
        message = 'Error rendering weeklyMail.js: %s' % response.error
        logging.error(message)
        raise NoEmailException(message)
    mjml_response = render_server.render_mjml(response.markup)
    rendered_html = mjml_response['html']
    if mjml_response.get('errors'):
        message = 'Errors rendering weeklyMail.mjml: %s', mjml_response[
            'errors']
        logging.error(message)
        raise NoEmailException(message)
    messages = [
        'Your Week in Dance: %s',
        'DanceDeets Weekly: %s',
        'Dance Events for %s',
    ]
    message = random.choice(messages)

    tag = re.sub(r'[^a-z]', '-', message.lower())[:50]
    tags = [
        'weekly',
        tag,
    ]

    subject = message % d.strftime('%b %d, %Y')
    message = {
        'from_email':
        '*****@*****.**',
        'from_name':
        'DanceDeets Events',
        'subject':
        subject,
        'to': [{
            'email': email_address,
            'name': user.full_name or user.first_name or '',
            'type': 'to',
        }],
        'html':
        rendered_html,
        'metadata': {
            'user_id': user.fb_uid,
            'email_type': 'weekly',
        },
        'tags':
        tags,
    }
    if should_send:
        logging.info('Sending weekly mail for user %s (%s)', user.fb_uid,
                     user.full_name)
        # Update the last-sent-time here, so we any retryable errors don't cause emails to be multi-sent
        user = users.User.get_by_id(user.fb_uid)
        user.weekly_email_send_date = datetime.datetime.now()
        user.put()
        # And send the message now.
        mandrill_api.send_message(message)
    return message
def email_for_user(user, fbl, should_send=True):
    if not user.send_email:
        raise NoEmailException('User has send_email==False')
    email_address = user.email
    if not email_address:
        raise NoEmailException('User does not have an email')

    if user.weekly_email_send_date and user.weekly_email_send_date > datetime.datetime.now() - datetime.timedelta(days=3):
        message = "Skipping user %s (%s) because last weekly email was sent on %s" % (
            user.fb_uid, user.full_name, user.weekly_email_send_date
        )
        logging.warning(message)
        # Sent the weekly email too recently, let's abort
        raise NoEmailException(message)
    fb_user = fbl.fetched_data(fb_api.LookupUser, fbl.fb_uid)
    if not 'profile' in fb_user:
        raise NoEmailException('Could not find LookupUser: %s', fb_user)

    user_location = user.location
    if not user_location:
        raise NoEmailException('User does not have location')

    d = datetime.date.today()
    start_time = d - datetime.timedelta(days=d.weekday())  # round down to last monday
    end_time = start_time + datetime.timedelta(days=8)
    data = {
        'location': user_location,
        'distance': user.distance_in_km(),
        'distance_units': 'km',
        'start': start_time,
        'end': end_time,
    }
    form = search_base.SearchForm(data=data)

    geocode = None
    distance = None
    if form.location.data:
        try:
            geocode, distance = search_base.get_geocode_with_distance(form)
        except Exception as e:
            raise NoEmailException('Could not normalize user location: %s: %s', data, e)

    try:
        search_query = form.build_query(start_end_query=True)
    except:
        logging.error('Error looking up user location for user %s, form: %s', user.fb_uid, form)
        raise
    search_results = search.Search(search_query).get_search_results()
    # Don't send email...
    if not search_results:
        raise NoEmailException('No search results for user')

    need_full_event = False
    json_search_response = api_format.build_search_results_api(
        form, search_query, search_results, (2, 0), need_full_event, geocode, distance, skip_people=True
    )
    locale = user.locale or 'en_US'
    email_unsubscribe_url = 'https://www.dancedeets.com/user/unsubscribe?email=%s' % urllib.quote(email_address)
    props = {
        'user': {
            'userName': user.first_name or user.full_name or '',
            'city': user.city_name,
            'countryName': names.get_country_name(user.location_country),
        },
        'response': json_search_response,
        'currentLocale': locale.replace('_', '-'),
        'mobileIosUrl': mobile.IOS_URL,
        'mobileAndroidUrl': mobile.ANDROID_URL,
        'emailPreferencesUrl': email_unsubscribe_url,
    }
    email_template = 'weeklyMail.js'
    response = render_server.render_jsx(email_template, props, static_html=True)
    if response.error:
        message = 'Error rendering weeklyMail.js: %s' % response.error
        logging.error(message)
        raise NoEmailException(message)
    mjml_response = render_server.render_mjml(response.markup)
    rendered_html = mjml_response['html']
    if mjml_response.get('errors'):
        message = 'Errors rendering weeklyMail.mjml: %s', mjml_response['errors']
        logging.error(message)
        raise NoEmailException(message)
    messages = [
        'Your Week in Dance: %s',
        'DanceDeets Weekly: %s',
        'Dance Events for %s',
    ]
    message = random.choice(messages)

    tag = re.sub(r'[^a-z]', '-', message.lower())[:50]
    tags = [
        'weekly',
        tag,
    ]

    subject = message % d.strftime('%b %d, %Y')
    message = {
        'from_email': '*****@*****.**',
        'from_name': 'DanceDeets Events',
        'subject': subject,
        'to': [{
            'email': email_address,
            'name': user.full_name or user.first_name or '',
            'type': 'to',
        }],
        'html': rendered_html,
        'metadata': {
            'user_id': user.fb_uid,
            'email_type': 'weekly',
        },
        'tags': tags,
    }
    if should_send:
        logging.info('Sending weekly mail for user %s (%s)', user.fb_uid, user.full_name)
        # Update the last-sent-time here, so we any retryable errors don't cause emails to be multi-sent
        user = users.User.get_by_id(user.fb_uid)
        user.weekly_email_send_date = datetime.datetime.now()
        user.put()
        # And send the message now.
        mandrill_api.send_message(message)
    return message
def email_for_user(user, fbl, should_send=False):
    if not user.send_email:
        raise NoEmailException('User has send_email==False')
    email_address = user.email
    if not email_address:
        raise NoEmailException('User does not have an email')

    fb_user = fbl.fetched_data(fb_api.LookupUser, fbl.fb_uid)
    if not 'profile' in fb_user:
        raise NoEmailException('Could not find LookupUser: %s', fb_user)

    locale = user.locale or 'en_US'
    email_unsubscribe_url = 'https://www.dancedeets.com/user/unsubscribe?email=%s' % urllib.quote(email_address)
    props = {
        'user': {
            'userName': user.first_name or user.full_name or '',
            'city': user.city_name,
            'countryName': names.get_country_name(user.location_country),
        },
        'currentLocale': locale.replace('_', '-'),
        'mobileIosUrl': mobile.IOS_URL,
        'mobileAndroidUrl': mobile.ANDROID_URL,
        'emailPreferencesUrl': email_unsubscribe_url,
    }
    email_template = 'mailNewUser.js'
    response = render_server.render_jsx(email_template, props, static_html=True)
    if response.error:
        message = 'Error rendering mailNewUser.js: %s' % response.error
        logging.error(message)
        raise NoEmailException(message)
    mjml_response = render_server.render_mjml(response.markup)
    rendered_html = mjml_response['html']
    if mjml_response.get('errors'):
        message = 'Errors rendering mailNewUser.mjml: %s', mjml_response['errors']
        logging.error(message)
        raise NoEmailException(message)

    subject = u'Welcome to DanceDeets! See you on the dance floor…'

    tags = [
        'new-user',
    ]

    message = {
        'from_email': '*****@*****.**',
        'from_name': 'DanceDeets Events',
        'subject': subject,
        'to': [{
            'email': email_address,
            'name': user.full_name or user.first_name or '',
            'type': 'to',
        }],
        'bcc_address': '*****@*****.**',
        'html': rendered_html,
        'metadata': {
            'user_id': user.fb_uid,
            'email_type': 'weekly',
        },
        'tags': tags,
    }
    if should_send:
        logging.info('Sending new-user email for user %s (%s)', user.fb_uid, user.full_name)
        # And send the message now.
        mandrill_api.send_message(message)
    return message
def email_for_user(user, fbl, should_send=False):
    if not user.send_email:
        raise NoEmailException('User has send_email==False')
    email_address = user.email
    if not email_address:
        raise NoEmailException('User does not have an email')

    fb_user = fbl.fetched_data(fb_api.LookupUser, fbl.fb_uid)
    if not 'profile' in fb_user:
        raise NoEmailException('Could not find LookupUser: %s', fb_user)

    locale = user.locale or 'en_US'
    email_unsubscribe_url = 'https://www.dancedeets.com/user/unsubscribe?email=%s' % urllib.quote(
        email_address)
    props = {
        'user': {
            'userName': user.first_name or user.full_name or '',
            'city': user.city_name,
            'countryName': names.get_country_name(user.location_country),
        },
        'currentLocale': locale.replace('_', '-'),
        'mobileIosUrl': mobile.IOS_URL,
        'mobileAndroidUrl': mobile.ANDROID_URL,
        'emailPreferencesUrl': email_unsubscribe_url,
    }
    email_template = 'mailNewUser.js'
    response = render_server.render_jsx(email_template,
                                        props,
                                        static_html=True)
    if response.error:
        message = 'Error rendering mailNewUser.js: %s' % response.error
        logging.error(message)
        raise NoEmailException(message)
    mjml_response = render_server.render_mjml(response.markup)
    rendered_html = mjml_response['html']
    if mjml_response.get('errors'):
        message = 'Errors rendering mailNewUser.mjml: %s', mjml_response[
            'errors']
        logging.error(message)
        raise NoEmailException(message)

    subject = u'Welcome to DanceDeets! See you on the dance floor…'

    tags = [
        'new-user',
    ]

    message = {
        'from_email':
        '*****@*****.**',
        'from_name':
        'DanceDeets Events',
        'subject':
        subject,
        'to': [{
            'email': email_address,
            'name': user.full_name or user.first_name or '',
            'type': 'to',
        }],
        'bcc_address':
        '*****@*****.**',
        'html':
        rendered_html,
        'metadata': {
            'user_id': user.fb_uid,
            'email_type': 'weekly',
        },
        'tags':
        tags,
    }
    if should_send:
        logging.info('Sending new-user email for user %s (%s)', user.fb_uid,
                     user.full_name)
        # And send the message now.
        mandrill_api.send_message(message)
    return message