import urllib, urllib2
import xml
import hashlib
import socket
from datetime import datetime, timedelta
from xml.dom import minidom
from django.db import models
from eve_proxy.exceptions import *
import settings

# You generally never want to change this unless you have a very good reason.

try:
    API_URL = getattr(settings, 'EVE_API_URL')
except AttributeError:
    API_URL = 'http://api.eve-online.com'

# Errors to rollback if we have a cached version of the document
# Errors 500-999 at the moment, this can be trimmed down as needed
ROLLBACK_ERRORS = range(500, 999)

class CachedDocumentManager(models.Manager):
    """
    This manager handles querying or retrieving CachedDocuments.
    """

    def construct_url(self, url_path, params):

         # Valid arguments for EVE API Calls
        allowed_params = ['userid', 'apikey', 'characterid', 'version', 'names', 'ids', 'corporationid', 'beforerefid', 'accountkey']

        if len(params):
            for k, v in params.items():
                del params[k]
                if k.lower() in allowed_params:
                   params[k.lower()] = v
            url = "%s%s?%s" % (API_URL, url_path, urllib.urlencode(params))
        else:
            url = "%s%s" % (API_URL, url_path)

        return url

    def api_query(self, url_path, params={}, no_cache=False, exceptions=True):
        """
        Transparently handles querying EVE API or retrieving the document from
        the cache.
        
        Arguments:
        url_path: (string) Path to the EVE API page to query. For example:
                           /eve/ErrorList.xml.aspx
        params: (dictionary/string) A dictionary of extra parameters to include.
                                    May also be a string representation of
                                    the query: userID=1&characterID=xxxxxxxx
        """

        url = self.construct_url(url_path, params)

        try:
            doc = super(CachedDocumentManager, self).get_query_set().get(url_path=url)
            print "Doc found"
        except self.model.MultipleObjectsReturned:
            super(CachedDocumentManager, self).get_query_set().filter(url_path=url).delete()
            doc = CachedDocument(url_path=url)
            print "Multiple doc"
        except self.model.DoesNotExist:
            doc = CachedDocument(url_path=url)
            print "No doc found"

        if doc.pk and no_cache:
            doc.delete()
            doc = CachedDocument(url_path=url)

        if not doc.cached_until or datetime.utcnow() > doc.cached_until:
            print "Doc expired"

            req = urllib2.Request(url)
            req.add_header('CCP-Contact', 'matalok@pleaseignore.com')
            try:
                conn = urllib2.urlopen(req)
            except urllib2.HTTPError, e:
                raise DocumentRetrievalError(e.code)
            except urllib2.URLError, e:
                raise DocumentRetrievalError(e.reason)

            doc.body = unicode(conn.read(), 'utf-8')
            doc.time_retrieved = datetime.utcnow()

            error = 0
            try:
                # Parse the response via minidom
                dom = minidom.parseString(doc.body.encode('utf-8'))
            except:
                doc.cached_until = datetime.utcnow()
            else:
                doc.cached_until = dom.getElementsByTagName('cachedUntil')[0].childNodes[0].nodeValue
                enode = dom.getElementsByTagName('error')
                if enode:
                   error = enode[0].getAttribute('code')

            # If we have a error in the ignored error list use the cached doc, otherwise return the new doc
            if not error or not error in ROLLBACK_ERRORS:
                doc.save()
                doc = self.get(id=doc.pk)

            # If this is user related, write a log instance
            if params and params.get('userid', None):
                try:
                    v = int(params.get('userid', None))
                except:
                    pass
                else:
                    ApiAccessLog(userid=v, service='Unknown', time_access=doc.time_retrieved, document=url).save()

        return doc

class CachedDocument(models.Model):
    """
    This is a cached XML document from the EVE API.
    """
    url_path = models.CharField(max_length=255, unique=True)
    body = models.TextField()
    time_retrieved = models.DateTimeField(blank=True, null=True)
    cached_until = models.DateTimeField(blank=True, null=True)

    # The custom manager handles the querying.
    objects = CachedDocumentManager()

    class Meta:
        verbose_name = 'Cached Document'
        verbose_name_plural = 'Cached Documents'
        ordering = ['time_retrieved']

class ApiAccessLog(models.Model):
    """
    Provides a list of API accesses made by applications or Auth
    """
    userid = models.IntegerField()
    service = models.CharField(max_length=255)
    time_access = models.DateTimeField()
    document = models.CharField(max_length=255)

    class Meta:
        verbose_name = 'API Access Log'
        verbose_name_plural = 'API Access Logs'
        ordering = ['time_access']

@@ -2011,38 +2011,8 @@
 rl)%0A
-            print %22Doc found%22%0A
     
@@ -2058,16 +2058,16 @@
 turned:%0A
+
         
@@ -2202,41 +2202,8 @@
 rl)%0A
-            print %22Multiple doc%22%0A
     
@@ -2288,41 +2288,8 @@
 url)
-%0A            print %22No doc found%22
 %0A%0A  
@@ -2383,32 +2383,32 @@
 (url_path=url)%0A%0A
+
         if not d
@@ -2467,40 +2467,8 @@
 til:
-%0A            print %22Doc expired%22
 %0A%0A  

Remove debug statements