From 27f35be02c37ce6dc9ffef15d6be92092f2bb350 Mon Sep 17 00:00:00 2001
From: Andreas Valder <andreasv@fsmpi.rwth-aachen.de>
Date: Sun, 4 Sep 2016 02:22:24 +0200
Subject: [PATCH] import fixes

---
 server.py                    | 21 +++++++++++++--------
 templates/import_campus.html | 12 ++++++++++++
 2 files changed, 25 insertions(+), 8 deletions(-)

diff --git a/server.py b/server.py
index e89b7a4..762dfaa 100755
--- a/server.py
+++ b/server.py
@@ -328,9 +328,6 @@ def log():
 @mod_required
 def import_from(source=None, id=None):
 
-	def recursive_dict(element):
-		     return element.tag, dict(map(recursive_dict, element)) or element.text
-
 	if source != "campus":
 		return "Unknown source", 404
 
@@ -355,6 +352,7 @@ def import_from(source=None, id=None):
 				query('DELETE FROM import_campus WHERE (id = ?) AND (course_id = ?)',int(i),id)
 	
 	import_campus = query('SELECT * FROM import_campus WHERE course_id = ?',id)
+	events = []
 
 	try:
 		from lxml import html
@@ -394,7 +392,6 @@ def import_from(source=None, id=None):
 
 			# parse single appointments
 			singletable = basetable.xpath("//table[@cellpadding='3']/tr/td[text()='Einmalige Termine:']")[0].getparent().getparent()
-#			i['single']=html.tostring(singletable)
 			for row in singletable.xpath("tr/td[2]"):
 				rowdata = {}
 				rowdata['place'] = row.xpath("text()[2]")[0][2:-1]
@@ -404,7 +401,6 @@ def import_from(source=None, id=None):
 				events_raw.append(rowdata)
 
 			#now we have to filter our data and do some lookups
-			events = []
 			for j in events_raw:
 				for k in j['dates']:
 					e = {}
@@ -412,12 +408,21 @@ def import_from(source=None, id=None):
 					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)
 					e['place'] = query("SELECT name FROM places WHERE (campus_name = ?) OR ((NOT campus_name) AND name = ?)",j['place'],j['place'])[0]['name'];
+					e['exists'] = len(query("SELECT id from lectures WHERE (time = ?) and (duration = ?) and (place = ?) and (course_id = ?)",e['time'],e['duration'],e['place'],id)) > 0
 					events.append(e)
 			# it is pared.
-			i['events'] = events
+
+
 
 	except ImportError:
 		flash('python-lxml not found, campus import will not work.')
-	
 
-	return render_template('import_campus.html', course=courses, lectures=lectures, import_campus=import_campus)
+	uniqevents = []
+	for i in events:
+		seen = False
+		for j in events:
+			seen = (i['place'] == j['place']) and (i['time'] == j['time']) and (i['duration'] == j['duration'])
+		if not seen:
+			uniqevents.append(i)
+
+	return render_template('import_campus.html', course=courses, lectures=lectures, import_campus=import_campus, events=uniqevents)
diff --git a/templates/import_campus.html b/templates/import_campus.html
index 6df280d..5a5b036 100644
--- a/templates/import_campus.html
+++ b/templates/import_campus.html
@@ -55,5 +55,17 @@
 			</div>
 		</div>
 	</div>
+	<div class="panel panel-default">
+		<div class="panel-heading">
+			<h1 class="panel-title">Fehlende Termine:</h1>
+		</div>
+		<ul class="list-group-item">
+			{% for i in events if not i.exists %}
+				<li class="list-group-item">
+					{{i|pprint}}
+				</li>
+			{% endfor %}
+		</ul>
+	</div>
 </div>
 {% endblock %}
-- 
GitLab