def editReport(**kwargs): context = { 'report': postgres.connection().selectOne('DMARCReport', kwargs), 'dMARCReportRows': postgres.connection().select('DMARCReportRow', kwargs), } return flask.render_template('report.html', **context)
def redirect(emailHash): postgres.connection().call('NewEmailSendFeedback', (emailHash, 'redirected', flask.request.remote_addr)) redirectURL = postgres.connection().call('EmailSendRedirectURL', emailHash) if redirectURL: return flask.redirect(redirectURL) flask.abort(404)
def view(emailHash): postgres.connection().call('NewEmailSendFeedback', (emailHash, 'viewed', flask.request.remote_addr)) body = postgres.connection().call('ViewEmailBody', emailHash) if body: return body flask.abort(404)
def upsertSubscriber(**kwargs): try: data = postData() if data: return postgres.connection().updateOne('Subscriber', data, kwargs) return postgres.connection().selectOne('Subscriber', kwargs) except postgres.NoRow: return postgres.connection().insert('Subscriber', postData(kwargs))
def editEmail(**kwargs): identifiers = {key: kwargs.pop(key) for key in ('fromaddress', 'emailid')} if 'email' not in kwargs: kwargs['email'] = postgres.connection().selectOne('Email', identifiers) kwargs['emailVariations'] = postgres.connection().select('EmailVariation', identifiers, 'variationId') kwargs['subscriberLocales'] = postgres.connection().select('SubscriberLocaleStatistics', {'fromAddress': identifiers['fromaddress']}) kwargs['exampleProperties'] = postgres.connection().call('SubscriberExampleProperties', identifiers['fromaddress']) kwargs['force'] = flask.request.args return flask.render_template('email.html', **kwargs)
def prepareBulkEmail(**kwargs): identifiers = {key: kwargs.pop(key) for key in ('fromaddress', 'emailid')} kwargs['email'] = postgres.connection().selectOne('Email', identifiers) kwargs['subscriberLocales'] = postgres.connection().select('EmailSubscriberLocaleStatistics', dict(list(identifiers.items()) + [('locale', kwargs['email']['locale'])])) kwargs['emailVariations'] = postgres.connection().select('EmailVariationStatistics', dict(list(identifiers.items()) + [('state', 'sent')])) kwargs['maxSubscriber'] = sum(row['remaining'] for row in kwargs['subscriberLocales']) kwargs['exampleProperties'] = postgres.connection().call('SubscriberExampleProperties', identifiers['fromaddress']) kwargs['propertyCount'] = 10 kwargs['canSend'] = (kwargs['email']['bulk'] and kwargs['email']['state'] == 'sent' and kwargs['maxSubscriber'] > 0 and kwargs['emailVariations']) return flask.render_template('bulkemail.html', **kwargs)
def editSender(**kwargs): identifiers = {key: kwargs.pop(key) for key in ('fromaddress',)} if 'sender' not in kwargs: kwargs['sender'] = postgres.connection().selectOne('Sender', identifiers) return flask.render_template('sender.html', **kwargs)
def domainStatistics(**kwargs): context = { 'identifiers': kwargs, 'dMARCReports': postgres.connection().select('DMARCReportDetail', kwargs), } return flask.render_template('domainstatistics.html', **context)
def newSender(**kwargs): parts = parseURL(flask.request.url_root) kwargs['sender'] = { 'returnurlroot': parts['protocol'] + '//' + parts['root'] + '/', 'password': postgres.connection().call('GeneratePassword'), } return flask.render_template('sender.html', **kwargs)
def newEmail(**kwargs): identifiers = {key: kwargs.pop(key) for key in ('fromaddress',)} kwargs['email'] = { 'fromaddress': identifiers['fromaddress'], 'state': 'new', } kwargs['subscriberLocales'] = postgres.connection().select('SubscriberLocaleStatistics', identifiers) return flask.render_template('email.html', **kwargs)
def paginate(tableName, conditions): response = {} if 'limit' in conditions: response['limit'] = conditions.pop('limit') else: response['limit'] = 100 # The default limit. if 'offset' in conditions: response['offset'] = conditions.pop('offset') response['records'] = postgres.connection().select(tableName, conditions, **response) return response
def wrapped(**kwargs): if flask.request.method in ('POST', 'PUT'): if flask.request.headers['Content-Type'] != 'application/json': raise werkzeug.exceptions.BadRequest('Content-Type must be application/json') if not isinstance(flask.request.json, dict): raise werkzeug.exceptions.BadRequest('data must be a JSON object') if not flask.request.authorization: raise werkzeug.exceptions.Unauthorized('authentication required') with postgres.connection(): if not postgres.connection().call('SenderAuthenticate', flask.request.authorization): raise werkzeug.exceptions.Unauthorized('authentication failed') response = operation(fromAddress=flask.request.authorization.username, **kwargs) assert isinstance(response, dict) return flask.jsonify(response)
def saveEmailVariation(**kwargs): with postgres.connection() as transaction: if 'variationid' not in kwargs: transaction.insert('EmailVariation', formData(**kwargs)) kwargs['saveEmailVariationMessage'] = 'Email Variation created.' else: transaction.updateOne('EmailVariation', formData(), kwargs) kwargs['saveEmailVariationMessage'] = 'Email variation updated.' return editEmail(**kwargs)
def saveSender(**kwargs): with postgres.connection() as transaction: if 'fromaddress' not in kwargs: kwargs['sender'] = transaction.insert('Sender', formData()) kwargs['fromaddress'] = kwargs['sender']['fromaddress'] kwargs['senderMessage'] = 'Sender created.' else: kwargs['sender'] = transaction.updateOne('Sender', formData(), kwargs) kwargs['saveMessage'] = 'Sender updated.' return editSender(**kwargs)
def saveEmail(**kwargs): with postgres.connection() as transaction: if 'emailid' not in kwargs: kwargs['email'] = transaction.insert('Email', formData(**kwargs)) kwargs['emailid'] = kwargs['email']['emailid'] kwargs['saveMessage'] = 'Email created.' else: kwargs['email'] = transaction.updateOne('Email', formData(), kwargs) kwargs['saveMessage'] = 'Email updated.' return editEmail(**kwargs)
def emailStatistics(**kwargs): with postgres.connection() as transaction: context = { 'identifiers': kwargs, 'emailSentDates': transaction.select('EmailSentDateStatistics', kwargs), 'emailVariations': transaction.select('EmailVariationStatistics', kwargs), } if 'emailid' in kwargs: context['email'] = transaction.selectOne('Email', kwargs) context['emailSubscriberLocales'] = transaction.select('EmailSubscriberLocaleStatistics', kwargs) else: context['emails'] = transaction.select('EmailStatistics', kwargs) return flask.render_template('emailstatistics.html', **context)
def main(): import xml.etree.cElementTree as ElementTree with postgres.connection() as transaction: transaction.truncate('DMARCReportRow') for report in transaction.select('DMARCReport'): tree = ElementTree.fromstring(report['body']) for record in tree.iter('record'): transaction.insert('DMARCReportRow', { 'reporterAddress': report['reporteraddress'], 'reportId': report['reportid'], 'source': record.find('row/source_ip').text, 'messageCount': record.find('row/count').text, 'disposition': record.find('row/policy_evaluated/disposition').text, 'dKIMPass': record.find('row/policy_evaluated/dkim').text == 'pass', 'sPFPass': record.find('row/policy_evaluated/spf').text == 'pass', })
def sendTestEmail(**kwargs): toAddress = formData()['toaddress'] with postgres.connection() as transaction: transaction.insertIfNotExists('Subscriber', { 'fromAddress': kwargs['fromaddress'], 'toAddress': toAddress, }) transaction.upsert('EmailSend', { 'state': 'new', 'variationId': kwargs['variationid'], }, { 'fromAddress': kwargs['fromaddress'], 'toAddress': toAddress, 'emailId': kwargs['emailid'], }) kwargs['sendTestEmailMessage'] = 'Test email message added to the queue.' return editEmail(**kwargs)
def getEmailVariation(**kwargs): return postgres.connection().selectOne('EmailVariation', kwargs)
def track(emailHash): postgres.connection().call('NewEmailSendFeedback', (emailHash, 'tracked', flask.request.remote_addr)) # Return 1px * 1px transparent image. return flask.send_file(io.BytesIO(b'GIF89a\x01\x00\x01\x00\x80\x00\x00\xff\xff\xff\x00\x00\x00!\xf9\x04\x01\x00\x00\x00\x00,\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x02D\x01\x00;'), mimetype='image/gif')
def getEmail(**kwargs): return postgres.connection().selectOne('NestedEmail', kwargs)
def addEmail(**kwargs): return postgres.connection().insert('NestedEmail', postData(kwargs))
def setSender(**kwargs): return postgres.connection().updateOne('Sender', postData(kwargs), kwargs)
def getSender(**kwargs): return postgres.connection().selectOne('Sender', kwargs)
def addEmailVariation(**kwargs): return postgres.connection().insert('EmailVariation', postData(kwargs))
def sendToSubscriber(**kwargs): try: return postgres.connection().call('SendToSubscriber', postData(kwargs)) except postgres.NoRow: raise NotAllowed('cannot send to this address')
def addSubscriber(**kwargs): return postgres.connection().insert('Subscriber', postData(kwargs))
def getSubscriber(**kwargs): return postgres.connection().selectOne('Subscriber', kwargs)
def unsubscribe(emailHash): if postgres.connection().call('NewEmailSendFeedback', (emailHash, 'unsubscribed', flask.request.remote_addr)): return 'You are successfully unsubscribed.' else: return 'You have already unsubscribed.'
def upsertEmailVariation(**kwargs): try: return postgres.connection().updateOne('EmailVariation', postData(), kwargs) except postgres.NoRow: return postgres.connection().insert('EmailVariation', postData(kwargs))