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' :}
        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(
        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,
        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:
       = 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)
        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,,
              , 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'] =
        if optional_attributes:
            facade.subsystems.Setter(auth_token, self, new_session, optional_attributes)
        return new_session
    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,
        :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
            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'

        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)
        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