-
Notifications
You must be signed in to change notification settings - Fork 1
/
db_util.py
58 lines (51 loc) · 1.96 KB
/
db_util.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
from models import *
class DBUtil:
def __init__(self):
pass
@classmethod
def set_worker_attributes(cls, uid, longitude, latitude, capacity=None, reliability=None, min_lon=None,
min_lat=None, max_lon=None, max_lat=None, is_online=None, commit=True):
user_detail = session.query(WorkerDetail).filter_by(id=uid, is_online=is_online).first()
if user_detail is None:
user_detail = WorkerDetail()
user_detail.id = uid
user_detail.is_online = is_online
user_detail.longitude = longitude
user_detail.latitude = latitude
user_detail.capacity = capacity
user_detail.reliability = reliability
user_detail.region_min_lon = min_lon
user_detail.region_min_lat = min_lat
user_detail.region_max_lon = max_lon
user_detail.region_max_lat = max_lat
session.add(user_detail)
if commit:
session.commit()
@classmethod
def create_hit(cls, longitude, latitude, arrival_time, expire_time, require_answer_count, entropy, confidence,
is_valid=None, commit=True):
hit_detail = HitDetail()
hit_detail.is_valid = is_valid
session.add(hit_detail)
if entropy is not None:
hit_detail.entropy = entropy
if confidence is not None:
hit_detail.confidence = confidence
hit_detail.longitude = longitude
hit_detail.latitude = latitude
hit_detail.begin_time = arrival_time
hit_detail.end_time = expire_time
hit_detail.required_answer_count = require_answer_count
session.flush()
session.refresh(hit_detail)
if commit:
session.commit()
return hit_detail.id
@classmethod
def clear(cls):
session.query(HitDetail).delete()
session.query(WorkerDetail).delete()
session.commit()
@classmethod
def initialize_db(cls):
DBUtil.clear()