|
1
|
|
|
# -*- coding: utf-8 -*- |
|
2
|
|
|
import transaction |
|
3
|
|
|
|
|
4
|
|
|
import sqlalchemy as sa |
|
5
|
|
|
|
|
6
|
|
|
from pyjobsweb.lib.time import base_time |
|
7
|
|
|
from pyjobsweb.model import DeclarativeBase, DBSession |
|
8
|
|
|
from pyjobsweb.model.elasticsearch_model.company import Company \ |
|
9
|
|
|
as CompanyElastic |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
class Company(DeclarativeBase): |
|
13
|
|
|
__tablename__ = 'companies' |
|
14
|
|
|
|
|
15
|
|
|
# The id column stores the company name column slug |
|
16
|
|
|
id = sa.Column(sa.String(1024), primary_key=True) |
|
17
|
|
|
|
|
18
|
|
|
name = sa.Column(sa.String(1024), nullable=False, default='') |
|
19
|
|
|
logo_url = sa.Column(sa.String(1024), nullable=False, default='') |
|
20
|
|
|
description = sa.Column(sa.Text(), nullable=False, default='') |
|
21
|
|
|
url = sa.Column(sa.String(1024), nullable=False, default='') |
|
22
|
|
|
# Comma separated technologies. We only store a textual representation of |
|
23
|
|
|
# the technologies used by the company to make it easier to duplicate the |
|
24
|
|
|
# model into Elasticsearch to allow searching on this field (Elasticsearch |
|
25
|
|
|
# is really good at string research). |
|
26
|
|
|
technologies = sa.Column(sa.Text(), nullable=False, default='') |
|
27
|
|
|
|
|
28
|
|
|
address = sa.Column(sa.String(1024), nullable=False, default='') |
|
29
|
|
|
address_is_valid = sa.Column(sa.Boolean, nullable=False, default=True) |
|
30
|
|
|
|
|
31
|
|
|
email = sa.Column(sa.String(1024), nullable=False, default='') |
|
32
|
|
|
phone = sa.Column(sa.String(1024), nullable=False, default='') |
|
33
|
|
|
|
|
34
|
|
|
latitude = sa.Column(sa.Float, nullable=False, default=0.0) |
|
35
|
|
|
longitude = sa.Column(sa.Float, nullable=False, default=0.0) |
|
36
|
|
|
geolocation_is_valid = sa.Column(sa.Boolean, nullable=False, default=False) |
|
37
|
|
|
|
|
38
|
|
|
validated = sa.Column(sa.Boolean, nullable=False, default=False) |
|
39
|
|
|
|
|
40
|
|
|
last_modified = sa.Column(sa.DateTime(timezone=True), nullable=False, |
|
41
|
|
|
server_default=sa.func.now(), |
|
42
|
|
|
onupdate=sa.func.current_timestamp()) |
|
43
|
|
|
|
|
44
|
|
|
last_sync = sa.Column(sa.DateTime(timezone=True), |
|
45
|
|
|
nullable=False, default=base_time()) |
|
46
|
|
|
|
|
47
|
|
|
def to_elasticsearch_document(self): |
|
48
|
|
|
result = CompanyElastic( |
|
49
|
|
|
meta={'id': self.id}, |
|
50
|
|
|
id=self.id, |
|
51
|
|
|
name=self.name, |
|
52
|
|
|
logo_url=self.logo_url, |
|
53
|
|
|
description=self.description, |
|
54
|
|
|
url=self.url, |
|
55
|
|
|
technologies=self.technologies, |
|
56
|
|
|
address=self.address, |
|
57
|
|
|
address_is_valid=self.address_is_valid, |
|
58
|
|
|
email=self.email, |
|
59
|
|
|
phone=self.phone, |
|
60
|
|
|
geolocation=dict(lat=self.latitude, lon=self.longitude), |
|
61
|
|
|
geolocation_is_valid=self.geolocation_is_valid |
|
62
|
|
|
) |
|
63
|
|
|
|
|
64
|
|
|
return result |
|
65
|
|
|
|
|
66
|
|
|
def __repr__(self): |
|
67
|
|
|
return "<Company: id='%d'>" % self.id |
|
68
|
|
|
|
|
69
|
|
|
@classmethod |
|
70
|
|
|
def get_validated_companies(cls): |
|
71
|
|
|
return DBSession.query(cls) \ |
|
72
|
|
|
.filter_by(validated=True) \ |
|
73
|
|
|
.order_by(cls.name.asc()) |
|
74
|
|
|
|
|
75
|
|
|
@classmethod |
|
76
|
|
|
def get_validated_company(cls, company_id): |
|
77
|
|
|
return DBSession.query(cls) \ |
|
78
|
|
|
.filter(cls.id == company_id) \ |
|
79
|
|
|
.filter_by(validated=True) \ |
|
80
|
|
|
.one() |
|
81
|
|
|
|
|
82
|
|
|
@classmethod |
|
83
|
|
|
def get_company(cls, company_id): |
|
84
|
|
|
return DBSession.query(cls).filter(cls.id == company_id).one() |
|
85
|
|
|
|
|
86
|
|
|
@classmethod |
|
87
|
|
|
def get_pending_geolocations(cls): |
|
88
|
|
|
return DBSession.query(cls) \ |
|
89
|
|
|
.filter_by(address_is_valid=True) \ |
|
90
|
|
|
.filter_by(geolocation_is_valid=False) \ |
|
91
|
|
|
.filter_by(validated=True) \ |
|
92
|
|
|
.order_by(cls.id.asc()) |
|
93
|
|
|
|
|
94
|
|
|
@classmethod |
|
95
|
|
|
def set_geolocation(cls, company_id, lat, lon): |
|
96
|
|
|
transaction.begin() |
|
97
|
|
|
DBSession.query(cls) \ |
|
98
|
|
|
.filter(cls.id == company_id) \ |
|
99
|
|
|
.update({'latitude': lat, |
|
100
|
|
|
'longitude': lon, |
|
101
|
|
|
'geolocation_is_valid': True}) |
|
102
|
|
|
transaction.commit() |
|
103
|
|
|
|
|
104
|
|
|
@classmethod |
|
105
|
|
|
def set_geolocation_is_valid(cls, company_id, is_valid): |
|
106
|
|
|
transaction.begin() |
|
107
|
|
|
DBSession.query(cls) \ |
|
108
|
|
|
.filter(cls.id == company_id) \ |
|
109
|
|
|
.update({'geolocation_is_valid': is_valid}) |
|
110
|
|
|
transaction.commit() |
|
111
|
|
|
|
|
112
|
|
|
@classmethod |
|
113
|
|
|
def set_address_is_valid(cls, company_id, is_valid): |
|
114
|
|
|
transaction.begin() |
|
115
|
|
|
DBSession.query(cls) \ |
|
116
|
|
|
.filter(cls.id == company_id) \ |
|
117
|
|
|
.update({'address_is_valid': is_valid}) |
|
118
|
|
|
transaction.commit() |
|
119
|
|
|
|
|
120
|
|
|
@classmethod |
|
121
|
|
|
def get_dirty_rows(cls): |
|
122
|
|
|
return DBSession.query(cls) \ |
|
123
|
|
|
.filter(cls.validated) \ |
|
124
|
|
|
.filter(cls.last_modified > cls.last_sync) \ |
|
125
|
|
|
.order_by(cls.id.asc()) |
|
126
|
|
|
|
|
127
|
|
|
@classmethod |
|
128
|
|
|
def update_last_sync(cls, job_id, timestamp): |
|
129
|
|
|
transaction.begin() |
|
130
|
|
|
DBSession.query(cls) \ |
|
131
|
|
|
.filter(cls.id == job_id) \ |
|
132
|
|
|
.update({'last_sync': timestamp, |
|
133
|
|
|
'last_modified': cls.last_modified}) |
|
134
|
|
|
DBSession.query(cls) \ |
|
135
|
|
|
.filter(cls.id == job_id) \ |
|
136
|
|
|
.filter(cls.last_modified < timestamp) \ |
|
137
|
|
|
.update({'last_modified': timestamp}) |
|
138
|
|
|
transaction.commit() |
|
139
|
|
|
|
|
140
|
|
|
@classmethod |
|
141
|
|
|
def reset_last_sync(cls): |
|
142
|
|
|
transaction.begin() |
|
143
|
|
|
DBSession.query(cls) \ |
|
144
|
|
|
.filter(cls.validated) \ |
|
145
|
|
|
.update({'last_sync': base_time()}) |
|
146
|
|
|
transaction.commit() |
|
147
|
|
|
|