School.py

For an initial export of the whole school with all school classes within their students the method addToLDAP() was added to class School. Also additionally a same named method addToLDAP() was added to class SchoolClass, which is invoked by the first one. It creates a single school class in Active Directory.

Also the method SchoolClass.deleteFromLDAP() was added, which deletes a school class with students from the Active Directory: 

 1  
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from Settings import Settings
from Ldap import PyAD
from CSV import Csv
from User import Student

class School:                           
    def __init__(self,schoolClasses):
        self.__schoolClasses = schoolClasses
        
    def getSchoolClasses(self):
        return self.__schoolClasses

    def addSchoolClass(self, schoolClass):        
        self.__schoolClasses.append(schoolClass)

    def importStudentsFromCSV(self, filename):    # import students of a certain csv file 
        csv = Csv(filename)
        classNames = csv.importClassNames()       # import names of school classes

        for className in classNames:
            schoolClass = SchoolClass(className,[])
            listStudents = csv.importStudentsOfClass(className) # import student data of a certain name of school class 
            for listStudent in listStudents:
                student = Student(listStudent[0],listStudent[1],listStudent[2],schoolClass)
            self.addSchoolClass(schoolClass)

    def addToLDAP(self):    # initial export of whole school to Active Directory
        for schoolClass in self.__schoolClasses:
            schoolClass.addToLDAP()
    
    
class SchoolClass:
    def __init__(self,name, students):
        self.__name = name
        self.__students = students                
        
    def getName(self):
        return self.__name

    def getStudents(self):
       return self.__students

    def addStudent(self, student):                
        self.__students.append(student)

    def addToLDAP(self):        # export school class with students to Active Directory
        ldap = PyAD(Settings.dnStudents)
        ldap.createSchoolClass(self.__name)
        for student in self.__students:
            student.addToLDAP()

    def deleteFromLDAP(self):   # delete school class with students from Active Directory
        ldap = PyAD(Settings.dnStudents)
        for student in self.__students:
            student.deleteFromLDAP()
        ldap.deleteSchoolClass(self.__name)


User.py

The class Student have got both methods addToLDAP() and deleteFromLDAP(), which are called from the same named methods of class SchoolClass:

 1  
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from Settings import Settings
from Ldap import PyAD

class User:                     # superclass "User"
    def __init__(self, surname, firstname, dateOfBirth):   
        self._surname = surname                            
        self._firstname = firstname
        self._password = firstname[0] + surname[0] + dateOfBirth  
        self._dateOfBirth = dateOfBirth
        self._username = surname 
        
    def getSurname(self):                                  
        return self._surname
    
    def getFirstname(self):
        return self._firstname

    def getPassword(self):
        return self._password

    def getDateOfBirth(self):
        return self._DateOfBirth

    def getUsername(self):
        return self._username


class Student(User):            # subclass "Student" 
    def __init__(self, surname, firstname, dateOfBirth, schoolClass):
        User.__init__(self, surname, firstname, dateOfBirth)  
        schoolClass.addStudent(self);   
        self.__schoolClass = schoolClass

    def getSchoolClass(self):              
        return self.__schoolClass

    def addToLDAP(self):        # adds student to ldap
        ldap = PyAD(Settings.dnStudents)
        # ldap.createStudent() returns the possibly modified username for updating
        self._username = ldap.createStudent(self._username
                               , self._surname
                               , self._firstname
                               , self._password
                               , self._dateOfBirth
                               , self.__schoolClass.getName())
    
    def deleteFromLDAP(self):   # remove student from ldap
        ldap = PyAD(Settings.dnStudents)
        ldap.deleteStudent(self._surname, self._firstname, self._dateOfBirth, self.__schoolClass.getName())      


Ldap.py and Settings.py are unchanged and were alread described in Solutions: Improvements of class PyAD.

testPyAD.py

The above defined functionality is used with the following code:

 1  
 2
 3
 4
 5
 6
 7
 8
 9
10
11
from School import *                         # import classes
from User import *

school = School([])
schoolClass = SchoolClass("school_class1",[])         
student1 = Student("Miller", "Sam", "11.11.2000", schoolClass)  
student2 = Student("Miller", "Paula", "10.10.2001", schoolClass)

school.addSchoolClass(schoolClass)
school.addToLDAP()  # exports whole school to AD
# schoolClass.deleteFromLDAP()   # deletes school class from AD

The result on AD is:

Two new students in AD

Two new students in AD


Finally a look at the UML sequence diagram again:


UML sequence diagram of adding students to AD

UML sequence diagram of adding students to AD


This programme now forms the basis for action-oriented assignments, because the students' competences should now be developed.

Last modified: Sunday, 16 April 2023, 12:12 PM