-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcartodb_sql.py
More file actions
67 lines (48 loc) · 2.19 KB
/
cartodb_sql.py
File metadata and controls
67 lines (48 loc) · 2.19 KB
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
# -*- coding: utf-8 -*-
from cartodb import CartoDBAPIKey, CartoDBException
import os
from random import choice
CDB_USER = os.environ.get('CDB_USER', None)
CDB_API_KEY = os.environ.get('CDB_API_KEY', None)
cl = CartoDBAPIKey(CDB_API_KEY, CDB_USER)
def check_login(clientname, username, password):
q = "select geolocation, operating_systems, browsers from company where company_name = '%s' and email = '%s' and password = '%s'" % (clientname, username, password)
results = cl.sql(q)
print q
print results
if results['total_rows'] > 0:
return results['rows']
return None
def get_log(clientname):
q = "select st_x(a.the_geom) as lng, st_y(a.the_geom) as lat, ip, result, info->'os' as os, info->'browser' as browser, a.created_at as attempt_ts from attempt a, company b where a.company_id = b.cartodb_id and b.company_name = '%s' order by attempt_ts desc" % clientname
results = cl.sql(q)
print q
print results['rows']
if results['total_rows'] > 0:
return results['rows']
def location_intersects(lat, lon):
results = cl.sql('select allowed from bboxes where st_intersects(CDB_LatLng(%f, %f), the_geom) limit 1' %(lat,lon))
print results
if results['total_rows'] == 0:
return None
allow = results['rows'][0]['allowed']
return allow
def mark_login_attempt(ip, latitude, longitude, os, mobile, browser, result):
args = locals()
SQL = '''
insert into attempt(the_geom,ip,info,result) values(CDB_LatLng({lat}, {lon}), '{ip}', '{data}',{result})
'''
query = SQL.format(data=', '.join(['%s => "%s"' %(el, args[el]) for el in args]), lat=latitude, lon=longitude, ip=ip, result=result)
print query
print cl.sql(query)
def dummy_create_company(data):
SQL = '''
INSERT INTO company
(email, password, company_name,
phone, geolocation_methods,
enabled_browsers, operating_systems)
VALUES ('{email}', '{password}', '{company_name}',
'{phone}', {geolocation_methods}, {enabled_browsers}, {operating_systems})'''
query = SQL.format(*data)
results = cl.sql(query)
return results