diff --git a/importer.py b/importer.py index b3d3bd3ecb5004351055669ab3733e44adb3e2f0..50e17152d16fff9ac07b32ba57945b88ab0194ac 100644 --- a/importer.py +++ b/importer.py @@ -68,7 +68,15 @@ def import_from(id): continue baserow = baserow[0] rowdata = {'dates': []} - rowdata['place'] = baserow.xpath("td[6]/text()")[0][2:-1] + + # "kein raum vergeben" ist ein Sonderfall, ansonsten die campus id + if baserow.xpath("td[6]/text()")[0] == 'Kein Raum vergeben': + rowdata['place'] = '' + elif baserow.xpath("td[6]/a"): + rowdata['place'] = baserow.xpath("td[6]/a")[0].text_content() + else: + rowdata['place'] = baserow.xpath("td[6]/text()")[0].split(' ',1)[0] + rowdata['start'] = baserow.xpath("td[3]/text()")[0] rowdata['end'] = baserow.xpath("td[5]/text()")[0] rowdata['dates'] = baserow.getparent().xpath("tr[@class='hierarchy5']//td[@colspan='3']/text()") @@ -79,7 +87,13 @@ def import_from(id): singletable = basetable.xpath("//table[@cellpadding='3']/tr/td[text()='Einmalige Termine:']")[0].getparent().getparent() for row in singletable.xpath("tr/td[2]"): rowdata = {} - rowdata['place'] = row.xpath("text()[2]")[0][2:-1] + if row.xpath("text()[2]")[0] == 'Kein Raum vergeben': + rowdata['place'] = '' + elif row.xpath("a"): + rowdata['place'] = row.xpath("a")[0].text_content() + else: + rowdata['place'] = row.xpath("text()[2]")[0].split(' ',1)[0] + rowdata['dates'] = [row.xpath("text()[1]")[0][4:14]] rowdata['start'] = row.xpath("text()[1]")[0][17:22] rowdata['end'] = row.xpath("text()[1]")[0][27:32] @@ -92,11 +106,15 @@ def import_from(id): fmt= "%d.%m.%Y %H:%M" e['time'] = datetime.strptime("%s %s"%(k,j['start']) ,fmt) e['duration'] = int((datetime.strptime("%s %s"%(k,j['end']) ,fmt) - e['time']).seconds/60) - dbplace = query("SELECT name FROM places WHERE (campus_name = ?) OR ((NOT campus_name) AND name = ?)",j['place'],j['place']) - if dbplace: - e['place'] = dbplace[0]['name'] + j['place'] = str(j['place']) + if j['place'] != '': + dbplace = query("SELECT name FROM places WHERE (campus_room = ?) OR (campus_name = ?) OR ((NOT campus_name) AND name = ?)",j['place'],j['place'],j['place']) + if dbplace: + e['place'] = dbplace[0]['name'] + else: + e['place'] = 'Unbekannter Ort ('+j['place']+')' else: - e['place'] = 'Unbekannter Ort ('+j['place']+')' + e['place'] = '' e['title'] = i['type'] events.append(e) # it is parsed. @@ -106,11 +124,12 @@ def import_from(id): except ImportError: flash('python-lxml not found, campus import will not work.') - uniqueevents = [] + # events to add + newevents = [] for i in events + lectures: unique = False exists = False - for j in uniqueevents: + for j in newevents: unique = (i['place'] == j['place']) and (i['time'] == j['time']) and (i['duration'] == j['duration']) if unique: break @@ -119,7 +138,17 @@ def import_from(id): if exists: break if (not unique) and (not exists): - i['type'] = 'import' - uniqueevents.append(i) + newevents.append(i) + + # deleted events + deletedlectures = [] + for i in lectures: + incampus = False + for j in events: + incampus = (i['place'] == j['place']) and (i['time'] == j['time']) and (i['duration'] == j['duration']) + if incampus: + break + if not incampus: + deletedlectures.append(i) - return render_template('import_campus.html', course=courses, import_campus=import_campus, events=uniqueevents) + return render_template('import_campus.html', course=courses, import_campus=import_campus, newevents=newevents, deletedlectures=deletedlectures) diff --git a/templates/import_campus.html b/templates/import_campus.html index 730e56493a66a436b94e33b8ca3184ae820e0327..35bce93f997970fb28e183d813eb6a440ce302a0 100644 --- a/templates/import_campus.html +++ b/templates/import_campus.html @@ -41,7 +41,10 @@ </span> </li> </ul> - <button class="btn btn-default pull-right" type="submit">Speichern</button> + <ul class="list-inline pull-right"> + <li><a class="btn btn-default" href="{{url_for('import_from', id=course['id'])}}"><span class="fa fa-refresh" aria-hidden="true" ></span> Campus abfragen</button></a></li> + <li><button class="btn btn-default" type="submit">Speichern</button></li> + </ul> </form> </div> </div> @@ -49,15 +52,57 @@ <div class="panel panel-default"> <div class="panel-heading" style="height:50px"> <h1 class="panel-title"> - Fehlende Termine: + Termine im Campus die nicht bei uns sind: <ul class="list-inline pull-right"> - <li><a class="btn btn-default" href="{{url_for('import_from', id=course['id'])}}"><span class="fa fa-refresh" aria-hidden="true" ></span> Aktualisieren</button></a> <li><button class="btn btn-default" onclick="$('.newlecture').click(); alert('done');">alle anlegen</button></li> </ul> </h1> </div> <ul class="list-group-item"> - {% for i in events|sort(attribute='time') %} + {% for i in newevents|sort(attribute='time') %} + <li class="list-group-item row"> + <span class="col-xs-3"> + Time: {{i.time}} + </span> + <span class="col-xs-2"> + Duration: {{i.duration}} + </span> + <span class="col-xs-3"> + Place: {{i.place}} + </span> + <span class="col-xs-3"> + </span> + <span class="pull-right"> + <button class="btn btn-default newlecture" onclick="moderator.api.gethttp('{{ url_for('create', table='lectures', course_id=course.id, time=i.time, title=i.title, place=i.place) }}')">anlegen</a> + </span> + </li> + {% endfor %} + </ul> + </div> + <div class="panel panel-default"> + <div class="panel-heading" style="height:50px"> + <h1 class="panel-title"> + Termine bei uns die nicht im Campus sind: + <ul class="list-inline pull-right"> + <li><button class="btn btn-default" onclick="deleteall(); alert('done');">alle entfernen</button></li> + <script> + function deleteall() { + var req = {}; + + {% for i in deletedlectures|sort(attribute='time') %} + req['lectures.{{ i.id }}.deleted'] = 1; + {% endfor %} + + if (confirm('Really delete all lectures that are not in campus?')) { + moderator.api.set_multi(req,false); + } + } + </script> + </ul> + </h1> + </div> + <ul class="list-group-item"> + {% for i in deletedlectures|sort(attribute='time') %} <li class="list-group-item row"> <span class="col-xs-3"> Time: {{i.time}} @@ -69,26 +114,20 @@ Place: {{i.place}} </span> <span class="col-xs-3"> - {% if (i.type == 'lecture') %} <p> {{i.comment}} </p> <p> {{i.internal}} </p> - {% endif%} </span> <span class="pull-right"> - {% if (i.type == 'lecture') %} - {{ moderator_delete(['lectures',i.id,'deleted']) }} - {% endif%} - {% if (i.type == 'import') %} - <button class="btn btn-default newlecture" onclick="moderator.api.gethttp('{{ url_for('create', table='lectures', course_id=course.id, time=i.time, title=i.title, place=i.place) }}')">anlegen</a> - {% endif%} + {{ moderator_delete(['lectures',i.id,'deleted']) }} </span> </li> {% endfor %} </ul> + </div> </div> {% endblock %}