Example #1
0
def single_photo_location(albumid, photoid):
    photo = models.get_photo(photoid)
    if request.method == "GET":
        location = None
        if photo.location:
            location = photo.location.json()
        return jsonify({"location": location})

    elif request.method == "POST":
        if 'location_id' in request.json:
            location = models.get_location(int(request.json['location_id']))
            photo.location = location
            s.add(photo)
            s.commit()
        else:
            location = models.Location(name=request.json['name'],
                                       description=request.json['description'])
            photo.location = location
            s.add(location)
            s.add(photo)
            s.commit()
        return jsonify({"location": photo.location.id})

    elif request.method == "DELETE":
        s.delete(photo.location)
        s.commit()
        return jsonify({"location": None})
Example #2
0
 def get(self, location_id):
     locationObject = models.get_location(location_id)
     if locationObject is None:
         return {
             'status': 404,
             'message': 'Location with id={} not found.'.format(location_id)
         }
     return locationObject, 200
Example #3
0
    def validate_work_location(self, data):
        if not data.get('work_city_name') or not data.get('work_city_uf'):
            return  # no action required
        try:
            work_location = get_location(data['work_city_name'],
                                         data['work_city_uf'])
        except LocationError:
            raise SerializerError(
                "Could not find the geolocation for the work address")

        return work_location.id
Example #4
0
def unsubscribe(request, location_slug):
	''' unsubscribe route '''
	# fail gracefully if location does not exist
	try:
		location = get_location(location_slug)
	except:
		# XXX TODO reject and bounce back to sender?
		return HttpResponse(status=200)
	logger.debug('unsubscribe@ for location: %s' % location)
	logger.debug(request.POST)
	logger.debug(request.FILES)
	return HttpResponse(status=200)
Example #5
0
    def validate_home_location(self, data):
        try:
            home_location = get_location(data['home_city_name'],
                                         data['home_city_uf'])
        except KeyError:
            raise ValidationError(
                "Home city and uf are required for user registration")
        except LocationError:
            raise ValidationError(
                "Could not find the geolocation for the home address")

        return home_location.id
Example #6
0
def unsubscribe(request, location_slug):
    ''' unsubscribe route '''
    # fail gracefully if location does not exist
    try:
        location = get_location(location_slug)
    except:
        # XXX TODO reject and bounce back to sender?
        return HttpResponse(status=200)
    logger.debug('unsubscribe@ for location: %s' % location)
    logger.debug(request.POST)
    logger.debug(request.FILES)
    return HttpResponse(status=200)
Example #7
0
def announce(request, location_slug):
    ''' email all people signed up for event activity notifications at this location.'''

    # fail gracefully if location does not exist
    try:
        location = get_location(location_slug)
    except:
        # XXX TODO reject and bounce back to sender?
        logger.error('location not found')
        return HttpResponse(status=200)
    logger.debug('announce@ for location: %s' % location)

    # Make sure this person can post to our list
    sender = request.POST.get('from')
    location_event_admins = EventAdminGroup.objects.get(
        location=location).users.all()
    allowed_senders = [user.email for user in location_event_admins]
    # have to be careful here because the "sender" email address is likely a
    # substring of the entire 'from' field in the POST request, ie, "Jessy Kate
    # Schingler <*****@*****.**>"
    this_sender_allowed = False
    for sender_email in allowed_senders:
        if sender_email in sender:
            this_sender_allowed = True
            break
    if not this_sender_allowed:
        # TODO - This could send a response so they know they were blocked
        logger.warn("Sender (%s) not allowed.  Exiting quietly." % sender)
        return HttpResponse(status=200)

    weekly_notifications_on = EventNotifications.objects.filter(
        location_weekly=location)
    remindees_for_location = [
        notify.user for notify in weekly_notifications_on
    ]

    # TESTING
    jessy = User.objects.get(id=1)
    for user in [
            jessy,
    ]:
        #for user in remindees_for_location:
        send_announce(request, user, location)

    return HttpResponse(status=200)
Example #8
0
def announce(request, location_slug):
	''' email all people signed up for event activity notifications at this location.'''

	# fail gracefully if location does not exist
	try:
		location = get_location(location_slug)
	except:
		# XXX TODO reject and bounce back to sender?
		logger.error('location not found')
		return HttpResponse(status=200)
	logger.debug('announce@ for location: %s' % location)

	# Make sure this person can post to our list
	sender = request.POST.get('from')
	location_event_admins = EventAdminGroup.objects.get(location=location).users.all()
	allowed_senders = [user.email for user in location_event_admins]
	# have to be careful here because the "sender" email address is likely a
	# substring of the entire 'from' field in the POST request, ie, "Jessy Kate
	# Schingler <*****@*****.**>"
	this_sender_allowed = False
	for sender_email in allowed_senders:
		if sender_email in sender:
			this_sender_allowed = True
			break
	if not this_sender_allowed:
		# TODO - This could send a response so they know they were blocked
		logger.warn("Sender (%s) not allowed.  Exiting quietly." % sender)
		return HttpResponse(status=200)

	weekly_notifications_on = EventNotifications.objects.filter(location_weekly = location)
	remindees_for_location = [notify.user for notify in weekly_notifications_on]
	
	# TESTING
	jessy = User.objects.get(id=1)
	for user in [jessy,]:
	#for user in remindees_for_location:
		send_announce(request, user, location)

	return HttpResponse(status=200)
Example #9
0
def single_location(locationid):
    location = models.get_location(locationid)
    if request.method == "GET":
        return jsonify(location.json())
    elif request.method == "PUT":
        try:
            if 'name' in request.json:
                location.name = request.json['name']
            if 'description' in request.json:
                location.description = request.json['description']
            s.add(location)
            s.commit()
            return jsonify({"status": True})
        except Exception as e:
            return jsonfiy({"status": False,
                            "message": str(e)})
    elif request.method == "DELETE":
        try:
            s.delete(location)
            s.commit()
            return jsonify({"status": True})
        except Exception as e:
            return jsonify({"status": True,
                            "message": str(e)})
Example #10
0
def residents(request, location_slug):
	''' email all residents at this location.'''

	# fail gracefully if location does not exist
	try:
		location = get_location(location_slug)
	except:
		# XXX TODO reject and bounce back to sender?
		logger.error('location not found')
		return HttpResponse(status=200)
	logger.debug('residents@ for location: %s' % location)

	# we think that message_headers is a list of strings
	header_txt = request.POST.get('message-headers')
	message_headers = json.loads(header_txt)
	message_header_keys = [item[0] for item in message_headers]

	# make sure this isn't an email we have already forwarded (cf. emailbombgate 2014)
	# A List-Id header will only be present if it has been added manually in
	# this function, ie, if we have already processed this message. 
	if request.POST.get('List-Id') or 'List-Id' in message_header_keys:
		# mailgun requires a code 200 or it will continue to retry delivery
		logger.debug('List-Id header was found! Dropping message silently')
		return HttpResponse(status=200)

	#if 'Auto-Submitted' in message_headers or message_headers['Auto-Submitted'] != 'no':
	if 'Auto-Submitted' in message_header_keys: 
		logger.info('message appears to be auto-submitted. reject silently')
		return HttpResponse(status=200)

	recipient = request.POST.get('recipient')
	from_address = request.POST.get('from')
	logger.debug('from: %s' % from_address)
	sender = request.POST.get('sender')
	logger.debug('sender: %s' % sender)
	subject = request.POST.get('subject')
	body_plain = request.POST.get('body-plain')
	body_html = request.POST.get('body-html')

	# Add all the residents at this location
	resident_emails = [] 
	for r in location.residents.all():
		resident_emails.append(r.email)

	# Now loop through all the emails and build the bcc list we will use.
	# This makes sure there are no duplicate emails.
	bcc_list = []
	for email in resident_emails:
		if email not in bcc_list:
			bcc_list.append(email)
	logger.debug("bcc list: %s" % bcc_list)
	
	# Make sure this person can post to our list
	#if not sender in bcc_list:
	#	# TODO - This shoud possibly send a response so they know they were blocked
	#	logger.warn("Sender (%s) not allowed.  Exiting quietly." % sender)
	#	return HttpResponse(status=200)
	if sender in bcc_list:
		bcc_list.remove(sender)
	
	# pass through attachments
	logger.debug(request)
	logger.debug(request.FILES)
	for attachment in request.FILES.values():
		a_file = default_storage.save('/tmp/'+attachment.name, ContentFile(attachment.read()))
	attachments = {}
	num = 0
	for attachment in request.FILES.values():
		attachments["attachment[%d]"] = (attachment.name, open('/tmp/'+attachment.name, 'rb'))
		num+= 1

	# prefix subject, but only if the prefix string isn't already in the
	# subject line (such as a reply)
	if subject.find(location.email_subject_prefix) < 0:
		prefix = "["+location.email_subject_prefix + "] " 
		subject = prefix + subject
	logger.debug("subject: %s" % subject)

	# add in footer
	text_footer = '''\n\n-------------------------------------------\n*~*~*~* %s residents email list *~*~*~* '''% location.name
	body_plain = body_plain + text_footer

	if body_html:
		html_footer = '''<br><br>-------------------------------------------<br>*~*~*~* %s residents email list *~*~*~* '''% location.name
		body_html = body_html + html_footer

	# send the message 
	list_address = "residents@%s.%s" % (location.slug, settings.LIST_DOMAIN)
	mailgun_data =  {"from": from_address,
		"to": [recipient, ],
		"bcc": bcc_list,
		"subject": subject,
		"text": body_plain,
		"html": body_html,
		# attach some headers: LIST-ID, REPLY-TO, MSG-ID, precedence...
		# Precedence: list - helps some out of office auto responders know not to send their auto-replies. 
		"h:List-Id": list_address,
		"h:Precedence": "list",
		# Reply-To: list email apparently has some religious debates
		# (http://www.gnu.org/software/mailman/mailman-admin/node11.html) but seems
		# to be common these days 
		"h:Reply-To": list_address,
	}
	return mailgun_send(mailgun_data, attachments)
Example #11
0
def residents(request, location_slug):
    ''' email all residents at this location.'''

    # fail gracefully if location does not exist
    try:
        location = get_location(location_slug)
    except:
        # XXX TODO reject and bounce back to sender?
        logger.error('location not found')
        return HttpResponse(status=200)
    logger.debug('residents@ for location: %s' % location)

    # we think that message_headers is a list of strings
    header_txt = request.POST.get('message-headers')
    message_headers = json.loads(header_txt)
    message_header_keys = [item[0] for item in message_headers]

    # make sure this isn't an email we have already forwarded (cf. emailbombgate 2014)
    # A List-Id header will only be present if it has been added manually in
    # this function, ie, if we have already processed this message.
    if request.POST.get('List-Id') or 'List-Id' in message_header_keys:
        # mailgun requires a code 200 or it will continue to retry delivery
        logger.debug('List-Id header was found! Dropping message silently')
        return HttpResponse(status=200)

    #if 'Auto-Submitted' in message_headers or message_headers['Auto-Submitted'] != 'no':
    if 'Auto-Submitted' in message_header_keys:
        logger.info('message appears to be auto-submitted. reject silently')
        return HttpResponse(status=200)

    recipient = request.POST.get('recipient')
    from_address = request.POST.get('from')
    logger.debug('from: %s' % from_address)
    sender = request.POST.get('sender')
    logger.debug('sender: %s' % sender)
    subject = request.POST.get('subject')
    body_plain = request.POST.get('body-plain')
    body_html = request.POST.get('body-html')

    # Add all the residents at this location
    resident_emails = []
    for r in location.residents():
        resident_emails.append(r.email)

    # Now loop through all the emails and build the bcc list we will use.
    # This makes sure there are no duplicate emails.
    bcc_list = []
    for email in resident_emails:
        if email not in bcc_list:
            bcc_list.append(email)
    logger.debug("bcc list: %s" % bcc_list)

    # Make sure this person can post to our list
    #if not sender in bcc_list:
    #    # TODO - This shoud possibly send a response so they know they were blocked
    #    logger.warn("Sender (%s) not allowed.  Exiting quietly." % sender)
    #    return HttpResponse(status=200)
    if sender in bcc_list:
        bcc_list.remove(sender)

    # Include attachements
    attachments = []
    for attachment in request.FILES.values():
        attachments.append(("attachment", attachment))

    # prefix subject, but only if the prefix string isn't already in the
    # subject line (such as a reply)
    if subject.find(location.email_subject_prefix) < 0:
        prefix = "[" + location.email_subject_prefix + "] "
        subject = prefix + subject
    logger.debug("subject: %s" % subject)

    # add in footer
    text_footer = '''\n\n-------------------------------------------\n*~*~*~* %s residents email list *~*~*~* ''' % location.name
    body_plain = body_plain + text_footer

    if body_html:
        html_footer = '''<br><br>-------------------------------------------<br>*~*~*~* %s residents email list *~*~*~* ''' % location.name
        body_html = body_html + html_footer

    # send the message
    list_address = "residents@%s.%s" % (location.slug, settings.LIST_DOMAIN)
    mailgun_data = {
        "from": from_address,
        "to": [
            recipient,
        ],
        "bcc": bcc_list,
        "subject": subject,
        "text": body_plain,
        "html": body_html,
        # attach some headers: LIST-ID, REPLY-TO, MSG-ID, precedence...
        # Precedence: list - helps some out of office auto responders know not to send their auto-replies.
        "h:List-Id": list_address,
        "h:Precedence": "list",
        # Reply-To: list email apparently has some religious debates
        # (http://www.gnu.org/software/mailman/mailman-admin/node11.html) but seems
        # to be common these days
        "h:Reply-To": list_address,
    }
    return mailgun_send(mailgun_data, attachments)
Example #12
0
def test80085(request, location_slug):
    ''' test route '''
    # fail gracefully if location does not exist
    try:
        location = get_location(location_slug)
    except:
        # XXX TODO reject and bounce back to sender?
        return HttpResponse(status=200)
    logger.debug('test80085@ for location: %s' % location)
    logger.debug(request.POST)
    logger.debug(request.FILES)

    # we think that message_headers is a list of strings
    header_txt = request.POST.get('message-headers')
    message_headers = json.loads(header_txt)
    message_header_keys = [item[0] for item in message_headers]

    # make sure this isn't an email we have already forwarded (cf. emailbombgate 2014)
    # A List-Id header will only be present if it has been added manually in
    # this function, ie, if we have already processed this message.
    if request.POST.get('List-Id') or 'List-Id' in message_header_keys:
        # mailgun requires a code 200 or it will continue to retry delivery
        logger.debug('List-Id header was found! Dropping message silently')
        return HttpResponse(status=200)

    #if 'Auto-Submitted' in message_headers or message_headers['Auto-Submitted'] != 'no':
    if 'Auto-Submitted' in message_header_keys:
        logger.info('message appears to be auto-submitted. reject silently')
        return HttpResponse(status=200)

    recipient = request.POST.get('recipient')
    to = request.POST.get('To')
    from_address = request.POST.get('from')
    logger.debug('from: %s' % from_address)
    sender = request.POST.get('sender')
    logger.debug('sender: %s' % sender)
    subject = request.POST.get('subject')
    body_plain = request.POST.get('body-plain')
    body_html = request.POST.get('body-html')

    # retrieve the current house admins for this location
    bcc_list = ['*****@*****.**', '*****@*****.**']
    logger.debug("bcc list: %s" % bcc_list)

    # Make sure this person can post to our list
    #if not sender in bcc_list:
    #    # TODO - This shoud possibly send a response so they know they were blocked
    #    logger.warn("Sender (%s) not allowed.  Exiting quietly." % sender)
    #    return HttpResponse(status=200)

    # usually we would remove the sender from receiving the email but because
    # we're testing, let 'em have it.
    #if sender in bcc_list:
    #    bcc_list.remove(sender)

    # pass through attachments
    # logger.debug(request)
    # logger.debug(request.FILES)
    # for attachment in request.FILES.values():
    #     # JKS NOTE! this does NOT work with unicode-encoded data. i'm not
    #     # actually sure that we should *expect* to receive unicode-encoded
    #     # attachments, but it definitely breaks (which i disocvered because
    #     # mailgun sends its test POST with a unicode-encoded attachment).
    #     a_file = default_storage.save(attachment.name, ContentFile(attachment.read()))
    # attachments = {}
    # num = 0
    # for attachment in request.FILES.values():
    #     attachments["attachment-%d" % num] = (attachment.name, default_storage.open(attachment.name, 'rb').read())
    #     #default_storage.delete(attachment.name)
    #     num+= 1

    attachments = []
    for attachment in request.FILES.values():
        attachments.append(("inline", attachment))

    # prefix subject, but only if the prefix string isn't already in the
    # subject line (such as a reply)
    if subject.find('EN Test') < 0:
        prefix = "[EN Test!] "
        subject = prefix + subject
    logger.debug("subject: %s" % subject)

    # add in footer
    text_footer = '''\n\n-------------------------------------------\nYou are receiving this email because someone at Embassy Network wanted to use you as a guinea pig. %mailing_list_unsubscribe_url%'''
    body_plain = body_plain + text_footer
    if body_html:
        html_footer = '''<br><br>-------------------------------------------<br>You are receiving this email because someone at Embassy Network wanted to use you as a guinea pig.'''
        body_html = body_html + html_footer

    # send the message
    list_address = "test80085@" + location.slug + ".mail.embassynetwork.com"
    mailgun_data = {
        "from": from_address,
        "to": [
            recipient,
        ],
        "bcc": bcc_list,
        "subject": subject,
        "text": body_plain,
        "html": body_html,
        # attach some headers: LIST-ID, REPLY-TO, MSG-ID, precedence...
        # Precedence: list - helps some out of office auto responders know not to send their auto-replies.
        "h:List-Id": list_address,
        "h:Precedence": "list",
        # Reply-To: list email apparently has some religious debates
        # (http://www.gnu.org/software/mailman/mailman-admin/node11.html) but seems
        # to be common these days
        "h:Reply-To": from_address
    }
    return mailgun_send(mailgun_data, attachments)
Example #13
0
def stay(request, location_slug):
	''' email all admins at this location.'''
	# fail gracefully if location does not exist
	try:
		location = get_location(location_slug)
	except:
		# XXX TODO reject and bounce back to sender?
		return HttpResponse(status=200)
	logger.debug('stay@ for location: %s' % location)
	logger.debug(request.POST)

	# we think that message_headers is a list of strings
	header_txt = request.POST.get('message-headers')
	message_headers = json.loads(header_txt)
	message_header_keys = [item[0] for item in message_headers]

	# make sure this isn't an email we have already forwarded (cf. emailbombgate 2014)
	# A List-Id header will only be present if it has been added manually in
	# this function, ie, if we have already processed this message. 
	if request.POST.get('List-Id') or 'List-Id' in message_header_keys:
		# mailgun requires a code 200 or it will continue to retry delivery
		logger.debug('List-Id header was found! Dropping message silently')
		return HttpResponse(status=200)

	#if 'Auto-Submitted' in message_headers or message_headers['Auto-Submitted'] != 'no':
	if 'Auto-Submitted' in message_header_keys: 
		logger.info('message appears to be auto-submitted. reject silently')
		return HttpResponse(status=200)

	recipient = request.POST.get('recipient')
	to = request.POST.get('To')
	from_address = request.POST.get('from')
	logger.debug('from: %s' % from_address)
	sender = request.POST.get('sender')
	logger.debug('sender: %s' % sender)	
	subject = request.POST.get('subject')
	body_plain = request.POST.get('body-plain')
	body_html = request.POST.get('body-html')

	# retrieve the current house admins for this location
	location_admins = location.house_admins.all()
	bcc_list = []
	for person in location_admins:
		if person.email not in bcc_list:
			bcc_list.append(person.email)
	logger.debug("bcc list: %s" % bcc_list)

	# Make sure this person can post to our list
	#if not sender in bcc_list:
	#	# TODO - This shoud possibly send a response so they know they were blocked
	#	logger.warn("Sender (%s) not allowed.  Exiting quietly." % sender)
	#	return HttpResponse(status=200)
	if sender in bcc_list:
		bcc_list.remove(sender)

	# Include attachements
	attachments = []
	for attachment in request.FILES.values():
		attachments.append(("attachment", attachment))

	# prefix subject, but only if the prefix string isn't already in the
	# subject line (such as a reply)
	if subject.find(location.email_subject_prefix) < 0:
		prefix = "["+location.email_subject_prefix + "] [Admin] " 
		subject = prefix + subject
	logger.debug("subject: %s" % subject)

	# add in footer
	text_footer = '''\n\n-------------------------------------------\nYou are receving email to %s because you are a location admin at %s. Send mail to this list to reach other admins.''' % (recipient, location.name)
	body_plain = body_plain + text_footer
	if body_html:
		html_footer = '''<br><br>-------------------------------------------<br>You are receving email to %s because you are a location admin at %s. Send mail to this list to reach other admins.''' % (recipient, location.name)
		body_html = body_html + html_footer

	# send the message 
	list_address = location.from_email()
	mailgun_data = {"from": from_address,
			"to": [recipient, ],
			"bcc": bcc_list,
			"subject": subject,
			"text": body_plain,
			"html": body_html,
			# attach some headers: LIST-ID, REPLY-TO, MSG-ID, precedence...
			# Precedence: list - helps some out of office auto responders know not to send their auto-replies. 
			"h:List-Id": list_address,
			"h:Precedence": "list",
			# Reply-To: list email apparently has some religious debates
			# (http://www.gnu.org/software/mailman/mailman-admin/node11.html) but seems
			# to be common these days 
			"h:Reply-To": from_address
		}
	return mailgun_send(mailgun_data, attachments)
Example #14
0
def test80085(request, location_slug):
	''' test route '''
	# fail gracefully if location does not exist
	try:
		location = get_location(location_slug)
	except:
		# XXX TODO reject and bounce back to sender?
		return HttpResponse(status=200)
	logger.debug('test80085@ for location: %s' % location)
	logger.debug(request.POST)
	logger.debug(request.FILES)

	# we think that message_headers is a list of strings
	header_txt = request.POST.get('message-headers')
	message_headers = json.loads(header_txt)
	message_header_keys = [item[0] for item in message_headers]

	# make sure this isn't an email we have already forwarded (cf. emailbombgate 2014)
	# A List-Id header will only be present if it has been added manually in
	# this function, ie, if we have already processed this message. 
	if request.POST.get('List-Id') or 'List-Id' in message_header_keys:
		# mailgun requires a code 200 or it will continue to retry delivery
		logger.debug('List-Id header was found! Dropping message silently')
		return HttpResponse(status=200)

	#if 'Auto-Submitted' in message_headers or message_headers['Auto-Submitted'] != 'no':
	if 'Auto-Submitted' in message_header_keys: 
		logger.info('message appears to be auto-submitted. reject silently')
		return HttpResponse(status=200)

	recipient = request.POST.get('recipient')
	to = request.POST.get('To')
	from_address = request.POST.get('from')
	logger.debug('from: %s' % from_address)
	sender = request.POST.get('sender')
	logger.debug('sender: %s' % sender)	
	subject = request.POST.get('subject')
	body_plain = request.POST.get('body-plain')
	body_html = request.POST.get('body-html')

	# retrieve the current house admins for this location
	bcc_list = ['*****@*****.**', '*****@*****.**']
	logger.debug("bcc list: %s" % bcc_list)

	# Make sure this person can post to our list
	#if not sender in bcc_list:
	#	# TODO - This shoud possibly send a response so they know they were blocked
	#	logger.warn("Sender (%s) not allowed.  Exiting quietly." % sender)
	#	return HttpResponse(status=200)
	
	# usually we would remove the sender from receiving the email but because
	# we're testing, let 'em have it.  
	#if sender in bcc_list:
	#	bcc_list.remove(sender)

	# pass through attachments
	# logger.debug(request)
	# logger.debug(request.FILES)
	# for attachment in request.FILES.values():
	# 	# JKS NOTE! this does NOT work with unicode-encoded data. i'm not
	# 	# actually sure that we should *expect* to receive unicode-encoded
	# 	# attachments, but it definitely breaks (which i disocvered because
	# 	# mailgun sends its test POST with a unicode-encoded attachment). 
	# 	a_file = default_storage.save(attachment.name, ContentFile(attachment.read()))
	# attachments = {}
	# num = 0
	# for attachment in request.FILES.values():
	# 	attachments["attachment-%d" % num] = (attachment.name, default_storage.open(attachment.name, 'rb').read())
	# 	#default_storage.delete(attachment.name)
	# 	num+= 1

	attachments = []
	for attachment in request.FILES.values():
		attachments.append(("inline", attachment))

	# prefix subject, but only if the prefix string isn't already in the
	# subject line (such as a reply)
	if subject.find('EN Test') < 0:
		prefix = "[EN Test!] "
		subject = prefix + subject
	logger.debug("subject: %s" % subject)

	# add in footer
	text_footer = '''\n\n-------------------------------------------\nYou are receiving this email because someone at Embassy Network wanted to use you as a guinea pig.'''
	body_plain = body_plain + text_footer
	if body_html:
		html_footer = '''<br><br>-------------------------------------------<br>You are receiving this email because someone at Embassy Network wanted to use you as a guinea pig.''' 
		body_html = body_html + html_footer

	# send the message 
	list_address = "test80085@"+location.slug+".mail.embassynetwork.com"
	mailgun_data = {"from": from_address,
			"to": [recipient, ],
			"bcc": bcc_list,
			"subject": subject,
			"text": body_plain,
			"html": body_html,
			# attach some headers: LIST-ID, REPLY-TO, MSG-ID, precedence...
			# Precedence: list - helps some out of office auto responders know not to send their auto-replies. 
			"h:List-Id": list_address,
			"h:Precedence": "list",
			# Reply-To: list email apparently has some religious debates
			# (http://www.gnu.org/software/mailman/mailman-admin/node11.html) but seems
			# to be common these days 
			"h:Reply-To": from_address
		}
	return mailgun_send(mailgun_data, attachments)
Example #15
0
def location(poke_id):
    res=models.get_location(poke_id)
    url=f'https://www.google.com/maps/place/"{res["town"]}","{res["street"]}"'
    webbrowser.open_new(url)
    return "Server is up and running smoothly"
Example #16
0
def controller(env):
    '''Main controller for the application'''

    if env.get('REQUEST_METHOD') == 'GET':

        # get the template
        template = get_template()

        # Default search location
        locations = db.get_locations()
        location_html = locations_to_html(locations)
        name = 'New York'
        lon = 73.935242
        lat = 40.730610

        # get raw json
        my_json = get_api(lon, lat)

        # convert json to html str
        html_images = json_to_html(my_json)

        return render(
            template, {
                'photos': html_images,
                'lon': lon,
                'lat': lat,
                'name': name,
                'options': location_html
            })

    elif env.get('REQUEST_METHOD') == 'POST':

        # get POST Data
        try:
            r_body_size = int(env.get('CONTENT_LENGTH', 0))
        except (ValueError):
            r_body_size = 0

        # get template
        template = get_template()

        d = env['wsgi.input'].read(r_body_size)
        data = parse_qs(d)
        lon = float(data.get(b'lon', [])[0])
        lat = float(data.get(b'lat', [])[0])
        try:
            name = (data.get(b'location-name', [])[0]).decode('utf-8')
        except:
            name = 'Mystery location'

        if b'pre-location-search' in data:
            loc_id = int(data.get(b'location-select', [])[0])
            print(loc_id)
            loc = db.get_location(loc_id)
            print(loc)
            name = loc[0][1]
            lon = loc[0][2]
            lat = loc[0][3]

        if b'location-save' in data:
            db.save_location(name, lon, lat)

        locations = db.get_locations()
        location_html = locations_to_html(locations)
        # get json data
        my_json = get_api(lon, lat)

        html_images = json_to_html(my_json)

        return render(
            template, {
                'photos': html_images,
                'lon': lon,
                'lat': lat,
                'name': name,
                'options': location_html
            })
Example #17
0
def stay(request, location_slug):
    ''' email all admins at this location.'''
    # fail gracefully if location does not exist
    try:
        location = get_location(location_slug)
    except:
        # XXX TODO reject and bounce back to sender?
        return HttpResponse(status=200)
    logger.debug('stay@ for location: %s' % location)
    logger.debug(request.POST)

    # we think that message_headers is a list of strings
    header_txt = request.POST.get('message-headers')
    message_headers = json.loads(header_txt)
    message_header_keys = [item[0] for item in message_headers]

    # make sure this isn't an email we have already forwarded (cf. emailbombgate 2014)
    # A List-Id header will only be present if it has been added manually in
    # this function, ie, if we have already processed this message.
    if request.POST.get('List-Id') or 'List-Id' in message_header_keys:
        # mailgun requires a code 200 or it will continue to retry delivery
        logger.debug('List-Id header was found! Dropping message silently')
        return HttpResponse(status=200)

    #if 'Auto-Submitted' in message_headers or message_headers['Auto-Submitted'] != 'no':
    if 'Auto-Submitted' in message_header_keys:
        logger.info('message appears to be auto-submitted. reject silently')
        return HttpResponse(status=200)

    recipient = request.POST.get('recipient')
    try:
        to = request.POST.get('To')
        logger.debug('Got "To" field: %s' % to)
    except:
        pass
    from_address = request.POST.get('from')
    logger.debug('from: %s' % from_address)
    sender = request.POST.get('sender')
    logger.debug('sender: %s' % sender)
    subject = request.POST.get('subject')
    body_plain = request.POST.get('body-plain')
    body_html = request.POST.get('body-html')

    # retrieve the current house admins for this location
    location_admins = location.house_admins.all()
    bcc_list = []
    for person in location_admins:
        if person.email not in bcc_list:
            bcc_list.append(person.email)
    logger.debug("bcc list: %s" % bcc_list)

    # Make sure this person can post to our list
    #if not sender in bcc_list:
    #	# TODO - This shoud possibly send a response so they know they were blocked
    #	logger.warn("Sender (%s) not allowed.  Exiting quietly." % sender)
    #	return HttpResponse(status=200)
    if sender in bcc_list:
        bcc_list.remove(sender)

    # prefix subject, but only if the prefix string isn't already in the
    # subject line (such as a reply)
    if subject.find(location.email_subject_prefix) < 0:
        prefix = "[" + location.email_subject_prefix + "] [Admin] "
        subject = prefix + subject
    logger.debug("subject: %s" % subject)

    # add in footer
    text_footer = '''\n\n-------------------------------------------\nYou are receving email to %s because you are a location admin at %s. Send mail to this list to reach other admins.''' % (
        recipient, location.name)
    body_plain = body_plain + text_footer
    if body_html:
        html_footer = '''<br><br>-------------------------------------------<br>You are receving email to %s because you are a location admin at %s. Send mail to this list to reach other admins.''' % (
            recipient, location.name)
        body_html = body_html + html_footer

    # send the message
    list_address = location.from_email()
    mailgun_data = {
        "from": from_address,
        "to": [
            recipient,
        ],
        "bcc": bcc_list,
        "subject": subject,
        "text": body_plain,
        "html": body_html,
        # attach some headers: LIST-ID, REPLY-TO, MSG-ID, precedence...
        # Precedence: list - helps some out of office auto responders know not to send their auto-replies.
        "h:List-Id": list_address,
        "h:Precedence": "list",
        # Reply-To: list email apparently has some religious debates
        # (http://www.gnu.org/software/mailman/mailman-admin/node11.html) but seems
        # to be common these days
        "h:Reply-To": from_address
    }
    return mailgun_send(mailgun_data)