Created
August 9, 2016 16:31
-
-
Save MatonAnthony/17a6eafcc38838303c53c032d8d56e54 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.db import models | |
from django.contrib.auth.models import User | |
from django.contrib.auth.models import Group | |
from django.apps import apps | |
import evelink.eve | |
import evelink.api | |
import json | |
class EveSkillEntity(models.Model): | |
id = models.IntegerField(primary_key=True) | |
name = models.CharField(max_length=128) | |
class EveSkill(models.Model): | |
skill = models.ForeignKey(EveSkillEntity) | |
level = models.IntegerField() | |
class EveLocation(models.Model): | |
id = models.IntegerField(primary_key=True) | |
name = models.CharField(max_length=128) | |
system = models.CharField(max_length=128, blank=True) | |
class EveItem(models.Model): | |
id = models.IntegerField(primary_key=True) | |
name = models.CharField(max_length=128) | |
class EveAlliance(models.Model): | |
id = models.IntegerField(primary_key=True) | |
name = models.CharField(max_length=128) | |
class EveCorporation(models.Model): | |
id = models.IntegerField(primary_key=True) | |
name = models.CharField(max_length=128) | |
alliance = models.ForeignKey(EveAlliance) | |
class EveApiKey(models.Model): | |
key = models.IntegerField() | |
vcode = models.CharField(max_length=128) | |
mask = models.IntegerField(editable=False) | |
validity = models.BooleanField() | |
owner = models.ForeignKey('Player') | |
def check_validity(self): | |
api = evelink.api.API(api_key=(self.key, self.vcode)) | |
account = evelink.account.Account(api) | |
key_info = account.key_info() | |
if(key_info.result['type'] == 'account' and key_info | |
.result['expire_ts'] is None and key_info.result['access_mask'] | |
== apps.get_app_config('oauth').account_access_mask): | |
return True | |
else: | |
return False | |
def update_validity(self): | |
self.validity = self.check_validity() | |
self.save() | |
class Character(models.Model): | |
character_id = models.IntegerField() | |
name = models.CharField(max_length=64) | |
api = models.OneToOneField(EveApiKey) | |
assets = models.TextField() | |
skills = models.TextField() | |
location = models.TextField() | |
corporation = models.ForeignKey(EveCorporation) | |
def update(self): | |
api = evelink.api.API(api_key=(self.api.key, self.api.vcode)) | |
character = evelink.char.Char(char_id=self.character_id, api=api) | |
self.location = json.dumps(character.locations()) | |
self.assets = json.dumps(character.assets()) | |
self.skills = json.dumps(character.skills()) | |
sheet = character.character_sheet() | |
try: | |
self.corporation = EveCorporation.objects.get(id=sheet | |
.result['corp']['id']) | |
except EveCorporation.DoesNotExist: | |
id = sheet.result['corp']['id'] | |
name = sheet.result['corp']['name'] | |
try: | |
alliance = EveAlliance.objects.get(id=sheet. | |
result['alliance']['id']) | |
except EveAlliance.DoesNotExist: | |
a_id = sheet.result['alliance']['id'] | |
a_name = sheet.result['name']['id'] | |
alliance = EveAlliance.create(id=a_id, name=a_name) | |
self.corporation = EveCorporation.create(id=id, name=name, | |
alliance=alliance) | |
self.save() | |
class Player(models.Model): | |
user = models.OneToOneField(User, on_delete=models.CASCADE) | |
class SecuredGroup(models.Model): | |
# Conditions variables | |
IN = 'IN' | |
OUT = 'NOT IN' | |
CONDITIONS = ( | |
(IN, 'IN'), | |
(OUT, 'NOT IN') | |
) | |
group = models.OneToOneField(Group, on_delete=models.CASCADE) | |
# Security condition and on what they are applied | |
condition_skills = models.CharField(choices=CONDITIONS, max_length=128, | |
blank=True) | |
skills = models.ManyToManyField(EveSkill) | |
condition_items = models.CharField(choices=CONDITIONS, max_length=128, | |
blank=True) | |
items = models.ManyToManyField(EveItem) | |
condition_location = models.CharField(choices=CONDITIONS, max_length=128, | |
blank=True) | |
locations = models.ManyToManyField(EveLocation) | |
condition_corporation = models.CharField(choices=CONDITIONS, | |
max_length=128, blank=True) | |
corporations = models.ManyToManyField(EveCorporation) | |
condition_alliance = models.CharField(choices=CONDITIONS, max_length=128, | |
blank=True) | |
alliances = models.ManyToManyField(EveAlliance) | |
# This custom condition is not accessible from the UI because | |
# it's made for very specific condition and the code inside of this | |
# field will be executed without any prior checking | |
condition_custom = models.TextField(editable=False, blank=True) | |
# Things non-related to core that this group unlocks | |
teamspeak = models.CharField(max_length=256, blank=True) | |
discord = models.BigIntegerField(null=True) | |
forum = models.IntegerField(null=True) | |
def custom_conditions(self): | |
''' Execute the custom code put in condition_custom ''' | |
eval(self.condition_custom) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment