Beispiel #1
0
 def test_get_roles(self):
     smith_school_membership = Membership(
         person=self.smith,
         entity=self.school,
         importance_to_person=5,
         importance_to_entity=5,
         role="Dean",
     )
     smith_school_membership.save()
     people = [self.smith]
     self.assertEquals(self.school.get_roles_for_members(people),
                       [self.smith])
Beispiel #2
0
    def setUp(self):

        p1 = Publication(guid="test-publication-1")
        p1.save()
        self.br1 = BibliographicRecord(
            publication=p1,
            publication_date=datetime.now()-timedelta(days=10)
            )
        self.br1.save()

        p2 = Publication(guid="test-publication-2")
        p2.save()
        self.br2 = BibliographicRecord(
            publication=p2,
            publication_date=datetime.now()-timedelta(days=20)
            )
        self.br2.save()

        e = Entity()
        e.save()

        p = Person()
        p.save()

        m = Membership(
            person=p,
            entity=e
            )
        m.save()

        r = Researcher(person=p)
        r.save()

        a2 = Authored(
            is_a_favourite=True, publication=p2, researcher=r,
            bibliographic_record=self.br2
            )
        a2.save()

        self.itemlist = PublicationsList()
        self.itemlist.items = BibliographicRecord.objects.all()

        self.ph = Placeholder(slot=u"some_slot")
        self.ph.save()
        self.pl = add_plugin(
            self.ph,
            u"CMSPublicationsPlugin",
            u"en",
            entity=e
            )
        self.pl.save()
 def test_get_roles(self):
     smith_school_membership = Membership(
         person=self.smith,
         entity=self.school,
         importance_to_person=5,
         importance_to_entity=5,
         role="Dean",
         )
     smith_school_membership.save()
     people = [self.smith]
     self.assertEquals(
         self.school.get_roles_for_members(people),
         [self.smith]
         )
Beispiel #4
0
    def test_person_with_abstract_entity_and_real_entity_memberships(self):
        #  smith's best entity so far is technician in the department
        smith_web_editor_membership = Membership(
            person=self.smith,
            entity=self.web_editors,
            importance_to_person=5,
            importance_to_entity=4,
            role="Lead web editor",
        )
        smith_web_editor_membership.save()

        smith_department_membership = Membership(
            person=self.smith,
            entity=self.department,
            # less important than his other one
            importance_to_entity=4,
            role="Technician",
        )
        smith_department_membership.save()

        self.assertEquals(list(self.smith.real_entity_memberships),
                          [smith_department_membership])
        self.assertEquals(self.smith.get_role, smith_department_membership)
        self.assertEquals(self.smith.get_entity, self.department)
        self.assertEquals(self.smith.get_building, self.main_building)
        self.assertEquals(self.smith.get_full_address, [
            self.school.name, u'Main Building', u"St Mary's Street", u'Cardiff'
        ])
    def test_person_with_abstract_entity_memberships(self):
        #  smith is a web editor; only has a membership of an abstract entity
        smith_web_editor_membership = Membership(
            person=self.smith,
            entity=self.web_editors,
            importance_to_person=5,
            importance_to_entity=4,
            role="Lead web editor",
            )
        smith_web_editor_membership.save()

        self.assertEquals(list(self.smith.real_entity_memberships), [])
        self.assertEquals(self.smith.get_role, None)
        self.assertEquals(self.smith.get_entity, None)
        self.assertEquals(self.smith.get_building, None)
        self.assertEquals(self.smith.get_full_address, [])
Beispiel #6
0
    def test_person_with_abstract_entity_memberships(self):
        #  smith is a web editor; only has a membership of an abstract entity
        smith_web_editor_membership = Membership(
            person=self.smith,
            entity=self.web_editors,
            importance_to_person=5,
            importance_to_entity=4,
            role="Lead web editor",
        )
        smith_web_editor_membership.save()

        self.assertEquals(list(self.smith.real_entity_memberships), [])
        self.assertEquals(self.smith.get_role, None)
        self.assertEquals(self.smith.get_entity, None)
        self.assertEquals(self.smith.get_building, None)
        self.assertEquals(self.smith.get_full_address, [])
Beispiel #7
0
    def setUp(self):
        p1 = Publication(guid="test-publication-1")
        p1.save()
        self.br1 = BibliographicRecord(
            publication=p1,
            publication_date=datetime.now()-timedelta(days=10)
            )
        self.br1.save()

        p2 = Publication(guid="test-publication-2")
        p2.save()
        self.br2 = BibliographicRecord(
            publication=p2,
            publication_date=datetime.now()-timedelta(days=20)
            )
        self.br2.save()

        e = Entity()
        e.save()

        p = Person()
        p.save()

        m = Membership(
            person=p,
            entity=e
            )
        m.save()

        r = Researcher(person=p)
        r.save()

        a2 = Authored(
            is_a_favourite=True, publication=p2, researcher=r,
            bibliographic_record=self.br2, visible=True
            )
        a2.save()

        self.itemlist = PublicationsList()
        self.itemlist.items = BibliographicRecord.objects.all()
        self.e = e
    def test_person_with_abstract_entity_and_real_entity_memberships(self):
        #  smith's best entity so far is technician in the department
        smith_web_editor_membership = Membership(
            person=self.smith,
            entity=self.web_editors,
            importance_to_person=5,
            importance_to_entity=4,
            role="Lead web editor",
            )
        smith_web_editor_membership.save()

        smith_department_membership = Membership(
            person=self.smith,
            entity=self.department,
            # less important than his other one
            importance_to_entity=4,
            role="Technician",
            )
        smith_department_membership.save()

        self.assertEquals(
            list(self.smith.real_entity_memberships),
            [smith_department_membership]
            )
        self.assertEquals(self.smith.get_role, smith_department_membership)
        self.assertEquals(self.smith.get_entity, self.department)
        self.assertEquals(self.smith.get_building, self.main_building)
        self.assertEquals(
            self.smith.get_full_address,
            [self.school, u'Main Building', u"St Mary's Street", u'Cardiff']
            )
    def test_person_with_better_entity_membership(self):
        #  now smith has a better entity: school
        smith_web_editor_membership = Membership(
            person=self.smith,
            entity=self.web_editors,
            importance_to_person=5,
            importance_to_entity=4,
            role="Lead web editor",
            )
        smith_web_editor_membership.save()

        smith_department_membership = Membership(
            person=self.smith,
            entity=self.department,
            # less important than his other one
            importance_to_person=2,
            importance_to_entity=4,
            role="Technician",
            )
        smith_department_membership.save()

        smith_school_membership = Membership(
            person=self.smith,
            entity=self.school,
            importance_to_person=5,
            importance_to_entity=5,
            role="Dean",
            )
        smith_school_membership.save()

        self.assertEquals(
            list(self.smith.real_entity_memberships),
            [smith_school_membership, smith_department_membership]
            )
        self.assertEquals(self.smith.get_role, smith_school_membership)
        self.assertEquals(self.smith.get_entity, self.school)
        self.assertEquals(self.smith.get_building, self.main_building)
        self.assertEquals(
            self.smith.get_full_address,
            [u'Main Building', u"St Mary's Street", u'Cardiff']
            )

        #  now smith's best entity will be department
        smith_department_membership.importance_to_person = 5
        smith_department_membership.save()

        self.assertEquals(
            list(self.smith.real_entity_memberships),
            [smith_school_membership, smith_department_membership]
            )
        self.assertEquals(self.smith.get_role, smith_department_membership)
        self.assertEquals(self.smith.get_entity, self.department)
        self.assertEquals(self.smith.get_building, self.main_building)
        self.assertEquals(
            self.smith.get_full_address,
            [self.school, u'Main Building', u"St Mary's Street", u'Cardiff']
            )
        #  check his membership of school has been downgraded by the save()
        self.assertEquals(
            Membership.objects.get(
                pk=smith_school_membership.pk
                ).importance_to_person,
            4
        )
Beispiel #10
0
    def test_person_with_better_entity_membership(self):
        #  now smith has a better entity: school
        smith_web_editor_membership = Membership(
            person=self.smith,
            entity=self.web_editors,
            importance_to_person=5,
            importance_to_entity=4,
            role="Lead web editor",
        )
        smith_web_editor_membership.save()

        smith_department_membership = Membership(
            person=self.smith,
            entity=self.department,
            # less important than his other one
            importance_to_person=2,
            importance_to_entity=4,
            role="Technician",
        )
        smith_department_membership.save()

        smith_school_membership = Membership(
            person=self.smith,
            entity=self.school,
            importance_to_person=5,
            importance_to_entity=5,
            role="Dean",
        )
        smith_school_membership.save()

        self.assertEquals(
            list(self.smith.real_entity_memberships),
            [smith_school_membership, smith_department_membership])
        self.assertEquals(self.smith.get_role, smith_school_membership)
        self.assertEquals(self.smith.get_entity, self.school)
        self.assertEquals(self.smith.get_building, self.main_building)
        self.assertEquals(self.smith.get_full_address,
                          [u'Main Building', u"St Mary's Street", u'Cardiff'])

        #  now smith's best entity will be department
        smith_department_membership.importance_to_person = 5
        smith_department_membership.save()

        self.assertEquals(
            list(self.smith.real_entity_memberships),
            [smith_school_membership, smith_department_membership])
        self.assertEquals(self.smith.get_role, smith_department_membership)
        self.assertEquals(self.smith.get_entity, self.department)
        self.assertEquals(self.smith.get_building, self.main_building)
        self.assertEquals(self.smith.get_full_address, [
            self.school.name, u'Main Building', u"St Mary's Street", u'Cardiff'
        ])
        #  check his membership of school has been downgraded by the save()
        self.assertEquals(
            Membership.objects.get(
                pk=smith_school_membership.pk).importance_to_person, 4)
Beispiel #11
0
def upload_students(request):

    # empty defaults
    uploaded_document = None
    student_formset = StudentFormset(prefix="student")
    upload_form = DocumentForm()
    supervisor_formsets = [SupervisorFormset(prefix="%s-supervisor" % student.prefix) for student in student_formset]

    if request.method == "POST":
        # we're receiving the uploaded file
        if request.POST.get("upload"):
            upload_form = DocumentForm(request.POST, request.FILES)

            # receive the CSV file
            if upload_form.is_valid():
                uploaded_document = request.FILES["docfile"]
                uploaded_document.rows = unicodecsv.DictReader(uploaded_document, errors="replace")

                # get a list of unique students from the uploaded document
                unique_students = csv_to_list(uploaded_document.rows)

                # build a formset for each one
                unique_students, student_formset = convert_to_formset(unique_students)

                # and for each one a formset of supervisors
                student_formset, supervisor_formsets = process_formset(unique_students, student_formset)

        # we're processing the uploaded file
        if request.POST.get("reprocess"):

            student_formset = StudentFormset(request.POST, request.FILES, prefix="student")

            # there will be a supervisor_formset for each student
            supervisor_formsets = []

            # make a mutable copy of the formset data
            student_formset.data = student_formset.data.copy()

            # loop over students
            for student in student_formset:

                # create the supervisor_formset for this student
                # the prefix matches the prefixes in the form, so that the
                # correct supervisors can be matched to the student
                supervisor_formset = SupervisorFormset(
                    request.POST, request.FILES, prefix="%s-supervisor" % student.prefix
                )

                # make the supervisor_formset available on the student form for
                # processing in forms.py, and likewise the student form on the
                # supervisor_formset
                student.supervisor_formset = supervisor_formset
                supervisor_formset.student = student

                # make a mutable copy of the formset data
                supervisor_formset.data = supervisor_formset.data.copy()

                for supervisor in supervisor_formset:
                    supervisor.is_valid()

                    for k, v in supervisor.cleaned_data.items():
                        supervisor_formset.data[supervisor.prefix + "-" + k] = v

                # rebind the data to the formset
                supervisor_formset = SupervisorFormset(supervisor_formset.data, prefix="%s-supervisor" % student.prefix)

                # add this supervisor_formset to the list of formsets
                supervisor_formsets.append(supervisor_formset)

                # check the supervisors
                supervisor_formset.is_valid()

                # student.is_ready_to_save() calls is_valid() on both the
                # student form and the supervisor formset
                ready_to_save = student.is_ready_to_save()

                if ready_to_save and student.cleaned_data.get("confirm"):

                    cleaned_data = student.cleaned_data

                    # if the Person is provided in form, great - if not, we
                    # have to create a new one based on the name provided
                    person = cleaned_data.get("person")

                    if not person:
                        given_name = cleaned_data["given_name"]
                        surname = cleaned_data["surname"]
                        slug = cleaned_data["slug"]
                        email = cleaned_data["email"]
                        institutional_username = cleaned_data["username"]

                        person = Person(
                            given_name=given_name,
                            surname=surname,
                            slug=slug,
                            email=email,
                            institutional_username=institutional_username,
                            active=True,
                        )
                        person.save()
                        cleaned_data["person"] = person

                    # get or create a Researcher for the Person
                    researcher, created = Researcher.objects.get_or_create(
                        person=person, defaults={"publishes": True, "symplectic_access": True}
                    )

                    # get or create a Student for the Researcher
                    stud, created = Student.objects.get_or_create(
                        researcher=researcher,
                        defaults={
                            "student_id": cleaned_data["student_id"],
                            "thesis": cleaned_data["thesis"],
                            "programme": cleaned_data["programme"],
                            "start_date": cleaned_data["start_date"],
                        },
                    )

                    entity = cleaned_data["entity"]

                    # create the Student's Membership
                    m = Membership(
                        person=person,
                        entity=entity,
                        role="%s student" % cleaned_data["programme"],
                        importance_to_person=5,
                    )
                    m.save()

                    # if the Student was created, we'll add the supervisors
                    if created:

                        # loop over the supervisor forms that have data
                        for s in [s for s in supervisor_formset if s.cleaned_data]:

                            supervisor = s.cleaned_data["person"]

                            if not supervisor:
                                s_given_name = s.cleaned_data["given_name"]
                                s_surname = s.cleaned_data["surname"]
                                s_slug = s.cleaned_data["slug"]
                                s_entity = s.cleaned_data["entity"]

                                supervisor = Person(
                                    given_name=s_given_name, surname=s_surname, active=True, slug=s_slug
                                )
                                supervisor.save()
                                s.cleaned_data["person"] = supervisor
                                # create the Supervisor's Membership

                                m = Membership(person=supervisor, entity=s_entity, importance_to_person=5)
                                m.save()

                            # get or create a Researcher for the Person
                            researcher, c = Researcher.objects.get_or_create(person=supervisor)

                            # get or create an Supervisor for each Researcher
                            supervisor, c = Supervisor.objects.get_or_create(researcher=researcher)

                            # get or create a Supervision relationship for each
                            supervision, c = Supervision.objects.get_or_create(supervisor=supervisor, student=stud)

                for k, v in student.cleaned_data.items():
                    student_formset.data[student.prefix + "-" + k] = v

            # rebind the data to the formset
            student_formset = StudentFormset(student_formset.data, prefix="student")

    zipped_formsets = zip(student_formset, supervisor_formsets)

    for (student_form, supervisor_formset) in zipped_formsets:
        student_form.supervisor_formset = supervisor_formset
        # supervisor_formset.student_form = student_form

    # print "zipped length:", len(zipped_formsets)
    return shortcuts.render_to_response(
        "upload/students.html",
        {
            "base_template": settings.CMS_TEMPLATES[0][0],
            "upload_form": upload_form,
            "document": uploaded_document,
            "student_formset": student_formset,
            "zipped_formsets": zipped_formsets,
        },
        RequestContext(request),
    )