summaryrefslogtreecommitdiff
path: root/rater.py
diff options
context:
space:
mode:
authorerdgeist <erdgeist@erdgeist.org>2024-10-12 21:29:51 +0200
committererdgeist <erdgeist@erdgeist.org>2024-10-12 21:29:51 +0200
commit27db823c46c83b13b4cf36c865471633da74816e (patch)
tree0d1ded7ffef4905e7652440011b3216515856c19 /rater.py
parentcd9e07341c2eebba1ed6980acdd07acc6ab9c9f4 (diff)
Use pretalx instead of frab
Diffstat (limited to 'rater.py')
-rwxr-xr-xrater.py84
1 files changed, 39 insertions, 45 deletions
diff --git a/rater.py b/rater.py
index 247c1d2..6302d64 100755
--- a/rater.py
+++ b/rater.py
@@ -12,7 +12,7 @@ import json
12# urllib3.contrib.pyopenssl.inject_into_urllib3() 12# urllib3.contrib.pyopenssl.inject_into_urllib3()
13 13
14parser = ArgumentParser(description="C3 rating helper") 14parser = ArgumentParser(description="C3 rating helper")
15parser.add_argument("-i", action="store_true", dest="frab_import", default=False, help="import events from frab") 15parser.add_argument("-i", action="store_true", dest="pretalx_import", default=False, help="import events from pretalx")
16parser.add_argument("-c", "--config", help="Config file location", default="./config.json") 16parser.add_argument("-c", "--config", help="Config file location", default="./config.json")
17args = parser.parse_args() 17args = parser.parse_args()
18 18
@@ -20,7 +20,7 @@ with open(args.config, mode="r", encoding="utf-8") as json_file:
20 config = json.load(json_file) 20 config = json.load(json_file)
21 21
22app = Flask(__name__) 22app = Flask(__name__)
23app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + config['frab-conference'] + '-' + config['track'] + '.db' 23app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + config['pretalx-conference'] + '-' + config['track'] + '.db'
24app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False 24app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
25app.config['SECRET_KEY'] = 'Silence is golden. Gerd Eist.' 25app.config['SECRET_KEY'] = 'Silence is golden. Gerd Eist.'
26app.jinja_env.trim_blocks = True 26app.jinja_env.trim_blocks = True
@@ -28,11 +28,12 @@ app.jinja_env.lstrip_blocks = True
28 28
29db = SQLAlchemy(app) 29db = SQLAlchemy(app)
30 30
31config['frab-conf-url'] = config['frab-url'] + config['frab-conference'] 31config['pretalx-conf-url'] = config['pretalx-url'] + 'orga/event/' + config['pretalx-conference']
32config['pretalx-api-url'] = config['pretalx-url'] + 'api/events/' + config['pretalx-conference'] + '/submissions?track_id' + str(config['track-id'])
32 33
33class Event(db.Model): 34class Event(db.Model):
34 """An event as dumped from frab""" 35 """An event as dumped from pretalx"""
35 frab_id = db.Column(db.Integer, primary_key=True) 36 pretalx_id = db.Column(db.String(64), primary_key=True)
36 title = db.Column(db.String(1024)) 37 title = db.Column(db.String(1024))
37 subtitle = db.Column(db.String(1024)) 38 subtitle = db.Column(db.String(1024))
38 abstract = db.Column(db.Text()) 39 abstract = db.Column(db.Text())
@@ -42,12 +43,13 @@ class Event(db.Model):
42 speakers = db.Column(db.String(1024)) 43 speakers = db.Column(db.String(1024))
43 coordinator = db.Column(db.String(1024)) 44 coordinator = db.Column(db.String(1024))
44 notes = db.Column(db.Text()) 45 notes = db.Column(db.Text())
46 duration = db.Column(db.Integer())
45 47
46class EventRating(db.Model): 48class EventRating(db.Model):
47 """A rating as given by a logged in user""" 49 """A rating as given by a logged in user"""
48 id = db.Column(db.Integer, primary_key=True) 50 id = db.Column(db.Integer, primary_key=True)
49 submitter = db.Column(db.String(1024)) 51 submitter = db.Column(db.String(1024))
50 event_id = db.Column(db.Integer, db.ForeignKey('event.frab_id')) 52 pretalx_id = db.Column(db.String(64), db.ForeignKey('event.pretalx_id'))
51 event = db.relationship('Event', backref=db.backref('ratings', lazy='dynamic')) 53 event = db.relationship('Event', backref=db.backref('ratings', lazy='dynamic'))
52 comment = db.Column(db.Text()) 54 comment = db.Column(db.Text())
53 rating_dict = db.Column(db.String(1024), server_default="{}") 55 rating_dict = db.Column(db.String(1024), server_default="{}")
@@ -64,7 +66,7 @@ def get_ratings():
64@app.route('/api/set_event_state/<eventid>', methods=['POST']) 66@app.route('/api/set_event_state/<eventid>', methods=['POST'])
65def set_sevent_state(eventid): 67def set_sevent_state(eventid):
66 content = request.json 68 content = request.json
67 dbevent = Event.query.get(eventid) 69 dbevent = db.session.get(Event, eventid)
68 dbevent.state = content.get('state', 'new') 70 dbevent.state = content.get('state', 'new')
69 db.session.commit() 71 db.session.commit()
70 return jsonify({"result":"ok"}) 72 return jsonify({"result":"ok"})
@@ -72,14 +74,14 @@ def set_sevent_state(eventid):
72@app.route('/api/set_event_coordinator/<eventid>', methods=['POST']) 74@app.route('/api/set_event_coordinator/<eventid>', methods=['POST'])
73def set_sevent_coordinator(eventid): 75def set_sevent_coordinator(eventid):
74 content = request.json 76 content = request.json
75 dbevent = Event.query.get(eventid) 77 dbevent = db.session.get(Event, eventid)
76 dbevent.coordinator = content['coordinator'] 78 dbevent.coordinator = content['coordinator']
77 db.session.commit() 79 db.session.commit()
78 return jsonify({"result":"ok"}) 80 return jsonify({"result":"ok"})
79 81
80@app.route('/api/remove_event/<eventid>', methods=['POST']) 82@app.route('/api/remove_event/<eventid>', methods=['POST'])
81def remove_event(eventid): 83def remove_event(eventid):
82 dbevent = Event.query.get(eventid) 84 dbevent = db.session.get(Event, eventid)
83 if dbevent.state == 'gone': 85 if dbevent.state == 'gone':
84 db.session.delete(dbevent) 86 db.session.delete(dbevent)
85 db.session.commit() 87 db.session.commit()
@@ -88,7 +90,7 @@ def remove_event(eventid):
88@app.route('/api/remove_rating/<eventid>', methods=['POST']) 90@app.route('/api/remove_rating/<eventid>', methods=['POST'])
89def remove_rating(eventid): 91def remove_rating(eventid):
90 content = request.json 92 content = request.json
91 rating = EventRating.query.filter_by(event_id = eventid, submitter = content['author']).first() 93 rating = EventRating.query.filter_by(pretalx_id = eventid, submitter = content['author']).first()
92 if rating: 94 if rating:
93 db.session.delete(rating) 95 db.session.delete(rating)
94 db.session.commit() 96 db.session.commit()
@@ -100,7 +102,7 @@ def add_rating(eventid):
100 print ( str(eventid) + " " + str(content)) 102 print ( str(eventid) + " " + str(content))
101 r = content.get('ratings', '{}'); 103 r = content.get('ratings', '{}');
102 104
103 rating = EventRating.query.filter_by(event_id = eventid, submitter = content['author']).first() 105 rating = EventRating.query.filter_by(pretalx_id = eventid, submitter = content['author']).first()
104 106
105 rd = json.dumps({ k: r.get(k,'0') for k in ['category1', 'category2', 'category3', 'category4'] }) 107 rd = json.dumps({ k: r.get(k,'0') for k in ['category1', 'category2', 'category3', 'category4'] })
106 if rating: 108 if rating:
@@ -108,65 +110,57 @@ def add_rating(eventid):
108 rating.comment = content['comment'] 110 rating.comment = content['comment']
109 rating.rating_dict = rd 111 rating.rating_dict = rd
110 else: 112 else:
111 db.session.add( EventRating( submitter = content.get('author','anonymous'), event_id = eventid, comment = content['comment'], rating_dict = rd)) 113 db.session.add( EventRating( submitter = content.get('author','anonymous'), pretalx_id = eventid, comment = content['comment'], rating_dict = rd))
112 114
113 db.session.commit() 115 db.session.commit()
114 return jsonify({"result":"ok"}) 116 return jsonify({"result":"ok"})
115 117
116def fetch_talks(): 118def fetch_talks():
117 sess = requests.Session() 119 sess = requests.Session()
118 new_session_page = sess.get(config['frab-url']) 120
119 tree = etree.HTML(new_session_page.text) 121 response = sess.get(config['pretalx-api-url'] + '&format=json&limit=20000' , verify=False, stream=True, headers={'Authorization': "Token " + config['pretalx-token']})
120 auth_token = tree.xpath("//meta[@name='csrf-token']")[0].get("content")
121 login_data = dict()
122 login_data['user[email]'] = config['frab-user']
123 login_data['user[password]'] = config['frab-password']
124 login_data['user[remember_me]'] = 1
125 login_data['authenticity_token'] = auth_token
126
127 sess.post(config['frab-url'] + 'users/sign_in?conference_acronym=' + config['frab-conference'] + '&locale=en', login_data, verify=False)
128 response = sess.get(config['frab-conf-url'] + '/events?track_name=' + config['track-name'] + '&format=json', verify=False, stream=True)
129 122
130 talks_json = json.loads(response.text) 123 talks_json = json.loads(response.text)
131 124
132# with open('dump.txt', mode='wb') as localfile: 125 # with open('dump.txt', mode='wb') as localfile:
133# localfile.write(response.content) 126 # localfile.write(response.content)
127
128 submissions = [s for s in talks_json['results'] if s['track_id'] == config['track-id'] ]
134 129
135 imported = 0 130 imported = 0
136 for json_event in talks_json['events']: 131 for json_event in submissions:
137# print (json_event) 132 print (json_event)
138 rawhtml = sess.get(config['frab-conf-url'] + '/events/'+ str(json_event['id']), verify=False, stream=True) 133
139 tree = etree.HTML(rawhtml.text) 134 dbevent = db.session.get(Event, json_event['code'])
140 submission_notes = tree.xpath('//b[text()="Submission Notes(user and admin):"]')[0].tail.strip() 135 speakers = { speaker['code']: speaker['name'] for speaker in json_event['speakers'] }
141 136 submission_type = json_event['submission_type'].get('de') or json_event['submission_type'].get('en')
142 dbevent = Event.query.get(json_event['id'])
143 speakers = { speaker['id']: speaker['full_public_name'] for speaker in json_event['speakers'] }
144 if dbevent: 137 if dbevent:
145 dbevent.title = json_event['title'] 138 dbevent.title = json_event['title']
146 dbevent.subtitle = json_event['subtitle']
147 dbevent.abstract = json_event['abstract'] 139 dbevent.abstract = json_event['abstract']
148 dbevent.description = json_event['description'] 140 dbevent.description = json_event['description']
149 dbevent.event_type = json_event['type'] 141 dbevent.event_type = submission_type
150 dbevent.notes = submission_notes 142 dbevent.notes = json_event.get('notes')
143 dbevent.duration = int(json_event['duration'])
151 if 'state' in json_event: 144 if 'state' in json_event:
152 if json_event['state'] != 'new' or dbevent.state == 'gone': 145 if json_event['state'] != 'submitted' or dbevent.state == 'gone':
153 dbevent.state = json_event['state'] 146 dbevent.state = json_event['state']
154 dbevent.speakers = json.dumps(speakers) 147 dbevent.speakers = json.dumps(speakers)
155 else: 148 else:
156 db.session.add( Event( frab_id = json_event['id'], title = json_event['title'], subtitle = json_event['subtitle'], abstract = json_event['abstract'], description = json_event['description'], speakers = json.dumps(speakers), state = json_event.get('state', 'new'), event_type = json_event['type'], notes = submission_notes) ) 149 db.session.add( Event( pretalx_id = json_event['code'], title = json_event['title'], abstract = json_event['abstract'], description = json_event['description'], speakers = json.dumps(speakers), state = json_event.get('state', 'submitted'), event_type = submission_type, notes = json_event.get('notes'), duration = int(json_event['duration'])) )
157 imported += 1 150 imported += 1
158 151
159 for goner in Event.query.filter( Event.frab_id.notin_([ ev['id'] for ev in talks_json['events'] ])).all(): 152 for goner in Event.query.filter( Event.pretalx_id.notin_([ ev['code'] for ev in submissions ])).all():
160 goner.state = 'gone' 153 goner.state = 'gone'
161 154
162 db.session.commit() 155 db.session.commit()
163 print ('Conference: ' + config['track'] + ', track: ' + config['track'] + ', imported ' + str(len(talks_json['events'])) + ' events, ' + str(imported) + ' new.') 156 print ('Conference: ' + config['track'] + ', track: ' + config['track'] + ', imported ' + str(len(submissions)) + ' events, ' + str(imported) + ' new.')
164 157
165 158
166if __name__ == "__main__": 159if __name__ == "__main__":
167 db.create_all() 160 with app.app_context():
168 if args.frab_import: 161 db.create_all()
169 fetch_talks() 162 if args.pretalx_import:
170 else: 163 fetch_talks()
171 app.run(host=config.get('host', '127.0.0.1'), port=int(config.get('port', '8080'))) 164 else:
165 app.run(host=config.get('host', '127.0.0.1'), port=int(config.get('port', '8080')))
172 166