# 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",
#             ),
#         )
# 
# class Uploader(models.Model):
#     # implicit 'id' IntegerField being used.
#     key = models.CharField(
#         maxlength=8,
#     )
#     name = models.CharField(
#         maxlength=64,
#     )
# 
# class PickledPut(models.Model):
#     filename = models.CharField(
#         maxlength=128,
#         primary_key=True,
#     )
#     docket = models.BooleanField(
#         default=0,
#     )
#     ready = models.BooleanField(
#         default=0,
#     )
#     processing = models.BooleanField(
#         default=0,
#     )


import re
import logging
import csv
import InternetArchiveCommon as IACommon
import UploadHandler
import BucketLockManager
import DocumentManager
import datetime
import os
import pytz
from django.http import HttpResponse, HttpResponseForbidden, \
    HttpResponseServerError
from django.utils import simplejson
from uploads.models import Document, Uploader, PickledPut
from django.conf import settings as config
    if not tpq or api_key not in api_keys:
        return HttpResponseForbidden()

    response = HttpResponse(mimetype='text/csv')
    tpq = float(tpq)
    tpq = datetime.datetime.fromtimestamp(tpq)
    docs = Document.objects.filter(lastdate__gt=tpq) | \
           Document.objects.filter(modified__gt=tpq)
    columns = ['court', 'casenum', 'docnum', 'subdocnum', 'available']
    docs = (docs.values(*columns)
            .order_by('court', 'casenum', 'docnum', 'subdocnum'))
    w = csv.DictWriter(response, fieldnames=columns)
    for doc in docs:
        w.writerow(doc)
    return response


def heartbeat(request):
    """
    This view is meant to be used for external monitoring, to ensure uptime
    """
    try:
        key = request.GET["key"].strip()
    except KeyError:
        # Fail.  Missing required arguments.
        return HttpResponseForbidden("403 Forbidden")

    if key != config.HEARTBEAT_KEY:
        return HttpResponseForbidden("403 Forbidden")


# 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",
#             ),
#         )
# 
# class Uploader(models.Model):
#     # implicit 'id' IntegerField being used.
#     key = models.CharField(
#         maxlength=8,
#     )
#     name = models.CharField(
#         maxlength=64,
#     )
# 
# class PickledPut(models.Model):
#     filename = models.CharField(
#         maxlength=128,
#         primary_key=True,
#     )
#     docket = models.BooleanField(
#         default=0,
#     )
#     ready = models.BooleanField(
#         default=0,
#     )
#     processing = models.BooleanField(
#         default=0,
#     )


# Path: uploads/views.py
import re
import logging
import csv
import InternetArchiveCommon as IACommon
import UploadHandler
import BucketLockManager
import DocumentManager
import datetime
import os
import pytz
from django.http import HttpResponse, HttpResponseForbidden, \
    HttpResponseServerError
from django.utils import simplejson
from uploads.models import Document, Uploader, PickledPut
from django.conf import settings as config

    if not tpq or api_key not in api_keys:
        return HttpResponseForbidden()

    response = HttpResponse(mimetype='text/csv')
    tpq = float(tpq)
    tpq = datetime.datetime.fromtimestamp(tpq)
    docs = Document.objects.filter(lastdate__gt=tpq) | \
           Document.objects.filter(modified__gt=tpq)
    columns = ['court', 'casenum', 'docnum', 'subdocnum', 'available']
    docs = (docs.values(*columns)
            .order_by('court', 'casenum', 'docnum', 'subdocnum'))
    w = csv.DictWriter(response, fieldnames=columns)
    for doc in docs:
        w.writerow(doc)
    return response


def heartbeat(request):
    """
    This view is meant to be used for external monitoring, to ensure uptime
    """
    try:
        key = request.GET["key"].strip()
    except KeyError:
        # Fail.  Missing required arguments.
        return HttpResponseForbidden("403 Forbidden")

    if key != config.HEARTBEAT_KEY:
        return HttpResponseForbidden("403 Forbidden")


    query = Document.objects.filter(court='cand', casenum='215270')