[MICROBA-1139]

* Change order of object Manager declarations in the GeneratedCertificate model. Declaration order matters and It appears that having the "base" `objects` declaration after the other Custom Managers creates an issue where I cannot access `objects` in a migration without this change.
* As part of the manager migration there is a cleanup function that resets the `error_reason` field to an empty string for all certificates in the `downloadable` state that currently have an error listed in the record.
This commit is contained in:
Justin Hynes
2021-04-26 07:41:04 -04:00
parent d7a4947ea9
commit 563b48c50c
2 changed files with 36 additions and 6 deletions

View File

@@ -0,0 +1,30 @@
# Generated by Django 2.2.20 on 2021-04-27 18:12
from django.db import migrations
class Migration(migrations.Migration):
"""
Clean records in the `CERTIFICATES_GENERATEDCERTIFICATE` table that are in the downloadable state but also have
old errors still part of their certificate record.
As part of this migration we are also altering the Managers for the GeneratedCertificate model. We need the ability
to access the `objects` attribute for the cleanup.
"""
def cleanup_certificate_records(apps, schema_editor):
GeneratedCertificate = apps.get_model('certificates', 'GeneratedCertificate')
GeneratedCertificate.objects.filter(status='downloadable').exclude(error_reason='').update(error_reason='')
dependencies = [
('certificates', '0024_delete_allowlistgenerationconfiguration'),
]
operations = [
migrations.AlterModelManagers(
name='generatedcertificate',
managers=[
],
),
migrations.RunPython(cleanup_certificate_records, reverse_code=migrations.RunPython.noop)
]

View File

@@ -202,7 +202,7 @@ class EligibleAvailableCertificateManager(EligibleCertificateManager):
A manager for `GeneratedCertificate` models that automatically
filters out ineligible certs and any linked to nonexistent courses.
Adds to the super class filtering ot also exclude certificates for
Adds to the super class filtering to also exclude certificates for
courses that do not have a corresponding CourseOverview.
"""
@@ -233,6 +233,11 @@ class GeneratedCertificate(models.Model):
# the course_modes app is loaded, resulting in a Django deprecation warning.
from common.djangoapps.course_modes.models import CourseMode # pylint: disable=reimported
# Normal object manager, which should only be used when ineligible
# certificates (i.e. new audit certs) should be included in the
# results. Django requires us to explicitly declare this.
objects = models.Manager()
# Only returns eligible certificates. This should be used in
# preference to the default `objects` manager in most cases.
eligible_certificates = EligibleCertificateManager()
@@ -241,11 +246,6 @@ class GeneratedCertificate(models.Model):
# associated CourseOverview
eligible_available_certificates = EligibleAvailableCertificateManager()
# Normal object manager, which should only be used when ineligible
# certificates (i.e. new audit certs) should be included in the
# results. Django requires us to explicitly declare this.
objects = models.Manager()
MODES = Choices(
'verified',
'honor',