Index: /main/waeup.uniben/trunk/src/waeup/uniben/scripts.py
===================================================================
--- /main/waeup.uniben/trunk/src/waeup/uniben/scripts.py	(revision 13448)
+++ /main/waeup.uniben/trunk/src/waeup/uniben/scripts.py	(revision 13449)
@@ -2,4 +2,24 @@
 
 To make this work, you have to pip install psutil in your local virtualenv.
+
+Other functions can be called from the commandline. These comprise:
+
+  - `bedless_students`
+  - `remove_orphan_beds`
+
+Once, you have installed uniben, you can use `bin/python-console`::
+
+
+  $ ,/bin/python-console
+  >>> from waeup.uniben.scripts import bedless_students
+  >>> studs = [x for x in bedless_students()]
+  ...  lots of output ...
+  ...  the cmd takes some time to finish ...
+  ...  please be patient ...
+  >>> len(studs)
+  196
+
+etc. Use Ctrl-D to quit.
+
 """
 import argparse
@@ -9,4 +29,5 @@
 import tempfile
 import time
+import transaction
 from ZODB import DB, DemoStorage, FileStorage
 from ZODB.blob import BlobStorage
@@ -58,5 +79,5 @@
 
 
-def init_dbs():
+def init_dbs(read_only=True):
     """Setup databases.
 
@@ -74,7 +95,7 @@
     db_path = os.path.join(db_dir, 'Data.fs')
     async_db_path = os.path.join(db_dir, 'Data.async.fs')
-    async_storage = FileStorage.FileStorage(async_db_path, read_only=True)
+    async_storage = FileStorage.FileStorage(async_db_path, read_only=read_only)
     db1 = DB(async_storage, database_name="async", databases=databases)
-    base_storage = FileStorage.FileStorage(db_path, read_only=True)
+    base_storage = FileStorage.FileStorage(db_path, read_only=read_only)
     blob_storage = BlobStorage(blob_dir, base_storage)
     db2 = DB(blob_storage, databases=databases)
@@ -200,2 +221,44 @@
     t2 = time.time()
     print("Elapsed: %s secs" % (t2 - t1))
+
+
+def bedless_students(university_inst=None):
+    """Get students with beds gone.
+    """
+    conn, closeables = None, None
+    if university_inst is None:
+        closeables = init_dbs(read_only=False)
+        conn = closeables[0].open()
+        university_inst = get_university(conn)
+    for stud_id in university_inst['students']:
+        stud = university_inst['students'][stud_id]
+        if not 'accommodation' in stud.keys():
+            continue
+        accomm = stud['accommodation']
+        for bed_ticket_id in accomm.keys():
+            ticket = accomm[bed_ticket_id]
+            bed = ticket.bed
+            if bed is None:
+                continue
+            if getattr(
+                getattr(bed, '__parent__', None),
+                    '__parent__', None) is None:
+                yield stud_id, ticket, bed
+    if conn is not None:
+        conn.close()
+        close_dbs(closeables)
+
+
+def remove_orphan_beds():
+    """Delete orphaned beds.
+    """
+    closeables = init_dbs(read_only=False)
+    conn = closeables[0].open()
+    uni = get_university(conn)
+    for stud_id, bed_ticket, bed in bedless_students(uni):
+        print("DELETE bed of student %s" % stud_id)
+        bed_ticket.bed = None
+        del bed
+    transaction.commit()
+    conn.close()
+    close_dbs(closeables)
