Пример #1
0
    def get_context_data(self, **kwargs):
        data = super(ResourceEvaluations, self).get_context_data(**kwargs)
        data["content_type"] = self.content_type
        data["object"] = self.object

        evaluations = Evaluation.objects.filter(
            confirmed=True, content_type=self.content_type, object_id=self.object.id
        )
        data["total_evaluations"] = evaluations.count()

        data["average_scores"] = []

        standard_scores = StandardAlignmentScore.objects.filter(
            evaluation__confirmed=True, evaluation__content_type=self.content_type, evaluation__object_id=self.object.id
        )
        data["average_scores"].append(
            dict(
                rubric=u"R1",
                score=standard_scores.exclude(score__value=None).aggregate(models.Avg("score__value"))[
                    "score__value__avg"
                ],
            )
        )

        rubric_scores = RubricScore.objects.filter(
            evaluation__confirmed=True, evaluation__content_type=self.content_type, evaluation__object_id=self.object.id
        )
        for i, rubric_id in enumerate(Rubric.objects.values_list("id", flat=True)):
            data["average_scores"].append(
                dict(
                    rubric=u"R%i" % (i + 2),
                    score=rubric_scores.filter(rubric__id=rubric_id)
                    .exclude(score__value=None)
                    .aggregate(models.Avg("score__value"))["score__value__avg"],
                )
            )

        comments = []
        for qs in (standard_scores, rubric_scores):
            for score in qs.exclude(comment="").select_related():
                comment = dict(text=score.comment, timestamp=score.evaluation.timestamp, author=score.evaluation.user)

                if isinstance(score, StandardAlignmentScore):
                    title = u"Degree of Alignment to %s" % score.alignment_tag.full_code
                else:
                    title = score.rubric.name

                comment["title"] = u"%s: %s (%s)" % (
                    title,
                    get_verbose_score_name(score.score.value),
                    score.score.get_value_display(),
                )

                comments.append(comment)
        data["comments"] = comments

        return data
Пример #2
0
    def get_context_data(self, **kwargs):
        code = kwargs["code"]
        content_type_id = kwargs.get("content_type_id")
        object_id = kwargs.get("object_id")
        try:
            tag = AlignmentTag.objects.get_from_full_code(code)
        except AlignmentTag.DoesNotExist:
            raise Http404()

        data = dict(tag=tag)

        if content_type_id and object_id:
            object_id = int(object_id)
            content_type = get_object_or_404(ContentType, id=content_type_id)

            data["evaluations_number"] = Evaluation.objects.filter(
                content_type=content_type,
                object_id=object_id,
                confirmed=True,
            ).count()

            scores = StandardAlignmentScore.objects.filter(
                alignment_tag=tag,
                evaluation__content_type=content_type,
                evaluation__object_id=object_id,
            )
            if scores.exists():
                value = scores.aggregate(value=Avg("score__value"))["value"]
                data["score_value"] = value
                if value is None:
                    data["score_class"] = "na"
                else:
                    value = int(round(value))
                    data["score_class"] = str(value)
                data["score_verbose"] = get_verbose_score_name(value)
            else:
                data["score_value"] = None
                data["score_verbose"] = u"Not Rated"
                data["score_class"] = "nr"

        return data
Пример #3
0
    def get_context_data(self, **kwargs):
        data = super(ViewItem, self).get_context_data(**kwargs)

        item = self.item
        request = self.request

        data["breadcrumbs"] = getattr(item, "breadcrumbs", [])

        if request.user.is_authenticated():
            content_actions = []
            if item.creator == request.user or request.user.is_staff:
                content_actions += [
                    {"url": reverse("materials:%s:edit" % item.namespace,
                                    kwargs=dict(slug=item.slug)),
                     "title": u"Edit",
                     "class": "edit"},

                    {"url": reverse("materials:%s:delete" % item.namespace,
                                    kwargs=dict(slug=item.slug)),
                     "title": u"Delete",
                     "class": "delete"},
                ]
            if request.user.is_staff:
                content_actions.append({
                    "url": reverse("admin:%s_%s_change" % (self.content_type.app_label,
                                                        self.content_type.model),
                                                        args=(item.id,)),
                    "title": u"Edit in admin",
                    "class": "edit"
                })

            workflow_actions = []
            for transition in WORKFLOW_TRANSITIONS:
                if item.workflow_state in transition["from"] and transition["condition"](request.user, item):
                    workflow_actions.append({
                        "url": reverse("materials:%s:transition" % item.namespace,
                                       kwargs=dict(slug=item.slug,
                                                   transition_id=transition["id"])),
                        "title": transition["title"],
                    })

            data["content_actions"] = content_actions
            data["workflow_actions"] = workflow_actions

        tags = []
        tags_slugs = set()
        microsite_markers = set()

        for microsite in Microsite.objects.all():
            microsite_markers.update(microsite.keywords.values_list("slug", flat=True))

        if request.user.is_authenticated():
            user_tags = item.tags.filter(user=request.user).values("id", "slug", "name")
            for tag in user_tags:
                tag["class"] = "tag"
                tags_slugs.add(tag["slug"])
            data["user_tags"] = user_tags

        for topic in item.topics():
            tag = {"class": "topic", "slug": topic.slug, "name": topic.name,
                   "microsite": topic.microsite, "other": topic.other}
            tags_slugs.add(tag["slug"])
            tags.append(tag)

        for tag in item.keywords.exclude(slug__in=tags_slugs | microsite_markers).values("id", "slug", "name"):
            tag["class"] = "keyword"
            tags_slugs.add(tag["slug"])
            tags.append(tag)

        for tag in item.tags.exclude(slug__in=tags_slugs | microsite_markers).values("id", "slug", "name"):
            if tag["slug"] in tags_slugs:
                continue
            tag["class"] = "tag"
            tags_slugs.add(tag["slug"])
            tags.append(tag)

        tags.sort(key=lambda tag: tag["slug"])

        data["tags"] = tags

        data["evaluations_number"] = item.evaluations_number
        data["evaluation_scores"] = []

        standard_scores = StandardAlignmentScore.objects.filter(
            evaluation__confirmed=True,
            evaluation__content_type=self.content_type,
            evaluation__object_id=self.item.id,
            alignment_tag__id__in=self.item.alignment_tags.all().values_list("tag__id", flat=True).distinct()
        )

        data["alignment_scores"] = []
        for row in standard_scores.exclude(score__value=None).values(
            "alignment_tag").annotate(score=Avg("score__value")):
            data["alignment_scores"].append(dict(
                code=AlignmentTag.objects.get(id=row["alignment_tag"]).full_code,
                score=row["score"],
                score_class=int(round(row["score"])),
            ))
        data["alignment_scores"].sort(key=lambda x: x["score"], reverse=True)

        rubric_scores = RubricScore.objects.filter(
            evaluation__confirmed=True,
            evaluation__content_type=self.content_type,
            evaluation__object_id=self.item.id,
        )

        scores = item.evaluation_scores
        for rubric_id, name in Rubric.objects.values_list("id", "name"):
            #noinspection PySimplifyBooleanCheck
            score = None
            score_class = "nr"
            if rubric_id in scores:
                score = scores[rubric_id]
                if score is None:
                    score_class = None
                else:
                    score_class = int(round(score))
            data["evaluation_scores"].append(dict(
                name=name,
                score=score,
                score_class=score_class,
                evaluations_number=rubric_scores.filter(
                    rubric__id=rubric_id
                ).exclude(score__value=None).values(
                    "evaluation"
                ).distinct().count()
            ))

        data["toolbar_view_url"] = reverse("materials:%s:toolbar_view_item" % item.namespace,
                                       kwargs=dict(slug=item.slug))


        comments = []

        data["hide_comment_form"] = False
        data["comment_form"] = ReviewForm()

        for qs in (standard_scores, rubric_scores):
            for score in qs.exclude(comment="").select_related():
                comment = dict(
                    text=score.comment,
                    timestamp=score.evaluation.timestamp,
                    author=score.evaluation.user,
                )

                if isinstance(score, StandardAlignmentScore):
                    title = u"Degree of Alignment to %s" % score.alignment_tag.full_code
                else:
                    title = score.rubric.name

                comment["title"] = u"%s: %s (%s)" % (
                    title,
                    get_verbose_score_name(score.score.value),
                    score.score.get_value_display(),
                )

                comments.append(comment)

        for review in item.reviews.all():
            comments.append(dict(
                title=u"",
                text=review.text,
                timestamp=review.timestamp,
                author=review.user,
                is_review=True,
            ))
            if review.user == request.user:
                data["hide_comment_form"] = True
                data["comment_form"] = ReviewForm(instance=review)

        comments.sort(key=lambda x: x["timestamp"])
        data["comments"] = comments

        return data