Changeset 13449 for main


Ignore:
Timestamp:
12 Nov 2015, 17:03:44 (9 years ago)
Author:
uli
Message:

Scripts for orphaned beds.

As it turns out, we can change a local ZODB from the commandline. And
it is even not too complicated.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.uniben/trunk/src/waeup/uniben/scripts.py

    r13210 r13449  
    22
    33To make this work, you have to pip install psutil in your local virtualenv.
     4
     5Other functions can be called from the commandline. These comprise:
     6
     7  - `bedless_students`
     8  - `remove_orphan_beds`
     9
     10Once, you have installed uniben, you can use `bin/python-console`::
     11
     12
     13  $ ,/bin/python-console
     14  >>> from waeup.uniben.scripts import bedless_students
     15  >>> studs = [x for x in bedless_students()]
     16  ...  lots of output ...
     17  ...  the cmd takes some time to finish ...
     18  ...  please be patient ...
     19  >>> len(studs)
     20  196
     21
     22etc. Use Ctrl-D to quit.
     23
    424"""
    525import argparse
     
    929import tempfile
    1030import time
     31import transaction
    1132from ZODB import DB, DemoStorage, FileStorage
    1233from ZODB.blob import BlobStorage
     
    5879
    5980
    60 def init_dbs():
     81def init_dbs(read_only=True):
    6182    """Setup databases.
    6283
     
    7495    db_path = os.path.join(db_dir, 'Data.fs')
    7596    async_db_path = os.path.join(db_dir, 'Data.async.fs')
    76     async_storage = FileStorage.FileStorage(async_db_path, read_only=True)
     97    async_storage = FileStorage.FileStorage(async_db_path, read_only=read_only)
    7798    db1 = DB(async_storage, database_name="async", databases=databases)
    78     base_storage = FileStorage.FileStorage(db_path, read_only=True)
     99    base_storage = FileStorage.FileStorage(db_path, read_only=read_only)
    79100    blob_storage = BlobStorage(blob_dir, base_storage)
    80101    db2 = DB(blob_storage, databases=databases)
     
    200221    t2 = time.time()
    201222    print("Elapsed: %s secs" % (t2 - t1))
     223
     224
     225def bedless_students(university_inst=None):
     226    """Get students with beds gone.
     227    """
     228    conn, closeables = None, None
     229    if university_inst is None:
     230        closeables = init_dbs(read_only=False)
     231        conn = closeables[0].open()
     232        university_inst = get_university(conn)
     233    for stud_id in university_inst['students']:
     234        stud = university_inst['students'][stud_id]
     235        if not 'accommodation' in stud.keys():
     236            continue
     237        accomm = stud['accommodation']
     238        for bed_ticket_id in accomm.keys():
     239            ticket = accomm[bed_ticket_id]
     240            bed = ticket.bed
     241            if bed is None:
     242                continue
     243            if getattr(
     244                getattr(bed, '__parent__', None),
     245                    '__parent__', None) is None:
     246                yield stud_id, ticket, bed
     247    if conn is not None:
     248        conn.close()
     249        close_dbs(closeables)
     250
     251
     252def remove_orphan_beds():
     253    """Delete orphaned beds.
     254    """
     255    closeables = init_dbs(read_only=False)
     256    conn = closeables[0].open()
     257    uni = get_university(conn)
     258    for stud_id, bed_ticket, bed in bedless_students(uni):
     259        print("DELETE bed of student %s" % stud_id)
     260        bed_ticket.bed = None
     261        del bed
     262    transaction.commit()
     263    conn.close()
     264    close_dbs(closeables)
Note: See TracChangeset for help on using the changeset viewer.