-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
99 lines (74 loc) · 1.82 KB
/
models.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
from datetime import datetime
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
class URL(db.Model):
"""Search URLs to find listings."""
__tablename__= 'urls'
id = db.Column(
db.Integer,
primary_key=True
)
name = db.Column(
db.String(50),
nullable=False,
unique=True
)
search_url = db.Column(
db.Text,
nullable=False
)
active = db.Column(
db.Boolean,
default=True
)
@classmethod
def add_search(cls, name, search_url):
"""creates new record in URL tabel with name and search URL"""
url = URL(
name=name,
search_url=search_url
)
db.session.add(url)
return url
@classmethod
def get_searches(cls):
searches = URL.query.filter_by(active=True).all()
return searches
class Listing(db.Model):
"""Listings from Craigslist queries"""
__tablename__ = 'listings'
id = db.Column(
db.Integer,
primary_key=True
)
url = db.Column(
db.String,
nullable=False,
unique=True
)
title = db.Column(
db.String,
nullable=False
)
timestamp = db.Column(
db.DateTime,
nullable=False,
default=datetime.utcnow
)
@classmethod
def find_listing(cls, listing_url):
listing = Listing.query.filter_by(url=listing_url).all()
return listing
@classmethod
def add_listing(cls, url, title):
listing = Listing(url=url,
title=title)
db.session.add(listing)
db.session.commit()
def connect_db(app):
"""Connect this database to provided Flask app.
You should call this in your Flask app.
"""
app.app_context().push()
db.app = app
db.init_app(app)