-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
112 lines (73 loc) · 2.58 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import os
import datetime
from datetime import timedelta
import pytz
from flask import Flask, jsonify, url_for, redirect, request, render_template
from flask_pymongo import PyMongo
from flask_restful import Api, Resource
"""
MongoDB configurations
"""
MONGO_URL = os.environ.get('MONGODB_URI')
if not MONGO_URL:
MONGO_URL = 'mongodb://127.0.0.1:27017/ad_db';
app = Flask(__name__)
app.config["MONGO_URI"] = MONGO_URL
mongo = PyMongo(app, config_prefix='MONGO')
utc=pytz.UTC
class PartnerPost (Resource):
def post(self):
data = request.get_json()
post_data = {}
ad_post_time = datetime.datetime.now()
if not data:
data = {"response": "Error"}
return jsonify(data)
else:
partner_id = data.get('partner_id')
duration = data.get('duration')
ad_content = data.get('ad_content')
#convert ad_duration to integer value
seconds = int(duration)
ad_duration =timedelta(seconds =seconds)
expiration = ad_post_time + ad_duration
print ad_duration
if data.has_key('partner_id') and data.has_key('duration') and data.has_key('ad_content'):
if mongo.db.partner_id.find_one({"partner_id": partner_id}):
return {"response": "partner already has ad running"}
else:
post_data['partner_id'] = str(partner_id)
post_data['duration'] = int(duration)
post_data['ad_content'] = str(ad_content)
post_data['expiration'] = expiration
mongo.db.partner_id.insert(post_data)
return {"response": "post data sucessfuly!"}
del post_data
else:
return {"response": "value missing || incorrect data format"}
return redirect(url_for("partner_id"))
class PartnerGet (Resource):
def get(self, id):
data = []
cursor = mongo.db.partner_id.find({"partner_id":str(id)}, {"_id": 0}).limit(1)
if cursor.count() == 0:
return {"response": "partner id does not exits"}
else:
for partner_id in cursor:
if utc.localize(datetime.datetime.now()) < partner_id['expiration']:
#print partner_id['expiration']
del partner_id['expiration']
data.append(partner_id)
del cursor
else:
return {"response": "Ad expired for partner_id = " + str(partner_id['partner_id'])}
return jsonify({"response": data})
class PartnerList(Resource):
def get(self):
return [x for x in mongo.db.partner_id.find({}, {"_id": 0, "expiration": 0}).limit(10)]
api = Api(app)
api.add_resource(PartnerList, "/", endpoint="list")
api.add_resource(PartnerPost, "/ad", endpoint="partner_id")
api.add_resource(PartnerGet, "/ad/<string:id>", endpoint = "id")
if __name__ == "__main__":
app.run(debug=True, port=3000)