api.py 20.6 KB
Newer Older
1
"""Certificates API
2

3 4 5 6
This is a Python API for generating certificates asynchronously.
Other Django apps should use the API functions defined in this module
rather than importing Django models directly.
"""
7
import logging
8

9
from django.conf import settings
10
from django.core.urlresolvers import reverse
11
from django.db.models import Q
12
from opaque_keys.edx.keys import CourseKey
13

14
from branding import api as branding_api
15 16
from certificates.models import (
    CertificateGenerationConfiguration,
17
    CertificateGenerationCourseSetting,
18
    CertificateInvalidation,
19
    CertificateStatuses,
20
    CertificateTemplate,
21
    CertificateTemplateAsset,
22 23
    ExampleCertificateSet,
    GeneratedCertificate,
24
    certificate_status_for_student
25
)
26
from certificates.queue import XQueueCertInterface
27 28 29
from eventtracking import tracker
from openedx.core.djangoapps.content.course_overviews.models import CourseOverview
from openedx.core.djangoapps.xmodule_django.models import CourseKeyField
30
from util.organizations_helpers import get_course_organization_id
31
from xmodule.modulestore.django import modulestore
32

33
log = logging.getLogger("edx.certificate")
34
MODES = GeneratedCertificate.MODES
35 36


37 38 39 40 41 42 43 44 45
def is_passing_status(cert_status):
    """
    Given the status of a certificate, return a boolean indicating whether
    the student passed the course.  This just proxies to the classmethod
    defined in models.py
    """
    return CertificateStatuses.is_passing_status(cert_status)


46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
def format_certificate_for_user(username, cert):
    """
    Helper function to serialize an user certificate.

    Arguments:
        username (unicode): The identifier of the user.
        cert (GeneratedCertificate): a user certificate

    Returns: dict
    """
    return {
        "username": username,
        "course_key": cert.course_id,
        "type": cert.mode,
        "status": cert.status,
        "grade": cert.grade,
        "created": cert.created_date,
        "modified": cert.modified_date,
64
        "is_passing": is_passing_status(cert.status),
65 66 67 68 69 70 71 72 73 74 75 76

        # NOTE: the download URL is not currently being set for webview certificates.
        # In the future, we can update this to construct a URL to the webview certificate
        # for courses that have this feature enabled.
        "download_url": (
            cert.download_url or get_certificate_url(cert.user.id, cert.course_id)
            if cert.status == CertificateStatuses.downloadable
            else None
        ),
    }


77 78 79 80 81 82 83 84 85 86 87 88 89 90
def get_certificates_for_user(username):
    """
    Retrieve certificate information for a particular user.

    Arguments:
        username (unicode): The identifier of the user.

    Returns: list

    Example Usage:
    >>> get_certificates_for_user("bob")
    [
        {
            "username": "bob",
91
            "course_key": CourseLocator('edX', 'DemoX', 'Demo_Course', None, None),
92 93 94 95 96 97 98 99 100 101 102
            "type": "verified",
            "status": "downloadable",
            "download_url": "http://www.example.com/cert.pdf",
            "grade": "0.98",
            "created": 2015-07-31T00:00:00Z,
            "modified": 2015-07-31T00:00:00Z
        }
    ]

    """
    return [
103
        format_certificate_for_user(username, cert)
104
        for cert in GeneratedCertificate.eligible_certificates.filter(user__username=username).order_by("course_id")
105 106 107
    ]


108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
def get_certificate_for_user(username, course_key):
    """
    Retrieve certificate information for a particular user for a specific course.

    Arguments:
        username (unicode): The identifier of the user.
        course_key (CourseKey): A Course Key.
    Returns: dict
    """
    try:
        cert = GeneratedCertificate.eligible_certificates.get(
            user__username=username,
            course_id=course_key
        )
    except GeneratedCertificate.DoesNotExist:
        return None
    return format_certificate_for_user(username, cert)


127 128
def generate_user_certificates(student, course_key, course=None, insecure=False, generation_mode='batch',
                               forced_grade=None):
129 130 131
    """
    It will add the add-cert request into the xqueue.

132 133
    A new record will be created to track the certificate
    generation task.  If an error occurs while adding the certificate
134 135
    to the queue, the task will have status 'error'. It also emits
    `edx.certificate.created` event for analytics.
136

137
    Args:
138 139 140 141 142 143
        student (User)
        course_key (CourseKey)

    Keyword Arguments:
        course (Course): Optionally provide the course object; if not provided
            it will be loaded.
144
        insecure - (Boolean)
145 146
        generation_mode - who has requested certificate generation. Its value should `batch`
        in case of django command and `self` if student initiated the request.
147 148
        forced_grade - a string indicating to replace grade parameter. if present grading
                       will be skipped.
149 150
    """
    xqueue = XQueueCertInterface()
151 152
    if insecure:
        xqueue.use_https = False
153 154 155 156 157 158

    if not course:
        course = modulestore().get_course(course_key, depth=0)

    generate_pdf = not has_html_certificates_enabled(course)

159 160 161 162 163 164 165
    cert = xqueue.add_cert(
        student,
        course_key,
        course=course,
        generate_pdf=generate_pdf,
        forced_grade=forced_grade
    )
166 167 168 169 170
    # If cert_status is not present in certificate valid_statuses (for example unverified) then
    # add_cert returns None and raises AttributeError while accesing cert attributes.
    if cert is None:
        return

171
    if CertificateStatuses.is_passing_status(cert.status):
172 173 174 175 176 177 178
        emit_certificate_event('created', student, course_key, course, {
            'user_id': student.id,
            'course_id': unicode(course_key),
            'certificate_id': cert.verify_uuid,
            'enrollment_mode': cert.mode,
            'generation_mode': generation_mode
        })
179
    return cert.status
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205


def regenerate_user_certificates(student, course_key, course=None,
                                 forced_grade=None, template_file=None, insecure=False):
    """
    It will add the regen-cert request into the xqueue.

    A new record will be created to track the certificate
    generation task.  If an error occurs while adding the certificate
    to the queue, the task will have status 'error'.

    Args:
        student (User)
        course_key (CourseKey)

    Keyword Arguments:
        course (Course): Optionally provide the course object; if not provided
            it will be loaded.
        grade_value - The grade string, such as "Distinction"
        template_file - The template file used to render this certificate
        insecure - (Boolean)
    """
    xqueue = XQueueCertInterface()
    if insecure:
        xqueue.use_https = False

206 207 208 209 210
    if not course:
        course = modulestore().get_course(course_key, depth=0)

    generate_pdf = not has_html_certificates_enabled(course)

211 212 213 214 215 216 217 218
    return xqueue.regen_cert(
        student,
        course_key,
        course=course,
        forced_grade=forced_grade,
        template_file=template_file,
        generate_pdf=generate_pdf
    )
219 220 221 222 223 224 225 226 227 228 229 230


def certificate_downloadable_status(student, course_key):
    """
    Check the student existing certificates against a given course.
    if status is not generating and not downloadable or error then user can view the generate button.

    Args:
        student (user object): logged-in user
        course_key (CourseKey): ID associated with the course

    Returns:
231
        Dict containing student passed status also download url, uuid for cert if available
232 233 234 235 236 237 238 239
    """
    current_status = certificate_status_for_student(student, course_key)

    # If the certificate status is an error user should view that status is "generating".
    # On the back-end, need to monitor those errors and re-submit the task.

    response_data = {
        'is_downloadable': False,
240 241
        'is_generating': True if current_status['status'] in [CertificateStatuses.generating,
                                                              CertificateStatuses.error] else False,
242
        'is_unverified': True if current_status['status'] == CertificateStatuses.unverified else False,
243 244
        'download_url': None,
        'uuid': None,
245
    }
246
    may_view_certificate = CourseOverview.get_from_id(course_key).may_certify()
247

248
    if current_status['status'] == CertificateStatuses.downloadable and may_view_certificate:
249
        response_data['is_downloadable'] = True
250
        response_data['download_url'] = current_status['download_url'] or get_certificate_url(student.id, course_key)
251
        response_data['uuid'] = current_status['uuid']
252 253

    return response_data
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278


def set_cert_generation_enabled(course_key, is_enabled):
    """Enable or disable self-generated certificates for a course.

    There are two "switches" that control whether self-generated certificates
    are enabled for a course:

    1) Whether the self-generated certificates feature is enabled.
    2) Whether self-generated certificates have been enabled for this particular course.

    The second flag should be enabled *only* when someone has successfully
    generated example certificates for the course.  This helps avoid
    configuration errors (for example, not having a template configured
    for the course installed on the workers).  The UI for the instructor
    dashboard enforces this constraint.

    Arguments:
        course_key (CourseKey): The course identifier.

    Keyword Arguments:
        is_enabled (boolean): If provided, enable/disable self-generated
            certificates for this course.

    """
279
    CertificateGenerationCourseSetting.set_self_generatation_enabled_for_course(course_key, is_enabled)
280 281 282 283 284
    cert_event_type = 'enabled' if is_enabled else 'disabled'
    event_name = '.'.join(['edx', 'certificate', 'generation', cert_event_type])
    tracker.emit(event_name, {
        'course_id': unicode(course_key),
    })
285 286 287 288 289 290
    if is_enabled:
        log.info(u"Enabled self-generated certificates for course '%s'.", unicode(course_key))
    else:
        log.info(u"Disabled self-generated certificates for course '%s'.", unicode(course_key))


291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
def is_certificate_invalid(student, course_key):
    """Check that whether the student in the course has been invalidated
    for receiving certificates.

    Arguments:
        student (user object): logged-in user
        course_key (CourseKey): The course identifier.

    Returns:
        Boolean denoting whether the student in the course is invalidated
        to receive certificates
    """
    is_invalid = False
    certificate = GeneratedCertificate.certificate_for_student(student, course_key)
    if certificate is not None:
        is_invalid = CertificateInvalidation.has_certificate_invalidation(student, course_key)

    return is_invalid


311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
def cert_generation_enabled(course_key):
    """Check whether certificate generation is enabled for a course.

    There are two "switches" that control whether self-generated certificates
    are enabled for a course:

    1) Whether the self-generated certificates feature is enabled.
    2) Whether self-generated certificates have been enabled for this particular course.

    Certificates are enabled for a course only when both switches
    are set to True.

    Arguments:
        course_key (CourseKey): The course identifier.

    Returns:
        boolean: Whether self-generated certificates are enabled
            for the course.

    """
    return (
        CertificateGenerationConfiguration.current().enabled and
333
        CertificateGenerationCourseSetting.is_self_generation_enabled_for_course(course_key)
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
    )


def generate_example_certificates(course_key):
    """Generate example certificates for a course.

    Example certificates are used to validate that certificates
    are configured correctly for the course.  Staff members can
    view the example certificates before enabling
    the self-generated certificates button for students.

    Several example certificates may be generated for a course.
    For example, if a course offers both verified and honor certificates,
    examples of both types of certificate will be generated.

    If an error occurs while starting the certificate generation
    job, the errors will be recorded in the database and
    can be retrieved using `example_certificate_status()`.

    Arguments:
        course_key (CourseKey): The course identifier.

    Returns:
        None

    """
    xqueue = XQueueCertInterface()
    for cert in ExampleCertificateSet.create_example_set(course_key):
        xqueue.add_example_cert(cert)


def example_certificates_status(course_key):
    """Check the status of example certificates for a course.

    This will check the *latest* example certificate task.
    This is generally what we care about in terms of enabling/disabling
    self-generated certificates for a course.

    Arguments:
        course_key (CourseKey): The course identifier.

    Returns:
        list

    Example Usage:

        >>> from certificates import api as certs_api
        >>> certs_api.example_certificate_status(course_key)
        [
            {
                'description': 'honor',
                'status': 'success',
                'download_url': 'http://www.example.com/abcd/honor_cert.pdf'
            },
            {
                'description': 'verified',
                'status': 'error',
                'error_reason': 'No template found!'
            }
        ]

    """
    return ExampleCertificateSet.latest_status(course_key)
397 398


399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
def _safe_course_key(course_key):
    if not isinstance(course_key, CourseKey):
        return CourseKey.from_string(course_key)
    return course_key


def _course_from_key(course_key):
    return CourseOverview.get_from_id(_safe_course_key(course_key))


def _certificate_html_url(user_id, course_id, uuid):
    if uuid:
        return reverse('certificates:render_cert_by_uuid', kwargs={'certificate_uuid': uuid})
    elif user_id and course_id:
        kwargs = {"user_id": str(user_id), "course_id": unicode(course_id)}
        return reverse('certificates:html_view', kwargs=kwargs)
    return ''


def _certificate_download_url(user_id, course_id):
    try:
        user_certificate = GeneratedCertificate.eligible_certificates.get(
            user=user_id,
            course_id=_safe_course_key(course_id)
        )
        return user_certificate.download_url
    except GeneratedCertificate.DoesNotExist:
        log.critical(
            'Unable to lookup certificate\n'
            'user id: %d\n'
            'course: %s', user_id, unicode(course_id)
        )
    return ''


def has_html_certificates_enabled(course):
    if not settings.FEATURES.get('CERTIFICATES_HTML_VIEW', False):
        return False
    return course.cert_html_view_enabled


440
def get_certificate_url(user_id=None, course_id=None, uuid=None):
441 442 443 444 445
    url = ''

    course = _course_from_key(course_id)
    if not course:
        return url
446

447 448 449 450
    if has_html_certificates_enabled(course):
        url = _certificate_html_url(user_id, course_id, uuid)
    else:
        url = _certificate_download_url(user_id, course_id)
451
    return url
452 453 454 455 456 457 458 459 460 461 462 463


def get_active_web_certificate(course, is_preview_mode=None):
    """
    Retrieves the active web certificate configuration for the specified course
    """
    certificates = getattr(course, 'certificates', '{}')
    configurations = certificates.get('certificates', [])
    for config in configurations:
        if config.get('is_active') or is_preview_mode:
            return config
    return None
464 465


466
def get_certificate_template(course_key, mode, language):
467
    """
468
    Retrieves the custom certificate template based on course_key, mode, and language.
469
    """
470
    template = None
471
    # fetch organization of the course
472
    org_id = get_course_organization_id(course_key)
473

474 475 476 477 478
    # only consider active templates
    active_templates = CertificateTemplate.objects.filter(is_active=True)

    if org_id and mode:  # get template by org, mode, and key
        org_mode_and_key_templates = active_templates.filter(
479 480
            organization_id=org_id,
            mode=mode,
481
            course_key=course_key
482
        )
483 484 485 486 487 488
        template = get_language_specific_template_or_default(language, org_mode_and_key_templates)

    # since no template matched that course_key, only consider templates with empty course_key
    empty_course_key_templates = active_templates.filter(course_key=CourseKeyField.Empty)
    if not template and org_id and mode:  # get template by org and mode
        org_and_mode_templates = empty_course_key_templates.filter(
489
            organization_id=org_id,
490
            mode=mode
491
        )
492 493 494
        template = get_language_specific_template_or_default(language, org_and_mode_templates)
    if not template and org_id:  # get template by only org
        org_templates = empty_course_key_templates.filter(
495
            organization_id=org_id,
496
            mode=None
497
        )
498 499 500
        template = get_language_specific_template_or_default(language, org_templates)
    if not template and mode:  # get template by only mode
        mode_templates = empty_course_key_templates.filter(
Zia Fazal committed
501
            organization_id=None,
502
            mode=mode
503
        )
504
        template = get_language_specific_template_or_default(language, mode_templates)
505
    return template if template else None
506

507 508 509 510 511 512 513

def get_language_specific_template_or_default(language, templates):
    """
    Returns templates that match passed in language.
    Returns default templates If no language matches, or language passed is None
    """
    two_letter_language = _get_two_letter_language_code(language)
514 515
    language_or_default_templates = list(templates.filter(Q(language=two_letter_language) | Q(language=None) | Q(language='')))
    language_specific_template = get_language_specific_template(two_letter_language, language_or_default_templates)
516 517 518
    if language_specific_template:
        return language_specific_template
    else:
519
        return get_all_languages_or_default_template(language_or_default_templates)
520 521 522 523 524 525 526 527 528


def get_language_specific_template(language, templates):
    for template in templates:
        if template.language == language:
            return template
    return None


529 530 531 532
def get_all_languages_or_default_template(templates):
    for template in templates:
        if template.language == '':
            return template
533

534 535 536
    return templates[0] if templates else None


537 538 539 540 541
def _get_two_letter_language_code(language_code):
    """
    Shortens language to only first two characters (e.g. es-419 becomes es)
    This is needed because Catalog returns locale language which is not always a 2 letter code.
    """
542 543 544 545 546 547
    if language_code is None:
        return None
    elif language_code == '':
        return ''
    else:
        return language_code[:2]
548 549


550 551 552 553 554 555 556 557 558 559 560 561 562 563
def emit_certificate_event(event_name, user, course_id, course=None, event_data=None):
    """
    Emits certificate event.
    """
    event_name = '.'.join(['edx', 'certificate', event_name])
    if course is None:
        course = modulestore().get_course(course_id, depth=0)
    context = {
        'org_id': course.org,
        'course_id': unicode(course_id)
    }
    data = {
        'user_id': user.id,
        'course_id': unicode(course_id),
564
        'certificate_url': get_certificate_url(user.id, course_id)
565 566 567 568 569 570
    }
    event_data = event_data or {}
    event_data.update(data)

    with tracker.get_tracker().context(event_name, context):
        tracker.emit(event_name, event_data)
571 572 573 574 575 576 577 578 579 580 581 582 583


def get_asset_url_by_slug(asset_slug):
    """
    Returns certificate template asset url for given asset_slug.
    """
    asset_url = ''
    try:
        template_asset = CertificateTemplateAsset.objects.get(asset_slug=asset_slug)
        asset_url = template_asset.asset.url
    except CertificateTemplateAsset.DoesNotExist:
        pass
    return asset_url
584 585 586 587 588


def get_certificate_header_context(is_secure=True):
    """
    Return data to be used in Certificate Header,
589
    data returned should be customized according to the site configuration.
590 591
    """
    data = dict(
592
        logo_src=branding_api.get_logo_url(is_secure),
593 594 595 596 597 598 599 600 601
        logo_url=branding_api.get_base_url(is_secure),
    )

    return data


def get_certificate_footer_context():
    """
    Return data to be used in Certificate Footer,
602
    data returned should be customized according to the site configuration.
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
    """
    data = dict()

    # get Terms of Service and Honor Code page url
    terms_of_service_and_honor_code = branding_api.get_tos_and_honor_code_url()
    if terms_of_service_and_honor_code != branding_api.EMPTY_URL:
        data.update({'company_tos_url': terms_of_service_and_honor_code})

    # get Privacy Policy page url
    privacy_policy = branding_api.get_privacy_url()
    if privacy_policy != branding_api.EMPTY_URL:
        data.update({'company_privacy_url': privacy_policy})

    # get About page url
    about = branding_api.get_about_url()
    if about != branding_api.EMPTY_URL:
        data.update({'company_about_url': about})

    return data