-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
135 lines (110 loc) · 4.09 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import datetime
import os
from flask import Flask, redirect, render_template, request, url_for
from flask.ext.cache import Cache
from flask.ext.mail import Mail, Message
from flask.ext.sqlalchemy import SQLAlchemy
from flask_debugtoolbar import DebugToolbarExtension
from werkzeug.contrib.atom import AtomFeed
app = Flask(__name__)
app.config.from_object(os.environ['APP_SETTINGS'])
app.cache = Cache(app)
db = SQLAlchemy(app)
mail = Mail(app)
toolbar = DebugToolbarExtension(app)
class JobListing(db.Model):
__tablename__ = 'scrapedresults'
id = db.Column(db.Integer, primary_key=True)
term = db.Column(db.Text)
location = db.Column(db.Text)
jobposition = db.Column(db.Text)
department = db.Column(db.Text)
agency = db.Column(db.Text)
dateposted = db.Column(db.Text)
link = db.Column(db.Text)
def __init__(
self,
term,
location,
jobposition,
department,
agency,
dateposted,
link):
self.term = term
self.location = location
self.jobposition = jobposition
self.department = department
self.agency = agency
self.dateposted = dateposted
self.link = link
def __repr__(self):
return self.jobposition
class EmailListing(db.Model):
__tablename__ = 'emaillist'
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.Text)
def __init__(self, email):
self.email = email
def __repr__(self):
return self.email
@app.route('/', methods=['GET'])
@app.cache.cached(timeout=10800)
def main():
"""
Route for home page. Checks if old page ids like spacejobs.us/?page=6 exist
and redirects back to the root of the domain with a 301.
Caches DB query for 180 minutes.
"""
old_url = request.args.get('page')
if old_url:
return redirect('/', 301)
else:
return render_template(
"main.html",
jobPostings=JobListing.query.order_by(
JobListing.id.desc()).limit(1000))
@app.route('/submit', methods=['GET', 'POST'])
def submit():
# this variable gets rid of form after submission
submit_bool = False
# if subscribe button clicked
if request.method == 'POST':
if request.form['submit'] == 'job_submit':
term = request.form['term']
location = request.form['location']
jobposition = request.form['jobposition']
department = request.form['department']
agency = request.form['agency']
dateposted = datetime.date.today()
dateposted = dateposted.strftime('%m-%d-%Y')
link = request.form['link']
# send job back to email for reference
msg = Message(
'Job posting attached',
sender="[email protected]",
recipients=["[email protected]"])
msg.body = str(term) + '\n' + str(location) + '\n' + str(jobposition) + '\n' + \
str(department) + '\n' + str(agency) + '\n' + str(dateposted) + '\n' + str(link)
try:
mail.send(msg)
except:
pass
# set to true = swap out form for thank you message
submit_bool = True
return render_template("submit.html", submit_bool=submit_bool)
# coming soon to a theater near you
@app.route('/metrics', methods=['GET'])
def metrics():
return render_template("metrics.html")
@app.route('/recent.atom')
def atom_feed():
feed = AtomFeed('50 Newest Jobs This Week',
feed_url=request.url, url=request.url_root)
jobs = JobListing.query.order_by(JobListing.dateposted.desc()).limit(50).all()
for job in jobs:
title = job.term + ' ' + job.jobposition + ' @ ' + job.agency + ' - ' + job.location
content = 'Department: ' + job.department + ' ---- Location: ' + job.location
date_obj = datetime.datetime.strptime(job.dateposted, '%m-%d-%Y')
feed.add(title, content, content_type='html',author=job.agency,url=job.link,updated=date_obj)
return feed.get_response()