#-*- mode: python; mode: fold -*-
# (C) Copyright 2005 The WAeUP group  <http://www.waeup.org>
# Author: Joachim Schmitz (js@aixtraware.de)
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
# 02111-1307, USA.
#
# $Id: SetupDemoDB.py 1882 2007-06-12 08:18:16Z henrik $
"""
remove Data from live db to make a demodb
"""
import random
import transaction
import logging
logger = logging.getLogger('Extension.SetupDemoDB.removeStudents')

def removeStudents(self):
    "remove nearly all students to form a lightweight demodb"
    context = self.uniben
    wftool = context.portal_workflow
    mtool = context.portal_membership
    member = mtool.getAuthenticatedMember()
    roles = member.getRolesInContext(context)
    stdir = context.portal_directories.students
    #students_catalog = context.students_catalog
    students = context.portal_url.getPortalObject().campus.students
    step = 5
    #all = students_catalog()
    all=students.objectIds()
    todel = (len(all)/10)
    sample = random.sample(all,todel)
    logger.info("starting to delete %d students" % todel)
    for i in range(0,len(sample),step):
        ids = [b  for b in sample[i:i + step]]
        logger.info("deleting students %s" % (ids))
        for sid in ids:
            if hasattr(stdir, sid):
                stdir.deleteEntry(sid)
        try:
            students.manage_delObjects(ids)
        except:
            logger.info("error deleting %s" % (ids))
        logger.info("deleted %d students %s" % (step,ids))
        transaction.commit()
