# Path: lrs/models.py
# class ActivityState(models.Model):
#     state_id = models.CharField(max_length=MAX_URL_LENGTH)
#     updated = models.DateTimeField(
#         auto_now_add=True, blank=True, db_index=True)
#     activity_id = models.CharField(max_length=MAX_URL_LENGTH, db_index=True)
#     registration_id = models.CharField(max_length=40, db_index=True)
#     content_type = models.CharField(max_length=255, blank=True)
#     etag = models.CharField(max_length=50, blank=True)
#     agent = models.ForeignKey(Agent, on_delete=models.CASCADE)
#     json_state = JSONField(default=dict)
#     state = models.FileField(upload_to=ACTIVITY_STATE_UPLOAD_TO, null=True)
# 
#     def delete(self, *args, **kwargs):
#         if self.state:
#             self.state.delete()
#         super(ActivityState, self).delete(*args, **kwargs)
# 
#     def save(self, *args, **kwargs):
# 
#         if isinstance(self.json_state, bytes):
#             self.json_state = self.json_state.decode("utf-8")
# 
#         if self.json_state and isinstance(self.json_state, str):
#             try:
#                 json.loads(self.json_state)
#             except Exception:
#                 try:
#                     ast.literal_eval(self.json_state)
#                 except Exception:
#                     raise BadRequest(f"[1] The Activity State body is not valid JSON, instead got:: {type(self.json_state)}:: {self.json_state}")
#         elif self.json_state and isinstance(self.json_state, bytes):
#             raise BadRequest(f"[2] The Activity State body is not valid JSON, instead got:: {type(self.json_state)}:: {self.json_state}")
#         super(ActivityState, 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.utils.timezone import utc
from ..models import ActivityState
from ..exceptions import IDNotFoundError
from ..utils import etag
        fn = "%s_%s_%s" % (s.agent_id, s.activity_id,
                           request_dict.get('filename', s.id))
        s.state.save(fn, state)

        s.save()

    def get_state_set(self, activity_id, registration, since):
        if registration:
            # Registration and since
            if since:
                state_set = self.Agent.activitystate_set.filter(
                    activity_id=activity_id, registration_id=registration, updated__gt=since)
            # Registration
            else:
                state_set = self.Agent.activitystate_set.filter(
                    activity_id=activity_id, registration_id=registration)
        else:
            # Since
            if since:
                state_set = self.Agent.activitystate_set.filter(
                    activity_id=activity_id, updated__gt=since)
            # Neither
            else:
                state_set = self.Agent.activitystate_set.filter(
                    activity_id=activity_id)
        return state_set

    def post_state(self, request_dict):
        registration = request_dict['params'].get('registration', None)
        if registration:

# Path: lrs/models.py
# class ActivityState(models.Model):
#     state_id = models.CharField(max_length=MAX_URL_LENGTH)
#     updated = models.DateTimeField(
#         auto_now_add=True, blank=True, db_index=True)
#     activity_id = models.CharField(max_length=MAX_URL_LENGTH, db_index=True)
#     registration_id = models.CharField(max_length=40, db_index=True)
#     content_type = models.CharField(max_length=255, blank=True)
#     etag = models.CharField(max_length=50, blank=True)
#     agent = models.ForeignKey(Agent, on_delete=models.CASCADE)
#     json_state = JSONField(default=dict)
#     state = models.FileField(upload_to=ACTIVITY_STATE_UPLOAD_TO, null=True)
# 
#     def delete(self, *args, **kwargs):
#         if self.state:
#             self.state.delete()
#         super(ActivityState, self).delete(*args, **kwargs)
# 
#     def save(self, *args, **kwargs):
# 
#         if isinstance(self.json_state, bytes):
#             self.json_state = self.json_state.decode("utf-8")
# 
#         if self.json_state and isinstance(self.json_state, str):
#             try:
#                 json.loads(self.json_state)
#             except Exception:
#                 try:
#                     ast.literal_eval(self.json_state)
#                 except Exception:
#                     raise BadRequest(f"[1] The Activity State body is not valid JSON, instead got:: {type(self.json_state)}:: {self.json_state}")
#         elif self.json_state and isinstance(self.json_state, bytes):
#             raise BadRequest(f"[2] The Activity State body is not valid JSON, instead got:: {type(self.json_state)}:: {self.json_state}")
#         super(ActivityState, 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/ActivityStateManager.py
import datetime
import json
from django.core.files.base import ContentFile
from django.utils.timezone import utc
from ..models import ActivityState
from ..exceptions import IDNotFoundError
from ..utils import etag

        fn = "%s_%s_%s" % (s.agent_id, s.activity_id,
                           request_dict.get('filename', s.id))
        s.state.save(fn, state)

        s.save()

    def get_state_set(self, activity_id, registration, since):
        if registration:
            # Registration and since
            if since:
                state_set = self.Agent.activitystate_set.filter(
                    activity_id=activity_id, registration_id=registration, updated__gt=since)
            # Registration
            else:
                state_set = self.Agent.activitystate_set.filter(
                    activity_id=activity_id, registration_id=registration)
        else:
            # Since
            if since:
                state_set = self.Agent.activitystate_set.filter(
                    activity_id=activity_id, updated__gt=since)
            # Neither
            else:
                state_set = self.Agent.activitystate_set.filter(
                    activity_id=activity_id)
        return state_set

    def post_state(self, request_dict):
        registration = request_dict['params'].get('registration', None)
        if registration:

            s, created = ActivityState.objects.get_or_create(state_id=request_dict['params']['stateId'], agent=self.Agent,