Example #1
0
    def create(self, auth_token, curriculum, start, end, users=None):
        """
        Create a new curriculum_enrollment.
        
        :param  curriculum:     FK for a curriculum
        :param  start:          start date in ISO8601 format
        :param  end:            end date in ISO8601 format
        :return:                a reference to the newly created curriculum_enrollment
        """

        start_date = pr_time.iso8601_to_datetime(start).replace(microsecond=0,
                                                                second=0,
                                                                minute=0,
                                                                hour=0)
        end_date = pr_time.iso8601_to_datetime(end).replace(microsecond=0,
                                                            second=0,
                                                            minute=0,
                                                            hour=0)

        c = self.my_django_model(start=start_date, end=end_date)
        c.curriculum = self._find_by_id(curriculum, facade.models.Curriculum)
        c.save()
        if users is not None:
            for user in facade.models.User.objects.filter(id__in=users):
                facade.models.CurriculumEnrollmentUserAssociation.objects.create(
                    user=user, curriculum_enrollment=c)
        self.authorizer.check_create_permissions(auth_token, c)
        return c
    def create(self,
               auth_token,
               name_prefix,
               title,
               description,
               start,
               end,
               organization,
               product_line,
               optional_attributes=None):
        """
        Create a new Event.
        
        A region or a venue must be provided.
        
        @param name_prefix          prefix for the human-readable unique identifier 'name'
        @param title                title of the Event
        @param description          description of the Event
        @param start                Date on which the Event starts, as an ISO8601 string.
                                    If you provide hour, minute, or second, they will be ignored.
        @param end                  Date on which the Event ends, as an ISO8601 string.
                                    If you provide hour, minute, or second, they will be ignored.
        @param organization         FK for organization
        @param product_line         Foreign Key for a product_line
        @param optional_attributes  currently only 'venue', 'region', 'event_template', and 'lead_time'
        @return                     a reference to the newly created Event
        """

        if optional_attributes is None:
            optional_attributes = {}

        start_date = pr_time.iso8601_to_datetime(start).replace(microsecond=0,
                                                                second=0,
                                                                minute=0,
                                                                hour=0)
        end_date = pr_time.iso8601_to_datetime(end).replace(microsecond=0,
                                                            second=0,
                                                            minute=0,
                                                            hour=0)
        e = self.my_django_model.objects.create(
            title=title,
            description=description,
            start=start_date,
            organization=self._find_by_id(organization,
                                          facade.models.Organization),
            product_line=self._find_by_id(product_line,
                                          facade.models.ProductLine),
            end=end_date,
            owner=auth_token.user)
        e.name = '%s%d' % (name_prefix if name_prefix is not None else '',
                           e.id)
        if 'lag_time' not in optional_attributes:
            optional_attributes['lag_time'] = settings.DEFAULT_EVENT_LAG_TIME
        facade.subsystems.Setter(auth_token, self, e, optional_attributes)
        e.save()

        self.authorizer.check_create_permissions(auth_token, e)
        return e
    def _get_num_views(self, start_date=None, end_date=None):
        """Returns the number of VideoSession objects that are associated with this Video object, optionally filtered by start and end dates."""

        session_filter = {'assignment__task__id__exact': self.id}
        if start_date is not None:
            if pr_time.is_iso8601(start_date):
                start_date = pr_time.iso8601_to_datetime(start_date)
            session_filter['date_started__gte'] = start_date
        if end_date is not None:
            if pr_time.is_iso8601(end_date):
                end_date = pr_time.iso8601_to_datetime(end_date)
            session_filter['date_started__lte'] = end_date
        return VideoSession.objects.filter(**session_filter).count()
    def _get_num_views(self, start_date=None, end_date=None):
        """Returns the number of VideoSession objects that are associated with this Video object, optionally filtered by start and end dates."""

        session_filter = {'assignment__task__id__exact' : self.id}
        if start_date is not None:
            if pr_time.is_iso8601(start_date):
                start_date = pr_time.iso8601_to_datetime(start_date)
            session_filter['date_started__gte'] = start_date
        if end_date is not None:
            if pr_time.is_iso8601(end_date):
                end_date = pr_time.iso8601_to_datetime(end_date)
            session_filter['date_started__lte'] = end_date
        return VideoSession.objects.filter(**session_filter).count()
    def _create(self, auth_token, start, end, status, confirmed, default_price,
            event, optional_attributes=None):
        """
        Create a new Session
        
        @param start          Start time, ISO8601 string
        @param end            End time, ISO8601 string
        @param status         String: one of 'active', 'pending', 'canceled', 'completed'
        @param confirmed      Boolean: is this Session confirmed?
        @param default_price  Default price for the Session in US cents
        @return               Instance of Session
        """

        if optional_attributes is None:
            optional_attributes = {}

        actor = auth_token.user
        b = facade.managers.BlameManager().create(auth_token)
        end = pr_time.iso8601_to_datetime(end)
        name = str(end.year)+str(end.month)+str(end.day)
        new_session = self.my_django_model(start = pr_time.iso8601_to_datetime(start), end=end, name=name,
                status=status, confirmed=confirmed, default_price=default_price,
                blame=b)
        if 'session_template' in optional_attributes:
            the_session_template = self._find_by_id(optional_attributes['session_template'], facade.models.SessionTemplate)
            if the_session_template.shortname:
                new_session.name = the_session_template.shortname + name
            if the_session_template.description is not None:
                new_session.description = the_session_template.description
            if (the_session_template.price is not None) and (new_session.default_price is None):
                new_session.default_price = the_session_template.price
            if (the_session_template.modality is not None) and (new_session.modality is None):
                new_session.modality = the_session_template.modality
        new_session.event = self._find_by_id(event, facade.models.Event)
        new_session.save()
        if 'session_template' in optional_attributes:
            if (the_session_template.session_template_user_role_requirements.all().count() != 0):
                # We need to create a session_user_role_requirement for each of these and associate it with this session
                for session_template_user_role_requirement in the_session_template.session_template_user_role_requirements.all():
                    new_session_user_role_requirement = self.session_user_role_requirement_manager.create(auth_token, new_session.id,
                        session_template_user_role_requirement.session_user_role.id, session_template_user_role_requirement.min, session_template_user_role_requirement.max, False)
            new_session.session_template = the_session_template
            del optional_attributes['session_template']
        new_session.name = new_session.name+new_session.mangle_id(new_session.id)
        new_session.save()
        if optional_attributes:
            facade.subsystems.Setter(auth_token, self, new_session, optional_attributes)
            new_session.save()
        return new_session
Example #6
0
    def create(self, auth_token, task, user=None, optional_parameters=None):
        """Create an Assignment

        Status cannot be specified because of permission issues.  You must
        allow the system to automatically determine the enrollment status,
        and then a user with suffient permission may update that status.

        :param task:                FK for a Task
        :type task:                 int
        :param user:                FK for a User, defaults to acting user if not specified
        :type user:                 int
        :param optional_parameters: Optional attribute definitions, including
                                    date_started, due_date, effective_date_assigned,
                                    serial_number
        :type optional_parameters:  dict

        :return:                    reference to an Assignment

        """
        if optional_parameters is None:
            optional_parameters = {}

        blame = self._get_blame(auth_token)
        
        task_object = self._find_by_id(task, facade.models.Task).downcast_completely()
        if user is None and isinstance(auth_token, facade.models.AuthToken):
            user_object = auth_token.user
        else:
            user_object = self._find_by_id(user, facade.models.User)

        # check for duplicate assignment, but we can't raise an exception
        # until we know the user is otherwise authorized to create one
        if task_object.prevent_duplicate_assignments:
            duplicate_assignment_exists = self.my_django_model.objects.filter(user=user_object, task=task_object).count() > 0

        # handle optional attributes that are simple so we avoid the problem
        # of using update permissions
        assignment = self.my_django_model(task=task_object, user=user_object, blame=blame)
        for attribute in ['serial_number',]:
            if attribute in optional_parameters:
                setattr(assignment, attribute, optional_parameters[attribute])
        for attribute in ['date_started', 'due_date', 'effective_date_assigned']:
            if attribute in optional_parameters:
                value = pr_time.iso8601_to_datetime(optional_parameters[attribute])
                setattr(assignment, attribute, value)
        if task_object.remaining_capacity <= 0:
            assignment.status = 'wait-listed'
        # if there is a wait-list, even if capacity has opened up, continue
        # wait-listing so an admin can sort out who gets priority
        elif task_object.assignments.filter(status='wait-listed').count() > 0:
            assignment.status = 'wait-listed'
        assignment.save()

        self.authorizer.check_create_permissions(auth_token, assignment)

        if task_object.prevent_duplicate_assignments and duplicate_assignment_exists:
            raise exceptions.DuplicateAssignmentException

        return assignment
    def create(self, auth_token, curriculum, start, end, users=None):
        """
        Create a new curriculum_enrollment.
        
        :param  curriculum:     FK for a curriculum
        :param  start:          start date in ISO8601 format
        :param  end:            end date in ISO8601 format
        :return:                a reference to the newly created curriculum_enrollment
        """

        start_date = pr_time.iso8601_to_datetime(start).replace(microsecond=0, second=0, minute=0, hour=0)
        end_date = pr_time.iso8601_to_datetime(end).replace(microsecond=0, second=0, minute=0, hour=0)

        c = self.my_django_model(start=start_date, end=end_date)
        c.curriculum = self._find_by_id(curriculum, facade.models.Curriculum)
        c.save()
        if users is not None:
            for user in facade.models.User.objects.filter(id__in=users):
                facade.models.CurriculumEnrollmentUserAssociation.objects.create(user=user, curriculum_enrollment=c)
        self.authorizer.check_create_permissions(auth_token, c)
        return c
 def watcher_report(self,
                    auth_token,
                    videos,
                    start_date=None,
                    end_date=None):
     """
     Returns a list of views of the given videos (optinally filtered by date)
     along with some information about the viewer.
     """
     filters = {'member': {'assignment__task__id': videos}}
     if start_date or end_date:
         filters = [filters]
         if start_date:
             if pr_time.is_iso8601(start_date):
                 start_date = pr_time.iso8601_to_datetime(start_date)
             filters.append(
                 {'greater_than_or_equal': {
                     'date_started': start_date
                 }})
         if end_date:
             if pr_time.is_iso8601(end_date):
                 end_date = pr_time.iso8601_to_datetime(end_date)
             filters.append(
                 {'less_than_or_equal': {
                     'date_started': end_date
                 }})
         filters = {'and': filters}
     views = self.get_filtered(auth_token, filters,
                               ['video', 'date_started', 'user'])
     views = Utils.merge_queries(views, facade.managers.VideoManager(),
                                 auth_token, ['name'], 'video')
     views = Utils.merge_queries(views, facade.managers.UserManager(),
                                 auth_token, [
                                     'first_name', 'last_name', 'email',
                                     'default_username_and_domain'
                                 ], 'user')
     return views
    def _create(self,
                auth_token,
                start,
                end,
                status,
                confirmed,
                default_price,
                event,
                optional_attributes=None):
        """
        Create a new Session
        
        @param start          Start time, ISO8601 string
        @param end            End time, ISO8601 string
        @param status         String: one of 'active', 'pending', 'canceled', 'completed'
        @param confirmed      Boolean: is this Session confirmed?
        @param default_price  Default price for the Session in US cents
        @return               Instance of Session
        """

        if optional_attributes is None:
            optional_attributes = {}

        actor = auth_token.user
        b = facade.managers.BlameManager().create(auth_token)
        end = pr_time.iso8601_to_datetime(end)
        name = str(end.year) + str(end.month) + str(end.day)
        new_session = self.my_django_model(
            start=pr_time.iso8601_to_datetime(start),
            end=end,
            name=name,
            status=status,
            confirmed=confirmed,
            default_price=default_price,
            blame=b)
        if 'session_template' in optional_attributes:
            the_session_template = self._find_by_id(
                optional_attributes['session_template'],
                facade.models.SessionTemplate)
            if the_session_template.shortname:
                new_session.name = the_session_template.shortname + name
            if the_session_template.description is not None:
                new_session.description = the_session_template.description
            if (the_session_template.price
                    is not None) and (new_session.default_price is None):
                new_session.default_price = the_session_template.price
            if (the_session_template.modality
                    is not None) and (new_session.modality is None):
                new_session.modality = the_session_template.modality
        new_session.event = self._find_by_id(event, facade.models.Event)
        new_session.save()
        if 'session_template' in optional_attributes:
            if (the_session_template.session_template_user_role_requirements.
                    all().count() != 0):
                # We need to create a session_user_role_requirement for each of these and associate it with this session
                for session_template_user_role_requirement in the_session_template.session_template_user_role_requirements.all(
                ):
                    new_session_user_role_requirement = self.session_user_role_requirement_manager.create(
                        auth_token, new_session.id,
                        session_template_user_role_requirement.
                        session_user_role.id,
                        session_template_user_role_requirement.min,
                        session_template_user_role_requirement.max, False)
            new_session.session_template = the_session_template
            del optional_attributes['session_template']
        new_session.name = new_session.name + new_session.mangle_id(
            new_session.id)
        new_session.save()
        if optional_attributes:
            facade.subsystems.Setter(auth_token, self, new_session,
                                     optional_attributes)
            new_session.save()
        return new_session