2d5fe45 by Anon Ray at 2013-06-27 1
# SwtMaker
2
# -------
3
# Server-side component to make sweets and post them to specified 
4
# sweet store
5
#
6
# License: BSD, see LICENSE for more details.
7
# Servelots 2013
8
# Authors:
9
#   Arvind Khadri <arvind@servelots.com>
10
#   Anon Ray <rayanon@servelots.com>
11
3bf81ca by Arvind at 2013-06-21 12
import requests
13
import json
2d5fe45 by Anon Ray at 2013-06-27 14
from datetime import datetime
15
16
TIMESTAMP_FORMAT = '%d-%m-%Y %H:%M:%S'
85fe77f by Arvind at 2013-06-21 17
2d5fe45 by Anon Ray at 2013-06-27 18
def sweet(sweet_url, sweet_list):
19
    sweets = makeSweet(sweet_list)
20
    if not sweets:
21
        return False
3bf81ca by Arvind at 2013-06-21 22
    else:
2d5fe45 by Anon Ray at 2013-06-27 23
        request = requests.api.post(sweet_url, {'data': json.dumps(sweets)})
24
        if request.status_code == 200:
25
            return True
26
        else:
27
            return False
28
29
def makeSweet(sweet_list):
30
    for sweet in sweet_list:
31
        if len(sweet['who']) and len(sweet['what']) and len(sweet['where'])\
32
           and len(sweet['how']):
e2d7af5 by Anon Ray at 2013-06-28 33
            sweet['created'] = datetime.utcnow().strftime(TIMESTAMP_FORMAT)
2d5fe45 by Anon Ray at 2013-06-27 34
        else:
35
            return False
36
    return sweet_list