Advertisement
Guest User

keygen + cachemodel

a guest
May 26th, 2015
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.36 KB | None | 0 0
  1. # generate cachkey for
  2. def generate_cache_key(key, category=None, api=None, **kwargs):
  3.     cache_key = "%s" % (key)
  4.     if category:
  5.         cache_key += "_%s" % category
  6.     if api:
  7.         cache_key += "_%d" % api.pk
  8.  
  9.     for key, value in kwargs.iteritems():
  10.         cache_key += "_%s_%s" % (key, value)
  11.     return cache_key
  12.  
  13.  
  14.  
  15. class EveApiCache(Task):
  16.  
  17.     """
  18.    set cache task. Creating new caches, and updating old ones.
  19.    """
  20.  
  21.     ACCOUNT = "account"
  22.     CHAR = "char"
  23.     CORP = "corp"
  24.     EVE = "eve"
  25.     MAP = "map"
  26.     SERVER = "server"
  27.     API = "API"
  28.     CATEGORIES = (
  29.         (ACCOUNT, "Account"),
  30.         (CHAR, "Char"),
  31.         (CORP, "Corp"),
  32.         (EVE, "Eve"),
  33.         (MAP, "Map"),
  34.         (SERVER, "Server"),
  35.         (API, "API"),
  36.     )
  37.  
  38.     UPDATES = ['WalletJournal', 'MarketOrders']
  39.  
  40.     api = models.ForeignKey('apis.Api', null=True)
  41.     kwargs = PickledObjectField(null=True, blank=True)
  42.     category = models.CharField(max_length=15, choices=CATEGORIES, blank=True)
  43.     key = models.CharField(max_length=254)
  44.     #cache_key = models.CharField(max_length=254)
  45.  
  46.     class Meta:
  47.         unique_together = ['api', 'kwargs', 'category', 'key']
  48.  
  49.     def __unicode__(self):
  50.         return "%s/%s" % (self.category, self.key)
  51.  
  52.     def cache_key(self):
  53.         key = connection.generate_cache_key(
  54.             self.key, category=self.category, api=self.api, **self.kwargs
  55.         )
  56.         return key
  57.  
  58.     def execute(self):
  59.         now = datetime.now().replace(tzinfo=utc)
  60.         # need to be created before running UPDATES
  61.         cache_key = self.cache_key()
  62.         result = {"created": now}
  63.         if self.key in self.UPDATES:
  64.             data = getattr(self, self.key.lower())()
  65.             cache_timer = utils.connection.API_TIMERS[self.key]
  66.         else:
  67.             data = self.request_call()
  68.             # default cachetimer is 48 hours
  69.             cache_timer = 60 * 60 * 48
  70.         result["data"] = data
  71.         cache.set(cache_key, result, cache_timer)
  72.  
  73.     # Get the first part of the api object.
  74.     # it will establish an authenticated connection if needed.
  75.     # the connection will already have the first part of the API uri
  76.     # example uri part: https://api.eveonline.com/Eve/KEY
  77.     # Where KEY will be added before retrieving the information
  78.     # for more information look at https://neweden-dev.com/API
  79.     def connection(self):
  80.         if self.api:
  81.             api = connection.auth_connect(self.api)
  82.         else:
  83.             api = connection.api_connect()
  84.         # return eveapi object
  85.         return getattr(api, self.category)
  86.  
  87.     # uses connection to retrieve data from specific API key
  88.     # Kwargs should contain all required and/or optional fields
  89.     # so example kwargs for a WalletJournal key with 2500 rows would be
  90.     # self.kwargs = {"characterID": some_character_id, "rowCount": 2500}
  91.     def request_call(self):
  92.         connect = self.connection()
  93.         if self.kwargs:
  94.             return getattr(connect, self.key)(**self.kwargs)
  95.         else:
  96.             return getattr(connect, self.key)()
  97.  
  98.     # update wallet journal for characters and corporation
  99.     def walletjournal(self):
  100.         if "characterID" in self.kwargs:
  101.             model = apps.get_model("characters", "CharacterJournal")
  102.             parent = apps.get_model("characters", "CharacterApi").objects.get(
  103.                 characterid=self.kwargs['characterID']
  104.             )
  105.         else:
  106.             model = apps.get_model("corporations", "CorporationJournal")
  107.  
  108.         fromid = 0
  109.         while True:
  110.             transactions = self.request_call().transactions
  111.             for trans in transactions:
  112.                 date = common.convert_timestamp(trans.date)
  113.                 if model.objects.filter(
  114.                     parent=parent,
  115.                     balance=trans.balance,
  116.                     date=date,
  117.                 ).exists():
  118.                     continue
  119.                 else:
  120.                     model.create_entry(parent, trans)
  121.  
  122.                 if int(trans.refID) < fromid or fromid == 0:
  123.                     fromid = int(trans.refID)
  124.  
  125.             if len(transactions) < 2500:
  126.                 break
  127.             else:
  128.                 kwargs = self.kwargs
  129.                 kwargs['fromID'] = fromid
  130.                 self.kwargs = Kwargs
  131.                 self.save()
  132.         return True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement