# Path: lrs/models.py
# class AgentProfile(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)
#     content_type = models.CharField(max_length=255, blank=True)
#     etag = models.CharField(max_length=50, blank=True)
#     agent = models.ForeignKey(Agent, db_index=True, on_delete=models.CASCADE)
#     json_profile = JSONField(default=dict)
#     profile = models.FileField(upload_to=AGENT_PROFILE_UPLOAD_TO, null=True)
# 
#     def delete(self, *args, **kwargs):
#         if self.profile:
#             self.profile.delete()
#         super(AgentProfile, 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 Agent 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 Agent Profile body is not valid JSON, instead got:: {type(self.json_profile)}:: {self.json_profile}")
#         super(AgentProfile, 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 json
import datetime
from django.core.files.base import ContentFile
from django.core.exceptions import ValidationError
from django.utils.timezone import utc
from ..models import AgentProfile
from ..exceptions import IDNotFoundError, ParamError
from ..utils import etag




class AgentProfileManager():

    def __init__(self, agent):
        self.Agent = agent

    def save_non_json_profile(self, p, profile, request_dict):
        p.content_type = request_dict['headers']['CONTENT_TYPE']

# Path: lrs/models.py
# class AgentProfile(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)
#     content_type = models.CharField(max_length=255, blank=True)
#     etag = models.CharField(max_length=50, blank=True)
#     agent = models.ForeignKey(Agent, db_index=True, on_delete=models.CASCADE)
#     json_profile = JSONField(default=dict)
#     profile = models.FileField(upload_to=AGENT_PROFILE_UPLOAD_TO, null=True)
# 
#     def delete(self, *args, **kwargs):
#         if self.profile:
#             self.profile.delete()
#         super(AgentProfile, 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 Agent 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 Agent Profile body is not valid JSON, instead got:: {type(self.json_profile)}:: {self.json_profile}")
#         super(AgentProfile, 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/AgentProfileManager.py
import json
import datetime
from django.core.files.base import ContentFile
from django.core.exceptions import ValidationError
from django.utils.timezone import utc
from ..models import AgentProfile
from ..exceptions import IDNotFoundError, ParamError
from ..utils import etag





class AgentProfileManager():

    def __init__(self, agent):
        self.Agent = agent

    def save_non_json_profile(self, p, profile, request_dict):
        p.content_type = request_dict['headers']['CONTENT_TYPE']

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