def test_has_equal_properties(): """ Assert that has_equal_properties returns True if an object has equivalent properties to a given dict """ obj = SimpleNamespace(a=1, b=2, c=3) assert has_equal_properties(obj, {}) is True assert has_equal_properties(obj, dict(a=1, b=2)) is True assert has_equal_properties(obj, dict(a=1, b=2, c=3)) is True assert has_equal_properties(obj, dict(a=2)) is False assert has_equal_properties(obj, dict(d=4)) is False
def create_or_update_enrollment(model_cls, defaults=None, **kwargs): """Creates or updates an enrollment record""" defaults = {**(defaults or {}), "active": True, "change_status": None} created = False enrollment = model_cls.all_objects.filter( **kwargs).order_by("-created_on").first() if not enrollment: enrollment = model_cls.objects.create(**{**defaults, **kwargs}) created = True elif enrollment and not has_equal_properties(enrollment, defaults): for field_name, field_value in defaults.items(): setattr(enrollment, field_name, field_value) enrollment.save_and_log(None) return enrollment, created
def ensure_course_run_grade(user, course_run, edx_grade, should_update=False): """ Ensure that the local grades repository has the grade for the User/CourseRun combination supplied. Args: user (user.models.User): The user for whom the grade is being synced course_run (courses.models.CourseRun): The course run for which the grade is created edx_grade (edx_api.grades.models.UserCurrentGrade): The OpenEdx grade object should_update (bool): Update the local grade record if it exists Returns: (courses.models.CourseRunGrade, bool, bool) that depicts the CourseRunGrade, created and updated values """ grade_properties = { "grade": edx_grade.percent, "passed": edx_grade.passed, "letter_grade": edx_grade.letter_grade, } updated = False if should_update: with transaction.atomic(): run_grade, created = CourseRunGrade.objects.select_for_update().get_or_create( course_run=course_run, user=user, defaults=grade_properties ) if ( not created and not run_grade.set_by_admin and not has_equal_properties(run_grade, grade_properties) ): # Perform actual update now. run_grade.grade = edx_grade.percent run_grade.passed = edx_grade.passed run_grade.letter_grade = edx_grade.letter_grade run_grade.save_and_log(None) updated = True else: run_grade, created = CourseRunGrade.objects.get_or_create( course_run=course_run, user=user, defaults=grade_properties ) return run_grade, created, updated
def _deserialize_product(self, courseware_obj, product_data): """ Attempts to deserialize and save Product/ProductVersion data, and creates those objects if they don't exist. """ product, created = Product.objects.get_or_create( content_type=ContentType.objects.get_for_model( courseware_obj.__class__), object_id=courseware_obj.id, ) if created: self.seed_result.add_created(product) latest_version = product.latest_version if not latest_version or not has_equal_properties( latest_version, product_data): new_version = ProductVersion.objects.create(product=product, **product_data) self.seed_result.add_created(new_version) return new_version else: self.seed_result.add_ignored(latest_version) return latest_version