# Path: uploads/models.py
# class Document(models.Model):
#     """Note that a number of fields have indexes in the database despite
#     lacking them here:
# 
#     mysql> show index from uploads_document;
#         +------------------+-----------------+-------------+-------------+
#         | Table            | Key_name        | Column_name | Cardinality |
#         +------------------+-----------------+-------------+-------------+
#         | uploads_document | PRIMARY         | id          |    26652929 |
#         | uploads_document | court           | court       |         206 |
#         | uploads_document | court           | casenum     |     1025112 |
#         | uploads_document | court           | docnum      |    26652929 |
#         | uploads_document | court           | subdocnum   |    26652929 |
#         | uploads_document | docid_idx       | docid       |    26652929 |
#         | uploads_document | document_free_i | free_import |           2 |
#         | uploads_document | lastdateindex   | lastdate    |    26652929 |
#         | uploads_document | modifiedindex   | modified    |     6663232 |
#         +------------------+-----------------+-------------+-------------+
# 
#     (https://dev.mysql.com/doc/refman/5.0/en/show-index.html)
# 
#     """
#     court = models.CharField(
#         maxlength=10,
#     )
#     casenum = models.CharField(
#         maxlength=30,
#     )
#     docnum = models.CharField(
#         maxlength=30,
#     )
#     subdocnum = models.CharField(
#         maxlength=30,
#     )
#     docid = models.CharField(
#         maxlength=32,
#         null=True,
#         blank=True,
#         db_index=True,
#     )
#     de_seq_num = models.PositiveIntegerField(
#         null=True,
#         blank=True,
#     )
#     dm_id = models.PositiveIntegerField(
#         null=True,
#         blank=True,
#     )
#     sha1 = models.CharField(
#         maxlength=40,
#         null=True,
#         blank=True,
#     )
#     available = models.BooleanField(
#         default=0,
#     )
#     lastdate = models.DateTimeField(
#         null=True,
#         blank=True,
#         db_index=True,
#     )
#     modified = models.DateTimeField(
#         null=True,
#         blank=True,
#         auto_now_add=True,
#         db_index=True,
#     )
#     free_import = models.BooleanField(
#         default=0,
#         db_index=True,
#     )
#     team_name = models.CharField(
#         maxlength=40,
#         null=True,
#         blank=True
#     )
# 
#     class Meta:
#         unique_together = (
#             (
#                 "court",
#                 "casenum",
#                 "docnum",
#                 "subdocnum",
#             ),
#         )


import logging
import InternetArchiveCommon as IACommon
import ParsePacer
from MySQLdb import IntegrityError, OperationalError
from uploads.models import Document
from DocketXML import DocketXML
            logging.error("update_local_db: could not save %s %s %s %s"
                          % (court, casenum, docnum, subdocnum))


def mark_as_available(filename):
    docmeta = IACommon.get_meta_from_filename(filename)

    docquery = Document.objects.filter(court=docmeta["court"],
                                       casenum=docmeta["casenum"],
                                       docnum=docmeta["docnum"],
                                       subdocnum=docmeta["subdocnum"])

    try:
        docentry = docquery[0]
    except IndexError:
        # Unexpected case.  No Document entry
        logging.error("mark_as_available: no entry for %s." % (filename))
    else:
        docentry.available = 1
        try:
            docentry.save()
        except IntegrityError:
            logging.error("mark_as_available: could not save %s."
                          % (filename))


def handle_adddocmeta(docid, court, casenum, de_seq_num, dm_id,
                      docnum, subdocnum):
    docid = ParsePacer.coerce_docid(docid)


# Path: uploads/models.py
# class Document(models.Model):
#     """Note that a number of fields have indexes in the database despite
#     lacking them here:
# 
#     mysql> show index from uploads_document;
#         +------------------+-----------------+-------------+-------------+
#         | Table            | Key_name        | Column_name | Cardinality |
#         +------------------+-----------------+-------------+-------------+
#         | uploads_document | PRIMARY         | id          |    26652929 |
#         | uploads_document | court           | court       |         206 |
#         | uploads_document | court           | casenum     |     1025112 |
#         | uploads_document | court           | docnum      |    26652929 |
#         | uploads_document | court           | subdocnum   |    26652929 |
#         | uploads_document | docid_idx       | docid       |    26652929 |
#         | uploads_document | document_free_i | free_import |           2 |
#         | uploads_document | lastdateindex   | lastdate    |    26652929 |
#         | uploads_document | modifiedindex   | modified    |     6663232 |
#         +------------------+-----------------+-------------+-------------+
# 
#     (https://dev.mysql.com/doc/refman/5.0/en/show-index.html)
# 
#     """
#     court = models.CharField(
#         maxlength=10,
#     )
#     casenum = models.CharField(
#         maxlength=30,
#     )
#     docnum = models.CharField(
#         maxlength=30,
#     )
#     subdocnum = models.CharField(
#         maxlength=30,
#     )
#     docid = models.CharField(
#         maxlength=32,
#         null=True,
#         blank=True,
#         db_index=True,
#     )
#     de_seq_num = models.PositiveIntegerField(
#         null=True,
#         blank=True,
#     )
#     dm_id = models.PositiveIntegerField(
#         null=True,
#         blank=True,
#     )
#     sha1 = models.CharField(
#         maxlength=40,
#         null=True,
#         blank=True,
#     )
#     available = models.BooleanField(
#         default=0,
#     )
#     lastdate = models.DateTimeField(
#         null=True,
#         blank=True,
#         db_index=True,
#     )
#     modified = models.DateTimeField(
#         null=True,
#         blank=True,
#         auto_now_add=True,
#         db_index=True,
#     )
#     free_import = models.BooleanField(
#         default=0,
#         db_index=True,
#     )
#     team_name = models.CharField(
#         maxlength=40,
#         null=True,
#         blank=True
#     )
# 
#     class Meta:
#         unique_together = (
#             (
#                 "court",
#                 "casenum",
#                 "docnum",
#                 "subdocnum",
#             ),
#         )


# Path: uploads/DocumentManager.py
import logging
import InternetArchiveCommon as IACommon
import ParsePacer
from MySQLdb import IntegrityError, OperationalError
from uploads.models import Document
from DocketXML import DocketXML

            logging.error("update_local_db: could not save %s %s %s %s"
                          % (court, casenum, docnum, subdocnum))


def mark_as_available(filename):
    docmeta = IACommon.get_meta_from_filename(filename)

    docquery = Document.objects.filter(court=docmeta["court"],
                                       casenum=docmeta["casenum"],
                                       docnum=docmeta["docnum"],
                                       subdocnum=docmeta["subdocnum"])

    try:
        docentry = docquery[0]
    except IndexError:
        # Unexpected case.  No Document entry
        logging.error("mark_as_available: no entry for %s." % (filename))
    else:
        docentry.available = 1
        try:
            docentry.save()
        except IntegrityError:
            logging.error("mark_as_available: could not save %s."
                          % (filename))


def handle_adddocmeta(docid, court, casenum, de_seq_num, dm_id,
                      docnum, subdocnum):
    docid = ParsePacer.coerce_docid(docid)


    query = Document.objects.filter(court=court, casenum=casenum,