Пример #1
0
    def process_participants(self, evaluation, test_run, user_list):
        evaluation_participants = evaluation.participants.all()
        already_related = [
            user for user in user_list if user in evaluation_participants
        ]
        users_to_add = [
            user for user in user_list if user not in evaluation_participants
        ]

        if already_related:
            msg = format_html(
                _("The following {} users are already participants in evaluation {}:"
                  ), len(already_related), evaluation.name)
            msg += create_user_list_html_string_for_message(already_related)
            self.warnings[ExcelImporter.W_GENERAL].append(msg)

        if not test_run:
            evaluation.participants.add(*users_to_add)
            msg = format_html(_("{} participants added to the evaluation {}:"),
                              len(users_to_add), evaluation.name)
        else:
            msg = format_html(
                _("{} participants would be added to the evaluation {}:"),
                len(users_to_add), evaluation.name)
        msg += create_user_list_html_string_for_message(users_to_add)

        self.success_messages.append(msg)
Пример #2
0
    def save_users_to_db(self):
        """
            Stores the read data in the database. Errors might still
            occur because of the data already in the database.
        """
        new_participants = []
        created_users = []
        with transaction.atomic():
            for user_data in self.users.values():
                try:
                    user, created = user_data.store_in_database()
                    new_participants.append(user)
                    if created:
                        created_users.append(user)

                except Exception as e:
                    self.errors.append(
                        _("A problem occured while writing the entries to the database."
                          " The error message has been: '%(error)s'") %
                        dict(error=e))
                    raise

        msg = format_html(_("Successfully created {} users:"),
                          len(created_users))
        msg += create_user_list_html_string_for_message(created_users)
        self.success_messages.append(msg)
        return new_participants
Пример #3
0
    def write_enrollments_to_db(self, semester, vote_start_datetime,
                                vote_end_date):
        students_created = []
        responsibles_created = []

        with transaction.atomic():
            for user_data in self.users.values():
                # this also marks the users active
                __, created = user_data.store_in_database()
                if created:
                    if user_data.is_responsible:
                        responsibles_created.append(user_data)
                    else:
                        students_created.append(user_data)
            for evaluation_data in self.evaluations.values():
                evaluation_data.store_in_database(vote_start_datetime,
                                                  vote_end_date, semester)

            for evaluation_data, student_data in self.enrollments:
                evaluation = Evaluation.objects.get(
                    course__semester=semester,
                    course__name_de=evaluation_data.name_de)
                student = UserProfile.objects.get(email=student_data.email)
                evaluation.participants.add(student)

        msg = format_html(
            _("Successfully created {} courses/evaluations, {} students and {} contributors:"
              ), len(self.evaluations), len(students_created),
            len(responsibles_created))
        msg += create_user_list_html_string_for_message(students_created +
                                                        responsibles_created)
        self.success_messages.append(msg)
Пример #4
0
    def create_test_success_messages(self):
        filtered_users = [user_data for user_data in self.users.values() if not user_data.user_already_exists()]

        self.success_messages.append(_("The test run showed no errors. No data was imported yet."))
        msg = format_html(_("The import run will create {} users:"), len(filtered_users))
        msg += create_user_list_html_string_for_message(filtered_users)
        self.success_messages.append(msg)
Пример #5
0
    def process_contributors(self, evaluation, test_run, user_list):
        already_related_contributions = Contribution.objects.filter(
            evaluation=evaluation, contributor__in=user_list)
        already_related = [
            contribution.contributor
            for contribution in already_related_contributions
        ]
        if already_related:
            msg = format_html(
                _("The following {} users are already contributing to evaluation {}:"
                  ),
                len(already_related),
                evaluation.full_name,
            )
            msg += create_user_list_html_string_for_message(already_related)
            self.warnings[ImporterWarning.GENERAL].append(msg)

        # since the user profiles are not necessarily saved to the database, they are not guaranteed to have a pk yet which
        # makes anything relying on hashes unusable here (for a faster list difference)
        users_to_add = [
            user for user in user_list if user not in already_related
        ]

        if not test_run:
            for user in users_to_add:
                order = Contribution.objects.filter(
                    evaluation=evaluation).count()
                Contribution.objects.create(evaluation=evaluation,
                                            contributor=user,
                                            order=order)
            msg = format_html(_("{} contributors added to the evaluation {}:"),
                              len(users_to_add), evaluation.full_name)
        else:
            msg = format_html(
                _("{} contributors would be added to the evaluation {}:"),
                len(users_to_add), evaluation.full_name)
        msg += create_user_list_html_string_for_message(users_to_add)

        self.success_messages.append(msg)
Пример #6
0
    def write_enrollments_to_db(self, semester, vote_start_datetime,
                                vote_end_date):
        students_created = []
        responsibles_created = []

        with transaction.atomic():
            for user_data in self.users.values():
                # this also marks the users active
                __, created = user_data.store_in_database()
                if created:
                    if user_data.is_responsible:
                        responsibles_created.append(user_data)
                    else:
                        students_created.append(user_data)
            for evaluation_data in self.evaluations.values():
                evaluation_data.store_in_database(vote_start_datetime,
                                                  vote_end_date, semester)

            participants_per_evaluation = defaultdict(list)
            for evaluation_data, student_data in self.enrollments:
                evaluation = Evaluation.objects.get(
                    course__semester=semester,
                    course__name_de=evaluation_data.name_de)
                participants_per_evaluation[evaluation].append(
                    UserProfile.objects.get(email=student_data.email))

            # add all participants at once to create only a single log message
            for evaluation, participants in participants_per_evaluation.items(
            ):
                evaluation.participants.add(*participants)

        msg = format_html(
            _("Successfully created {} courses/evaluations, {} participants and {} contributors:"
              ),
            self.created_evaluations_count,
            len(students_created),
            len(responsibles_created),
        )
        msg += create_user_list_html_string_for_message(students_created +
                                                        responsibles_created)
        self.success_messages.append(msg)