# Path: lrs/models.py
# class ActivityProfile(models.Model):
#     profile_id = models.CharField(max_length=MAX_URL_LENGTH, db_index=True)
#     updated = models.DateTimeField(
#         auto_now_add=True, blank=True, db_index=True)
#     activity_id = models.CharField(max_length=MAX_URL_LENGTH, db_index=True)
#     content_type = models.CharField(max_length=255, blank=True)
#     etag = models.CharField(max_length=50, blank=True)
#     json_profile = JSONField(default=dict)
#     profile = models.FileField(upload_to=ACTIVITY_PROFILE_UPLOAD_TO, null=True)
# 
#     def delete(self, *args, **kwargs):
#         if self.profile:
#             self.profile.delete()
#         super(ActivityProfile, self).delete(*args, **kwargs)
# 
#     def save(self, *args, **kwargs):
# 
#         if isinstance(self.json_profile, bytes):
#             self.json_profile = self.json_profile.decode("utf-8")
# 
#         if self.json_profile and isinstance(self.json_profile, str):
#             try:
#                 json.loads(self.json_profile)
#             except Exception:
#                 try:
#                     ast.literal_eval(self.json_profile)
#                 except Exception:
#                     raise BadRequest(f"[1] The Activity Profile body is not valid JSON, instead got:: {type(self.json_profile)}:: {self.json_profile}")
#         elif self.json_profile and not isinstance(self.json_profile, str):
#             raise BadRequest(f"[2] The Activity Profile body is not valid JSON, instead got:: {type(self.json_profile)}:: {self.json_profile}")
#         super(ActivityProfile, self).save(*args, **kwargs)
# 
# Path: lrs/utils/etag.py
# IF_MATCH = "HTTP_IF_MATCH"
# IF_NONE_MATCH = "HTTP_IF_NONE_MATCH"
# def create_tag(resource):
# def get_etag_info(headers):
# def check_preconditions(request, contents, created, required=True):
#     def __init__(self, msg):
#     def __str__(self):
#     def __init__(self, msg):
#     def __str__(self):
#     def __init__(self, msg):
#     def __str__(self):
# class MissingEtagInfo(BadRequest):
# class MissingEtagInfoExists(Conflict):
# class EtagPreconditionFail(PreconditionFail):


import datetime
import json
from django.core.files.base import ContentFile
from django.core.exceptions import ValidationError
from django.utils.timezone import utc
from ..models import ActivityProfile
from ..exceptions import IDNotFoundError, ParamError
from ..utils import etag




class ActivityProfileManager():

    def save_non_json_profile(self, p, created, profile, request_dict):
        # Save profile content type based on incoming content type header and
        # create etag
        p.content_type = request_dict['headers']['CONTENT_TYPE']

# Path: lrs/models.py
# class ActivityProfile(models.Model):
#     profile_id = models.CharField(max_length=MAX_URL_LENGTH, db_index=True)
#     updated = models.DateTimeField(
#         auto_now_add=True, blank=True, db_index=True)
#     activity_id = models.CharField(max_length=MAX_URL_LENGTH, db_index=True)
#     content_type = models.CharField(max_length=255, blank=True)
#     etag = models.CharField(max_length=50, blank=True)
#     json_profile = JSONField(default=dict)
#     profile = models.FileField(upload_to=ACTIVITY_PROFILE_UPLOAD_TO, null=True)
# 
#     def delete(self, *args, **kwargs):
#         if self.profile:
#             self.profile.delete()
#         super(ActivityProfile, self).delete(*args, **kwargs)
# 
#     def save(self, *args, **kwargs):
# 
#         if isinstance(self.json_profile, bytes):
#             self.json_profile = self.json_profile.decode("utf-8")
# 
#         if self.json_profile and isinstance(self.json_profile, str):
#             try:
#                 json.loads(self.json_profile)
#             except Exception:
#                 try:
#                     ast.literal_eval(self.json_profile)
#                 except Exception:
#                     raise BadRequest(f"[1] The Activity Profile body is not valid JSON, instead got:: {type(self.json_profile)}:: {self.json_profile}")
#         elif self.json_profile and not isinstance(self.json_profile, str):
#             raise BadRequest(f"[2] The Activity Profile body is not valid JSON, instead got:: {type(self.json_profile)}:: {self.json_profile}")
#         super(ActivityProfile, self).save(*args, **kwargs)
# 
# Path: lrs/utils/etag.py
# IF_MATCH = "HTTP_IF_MATCH"
# IF_NONE_MATCH = "HTTP_IF_NONE_MATCH"
# def create_tag(resource):
# def get_etag_info(headers):
# def check_preconditions(request, contents, created, required=True):
#     def __init__(self, msg):
#     def __str__(self):
#     def __init__(self, msg):
#     def __str__(self):
#     def __init__(self, msg):
#     def __str__(self):
# class MissingEtagInfo(BadRequest):
# class MissingEtagInfoExists(Conflict):
# class EtagPreconditionFail(PreconditionFail):


# Path: lrs/managers/ActivityProfileManager.py
import datetime
import json
from django.core.files.base import ContentFile
from django.core.exceptions import ValidationError
from django.utils.timezone import utc
from ..models import ActivityProfile
from ..exceptions import IDNotFoundError, ParamError
from ..utils import etag





class ActivityProfileManager():

    def save_non_json_profile(self, p, created, profile, request_dict):
        # Save profile content type based on incoming content type header and
        # create etag
        p.content_type = request_dict['headers']['CONTENT_TYPE']

        p.etag = etag.create_tag(profile.read())