Skip to content

Commit

Permalink
Merge pull request #4 from sendwithus/update/send-call
Browse files Browse the repository at this point in the history
Update send call
  • Loading branch information
FlipCodes committed Jun 6, 2016
2 parents 8fd1f4c + 4b1c70f commit e8f3840
Showing 1 changed file with 16 additions and 16 deletions.
32 changes: 16 additions & 16 deletions sendwithus_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ def pull_snippets(swu, directory):


def push_snippets(swu, directory):
updated = False
updated = False
snippets = swu.snippets().json()
snippet_dir = os.path.join(directory, 'snippets')
if os.path.exists(os.path.join(snippet_dir, '.swu')):
lastpush = os.path.getmtime(os.path.join(snippet_dir, '.swu'))
else:
lastpush = 0

for root, subFolders, files in os.walk(snippet_dir):
for snippet_file in files:
path = os.path.join(root, snippet_file)
Expand Down Expand Up @@ -102,8 +102,8 @@ def pull_templates(swu, directory):


def push_templates(swu, directory):
updated = False
templates = swu.templates().json()
updated = False
templates = swu.templates().json()
templates_dir = os.path.join(directory, 'templates')

if os.path.exists(os.path.join(templates_dir, '.swu')):
Expand Down Expand Up @@ -177,7 +177,7 @@ def push_templates(swu, directory):
if not updated:
print "There are no templates that has been modified since the last push."
touch(os.path.join(templates_dir, '.swu'))

def get_template_info_by_path(swu, template_path):
path_elements_list = os.path.normpath(template_path).split(os.sep)
version_name = os.path.splitext(os.path.basename(path_elements_list[-1]))[0]
Expand All @@ -192,9 +192,9 @@ def get_template_info_by_path(swu, template_path):
if version["name"] == version_name:
version_id = version["id"]
break
break
break
return json.dumps(dict(template_name=template_name, template_id=template_id, version_name=version_name, version_id = version_id))

def render_template(swu, data_file, template):
if not data_file:
print 'No source data provided.'
Expand All @@ -208,14 +208,14 @@ def render_template(swu, data_file, template):
if os.path.exists(data_file):
data = _read_file(data_file)
template_info = json.loads(get_template_info_by_path(swu, template))

render_result = swu.render(template_info['template_id'], json.loads(data), template_info['version_id'], template_info['version_name'])
print render_result.json()['html'].encode("utf-8")
else:
print 'File {} not found.'.format(os.path.abspath(data_file))
else:
print 'Invalid template name and version provided.'

def send_mail(swu, data_file, email, template):
if not data_file:
print 'No source data provided.'
Expand All @@ -231,8 +231,8 @@ def send_mail(swu, data_file, email, template):
if os.path.exists(data_file):
data = _read_file(data_file)
template_info = json.loads(get_template_info_by_path(swu, template))
recipient = json.dumps(dict(address=email))
render_result = swu.send(template_info['template_id'], json.loads(recipient), json.loads(data), None, None, None, None, None, template_info['version_name'])
recipient = json.dumps(dict(address=email))
render_result = swu.send(email_id=template_info['template_id'], recipient=json.loads(recipient), email_data=json.loads(data), email_version_name=template_info['version_name'])
try:
response = render_result.json();
if response['success']:
Expand All @@ -250,18 +250,18 @@ def main():
main_parser.add_argument('-e', '--email', type=str, required=False, help="optional, required only in conjunction with send command, specifies the email address to which the email will be sent to")

subparsers = main_parser.add_subparsers(help='commands', dest='command')

pull_parser = subparsers.add_parser('pull', help='process pull templates and snippets')
pull_parser.add_argument('resource', choices=CMD_RESOURCES)
pull_parser.add_argument('directory', nargs='?', default='.')

push_parser = subparsers.add_parser('push', help='process push templates and snippets')
push_parser.add_argument('resource', choices=CMD_RESOURCES)
push_parser.add_argument('directory', nargs='?', default='.')

render_parser = subparsers.add_parser('render', help='process render templates')
render_parser.add_argument('template', nargs='?', help="Required, specifies path to template version which is to be rendered. <template_name>/<template_version>.html")

send_parser = subparsers.add_parser('send', help='process send email')
send_parser.add_argument('template', nargs='?', help="Required, specifies path to template version which is to be sent. <template_name>/<template_version>.html")

Expand Down Expand Up @@ -291,4 +291,4 @@ def main():
print 'Unknown Command: {} {}'.format(args.command, args.resource)

if __name__ == "__main__":
main()
main()

0 comments on commit e8f3840

Please sign in to comment.