Пример #1
0
 def test_get_not_existant_image(self):
     image_uri = media_api._get_image_uri(99999)
     try:
         media_api.get_image(image_uri)
     except media_api.GetImageError:
         return
     except:
         pass
     self.assertTrue(False, 'GetImageError not raised')
Пример #2
0
def get_assertion(request, uid):
    """
    Handle badge assertion requests.
    """

    award = get_object_or_404(Award, pk=uid)

    if award.ob_state=='REVOKED':
        return HttpResponseGone('{"revoked": true}', mimetype="application/json")

    badge = award.badge
    recipient_email = get_user(award.user_uri)['email']
    image = get_image(badge.image_uri)

    assertion = create_assertion_from_template(
        uid = uid,
        recipient_email = recipient_email,
        image = image['url'],
        evidence = award.evidence_url,
        issued_on = award.date_awarded,
        badge_id = badge.pk,
    )
    json_assertion = json.dumps(assertion)
    print json_assertion
    return HttpResponse(json_assertion, mimetype="application/json")
Пример #3
0
def _populate_course_context( request, course_id, context ):
    course_uri = course_model.course_id2uri(course_id)
    course = _get_course_or_404(course_uri)
    context['course'] = course
    context['course_url'] = request.get_full_path()
    if 'image_uri' in course:
        context['course']['image'] = media_model.get_image(course['image_uri'])

    #NOTE if performance becomes a problem dont fetch cohort
    cohort = course_model.get_course_cohort(course_uri)
    context['cohort'] = cohort
    user_uri = "/uri/user/{0}".format(request.user.username)
    context['organizer'] = course_model.is_cohort_organizer(
        user_uri, cohort['uri']
    )
    context['organizer'] |= request.user.is_superuser
    context['can_edit'] = context['organizer'] and not course['status'] == 'archived'
    if course_model.user_in_cohort(user_uri, cohort['uri']):
        if not context['organizer']:
            context['show_leave_course'] = True
        context['learner'] = True
    elif cohort['signup'] == "OPEN":
        context['show_signup'] = True

    return context
Пример #4
0
def get_user_courses(user_uri):
    """ return courses organized or participated in by an user """
    signups = db.CohortSignup.objects.filter(user_uri=user_uri,
                                             leave_date__isnull=True,
                                             cohort__course__archived=False)
    courses = []
    for signup in signups:
        try:
            course = get_course(course_id2uri(signup.cohort.course.id))
        except ResourceDeletedException:
            continue
        course_data = {
            "id":
            course['id'],
            "title":
            course['title'],
            "user_role":
            signup.role,
            "url":
            reverse("courses_show",
                    kwargs={
                        "course_id": course["id"],
                        "slug": course["slug"]
                    }),
        }
        if "image_uri" in course:
            course_data["image_url"] = media_model.get_image(
                course['image_uri'])['url']
        courses += [course_data]
    return courses
Пример #5
0
def get_user_courses(user_uri):
    """ return courses organized or participated in by an user """
    signups = db.CohortSignup.objects.filter(user_uri=user_uri, leave_date__isnull=True, cohort__course__archived=False)
    courses = []
    for signup in signups:
        course = get_course(course_id2uri(signup.cohort.course.id))
        course_data = {
            "title": course["title"],
            "user_role": signup.role,
            "url": reverse("courses_show", kwargs={"course_id": course["id"], "slug": course["slug"]}),
        }
        if "image_uri" in course:
            course_data["image_url"] = media_model.get_image(course["image_uri"])["url"]
        courses += [course_data]
    return courses
Пример #6
0
def _populate_course_context( request, course_id, context ):
    course_uri = course_model.course_id2uri(course_id)
    course = _get_course_or_404(course_uri)
    course['author'] = course['author_uri'].strip('/').split('/')[-1]
    context['course'] = course
    context['course_url'] = reverse('courses_show',
        kwargs={'course_id': course['id'], 'slug': course['slug']}
    )
    if 'image_uri' in course:
        context['course']['image'] = media_model.get_image(course['image_uri'])

    #NOTE if performance becomes a problem dont fetch cohort
    cohort = course_model.get_course_cohort(course_uri)
    context['cohort'] = cohort
    user_uri = u"/uri/user/{0}".format(request.user.username)
    context['organizer'] = course_model.is_cohort_organizer(
        user_uri, cohort['uri']
    )
    context['organizer'] |= request.user.is_superuser
    context['admin'] = request.user.is_superuser
    context['can_edit'] = context['organizer'] and not course['status'] == 'archived'
    context['trusted_user'] = request.user.has_perm('users.trusted_user')
    if course_model.user_in_cohort(user_uri, cohort['uri']):
        if not context['organizer']:
            context['show_leave_course'] = True
        context['learner'] = True
    elif cohort['signup'] == "OPEN":
        context['show_signup'] = True

    try:
        course_lists = learn_model.get_lists_for_course(reverse(
            'courses_slug_redirect',
            kwargs={'course_id': course_id}
        ))
        f = lambda l: l['name'] not in ['drafts', 'listed', 'archived']
        context['lists'] = filter(f, course_lists)
    except:
        log.error("Could not get lists for course!")

    if 'based_on_uri' in course:
        course['based_on'] = course_model.get_course(course['based_on_uri'])

    context['meta_data'] = lrmi_model.get_tags(course_uri)
    if 'educational_alignment' in context['meta_data']:
        context['educational_alignment'] = json.loads(context['meta_data']['educational_alignment'])
        del context['meta_data']['educational_alignment']

    return context
Пример #7
0
def get_badge(request, badge_id):
    """
    Handles badge requests.
    """

    badge = get_object_or_404(Badge, pk=badge_id)
    image = get_image(badge.image_uri)
    criteria = reverse_url('badge_view', args=[badge_id])

    badge = create_badge_from_template(
        name = badge.title,
        description = badge.description,
        image = image['url'],
        criteria = criteria,
    )
    json_badge = json.dumps(badge)

    return HttpResponse(json_badge, mimetype="application/json")
Пример #8
0
def get_course_learn_api_data(course_uri):
    """ return data used by the learn API to list courses """
    course = get_course(course_uri)

    course_url = reverse("courses_slug_redirect", kwargs={"course_id": course["id"]})
    data_url = reverse("courses_learn_api_data", kwargs={"course_id": course["id"]})

    learn_api_data = {
        "course_url": course_url,
        "title": course["title"],
        "description": course["description"],
        "data_url": data_url,
        "language": course["language"],
        "thumbnail_url": "",
        "tags": get_course_tags(course_uri),
    }

    if "image_uri" in course:
        learn_api_data["thumbnail_url"] = media_model.get_image(course["image_uri"])["url"]

    return learn_api_data
Пример #9
0
def get_course_learn_api_data(course_uri):
    """ return data used by the learn API to list courses """
    course = get_course(course_uri)

    course_url = reverse('courses_slug_redirect',
                         kwargs={'course_id': course["id"]})
    data_url = reverse('courses_learn_api_data',
                       kwargs={'course_id': course["id"]})

    learn_api_data = {
        "course_url": course_url,
        "title": course['title'],
        "description": course['description'],
        "data_url": data_url,
        "language": course['language'],
        "thumbnail_url": "",
        "tags": get_course_tags(course_uri)
    }

    if 'image_uri' in course:
        learn_api_data["thumbnail_url"] = media_model.get_image(
            course['image_uri'])['url']

    return learn_api_data
Пример #10
0
def get_course_learn_api_data(course_uri):
    """ return data used by the learn API to list courses """
    course = get_course(course_uri)

    course_url = reverse(
        'courses_slug_redirect',
        kwargs={'course_id': course["id"]}
    )
    data_url = reverse('courses_learn_api_data', kwargs={'course_id': course["id"]})

    learn_api_data = {
        "course_url": course_url,
        "title": course['title'],
        "description": course['description'],
        "data_url": data_url,
        "language": course['language'],
        "thumbnail_url": "",
        "tags": get_course_tags(course_uri)
    }

    if 'image_uri' in course:
        learn_api_data["thumbnail_url"] = media_model.get_image(course['image_uri'])['url']

    return learn_api_data
Пример #11
0
def fetch_badge_resources( badge ):
    badge['image'] = media_api.get_image(badge['image_uri'])
    badge['author'] = p2pu_user_api.get_user(badge['author_uri'])
    return badge
Пример #12
0
def fetch_resources( project ):
    project['image'] = media_api.get_image(project['image_uri'])
    project['author'] = p2pu_user_api.get_user(project['author_uri'])
    return project
Пример #13
0
def fetch_badge_resources( badge ):
    badge['image'] = media_api.get_image(badge['image_uri'])
    badge['author'] = p2pu_user_api.get_user(badge['author_uri'])
    badge['url'] = settings.ORGANISATION_URL + reverse('badge_view', args=(badge['id'],))
    return badge