added ability to search for corps, chars and alliances.
This commit is contained in:
67
eve_auth/data_classes.py
Normal file
67
eve_auth/data_classes.py
Normal file
@@ -0,0 +1,67 @@
|
||||
from dateutil import parser
|
||||
from dataclasses import dataclass
|
||||
from datetime import datetime
|
||||
import bleach
|
||||
|
||||
BLEACH_CONFIG = {
|
||||
'tags': ['br', 'font', 'b', 'i', 'u', 'loc', 'a'],
|
||||
'attributes': ['href']
|
||||
}
|
||||
|
||||
|
||||
@dataclass
|
||||
class Alliance:
|
||||
id: int
|
||||
name: str
|
||||
creator_id: int
|
||||
creator_corporation_id: int
|
||||
ticker: str
|
||||
date_founded: datetime
|
||||
executor_corporation_id: int
|
||||
faction_id: int = None
|
||||
|
||||
def __post_init__(self):
|
||||
if self.date_founded:
|
||||
self.date_founded = parser.parse(self.date_founded)
|
||||
|
||||
|
||||
@dataclass
|
||||
class Corporation:
|
||||
id: int
|
||||
name: str
|
||||
ticker: str
|
||||
member_count: int
|
||||
ceo_id: int
|
||||
tax_rate: int
|
||||
creator_id: int
|
||||
description: str
|
||||
date_founded: datetime
|
||||
home_station_id: int
|
||||
shares: int
|
||||
alliance_id: int = None
|
||||
faction_id: int = None
|
||||
url: str = ''
|
||||
|
||||
def __post_init__(self):
|
||||
if self.date_founded:
|
||||
self.date_founded = parser.parse(self.date_founded)
|
||||
self.description = bleach.clean(self.description, **BLEACH_CONFIG)
|
||||
|
||||
|
||||
@dataclass
|
||||
class Character:
|
||||
id: int
|
||||
corporation_id: int
|
||||
name: str
|
||||
gender: str
|
||||
race_id: int
|
||||
bloodline_id: int
|
||||
description: str
|
||||
ancestry_id: int
|
||||
security_status: float
|
||||
birthday: datetime
|
||||
|
||||
def __post_init__(self):
|
||||
if self.birthday:
|
||||
self.birthday = parser.parse(self.birthday)
|
||||
self.description = bleach.clean(self.description, **BLEACH_CONFIG)
|
||||
24
eve_auth/forms.py
Normal file
24
eve_auth/forms.py
Normal file
@@ -0,0 +1,24 @@
|
||||
from django import forms
|
||||
|
||||
|
||||
class SearchForm(forms.Form):
|
||||
category_choices = (
|
||||
('alliance', 'Alliance'),
|
||||
('character', 'Character'),
|
||||
('corporation', 'Corporation')
|
||||
)
|
||||
search_text = forms.CharField(label='Search Text',
|
||||
max_length=100,
|
||||
min_length=3,
|
||||
widget=forms.TextInput(
|
||||
attrs={
|
||||
'class': 'form-control',
|
||||
'placeholder': 'Search Text'
|
||||
},
|
||||
))
|
||||
category = forms.ChoiceField(choices=category_choices,
|
||||
widget=forms.Select(
|
||||
attrs={
|
||||
'class': 'custom-select'
|
||||
}
|
||||
))
|
||||
@@ -10,6 +10,8 @@ from allauth.socialaccount.models import SocialAccount, SocialToken, SocialApp
|
||||
from requests.auth import HTTPBasicAuth
|
||||
|
||||
from eve_auth.models import Corporation
|
||||
from eve_auth.forms import SearchForm
|
||||
from eve_auth import data_classes
|
||||
|
||||
|
||||
def hello(request):
|
||||
@@ -38,3 +40,41 @@ def renew_token(request, account: Union[SocialAccount, User]):
|
||||
)
|
||||
token.token = req.json()['access_token']
|
||||
token.save()
|
||||
|
||||
|
||||
def search(request):
|
||||
results = []
|
||||
if request.POST:
|
||||
form = SearchForm(request.POST)
|
||||
if form.is_valid():
|
||||
search_results = requests.get(f'https://esi.tech.ccp.is/latest/search/?categories='
|
||||
f'{form.cleaned_data["category"]}&datasource=tranquility&language=en-us&'
|
||||
f'search={form.cleaned_data["search_text"]}&strict=false').json()
|
||||
if form.cleaned_data["category"] == 'alliance':
|
||||
if search_results:
|
||||
for alliance in search_results['alliance']:
|
||||
res = requests.get(f'https://esi.tech.ccp.is/latest/alliances'
|
||||
f'/{alliance}/?datasource=tranquility').json()
|
||||
results.append(data_classes.Alliance(id=alliance, **res))
|
||||
elif form.cleaned_data["category"] == 'corporation':
|
||||
if search_results:
|
||||
for corp in search_results['corporation']:
|
||||
res = requests.get(f'https://esi.tech.ccp.is/latest/corporations/{corp}/?'
|
||||
f'datasource=tranquility').json()
|
||||
results.append(data_classes.Corporation(id=corp, **res))
|
||||
elif form.cleaned_data["category"] == 'character':
|
||||
if search_results:
|
||||
for cha in search_results['character']:
|
||||
res = requests.get(f'https://esi.tech.ccp.is/latest/characters/{cha}/?'
|
||||
f'datasource=tranquility').json()
|
||||
results.append(data_classes.Character(id=cha, **res))
|
||||
else:
|
||||
form = SearchForm()
|
||||
return render(
|
||||
request,
|
||||
'search.html',
|
||||
{
|
||||
'form': form,
|
||||
'results': results
|
||||
}
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user