added ability to search for corps, chars and alliances.

This commit is contained in:
2018-04-25 15:57:11 +01:00
parent 3839235937
commit 9f5fe51c35
8 changed files with 200 additions and 5 deletions

8
.idea/dictionaries/Peter_Dwyer.xml generated Normal file
View File

@@ -0,0 +1,8 @@
<component name="ProjectDictionaryState">
<dictionary name="Peter.Dwyer">
<words>
<w>datasource</w>
<w>eveonline</w>
</words>
</dictionary>
</component>

67
eve_auth/data_classes.py Normal file
View 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
View 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'
}
))

View File

@@ -10,6 +10,8 @@ from allauth.socialaccount.models import SocialAccount, SocialToken, SocialApp
from requests.auth import HTTPBasicAuth from requests.auth import HTTPBasicAuth
from eve_auth.models import Corporation from eve_auth.models import Corporation
from eve_auth.forms import SearchForm
from eve_auth import data_classes
def hello(request): def hello(request):
@@ -38,3 +40,41 @@ def renew_token(request, account: Union[SocialAccount, User]):
) )
token.token = req.json()['access_token'] token.token = req.json()['access_token']
token.save() 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
}
)

View File

@@ -21,5 +21,6 @@ import eve_auth.views
urlpatterns = [ urlpatterns = [
path('admin/', admin.site.urls), path('admin/', admin.site.urls),
url(r'^accounts/', include('allauth.urls')), url(r'^accounts/', include('allauth.urls')),
path('', eve_auth.views.hello) path('', eve_auth.views.hello),
path('search/', eve_auth.views.search)
] ]

View File

@@ -1,4 +1,6 @@
Django Django
requests requests
django-allauth django-allauth
django-bootstrap4 django-bootstrap4
python-dateutil
bleach

View File

@@ -12,23 +12,27 @@
<!-- Bootstrap core CSS --> <!-- Bootstrap core CSS -->
{% bootstrap_css %} {% bootstrap_css %}
<!-- Font Awesome -->
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.0.10/css/all.css" integrity="sha384-+d0P83n9kaQMCwj8F4RJB66tzIwOKmrdb46+porD/OvrJ+37WqIM7UoBtwHO6Nlg" crossorigin="anonymous">
<!-- Custom styles for this template --> <!-- Custom styles for this template -->
<link href="/static/css/custom.css" rel="stylesheet"> <link href="/static/css/custom.css" rel="stylesheet">
</head> </head>
<body> <body>
{% include 'nav.html' %} {% include 'nav.html' %}
<main role="main" class="container">
{% block main_body %}
<main role="main" class="container">
<div class="starter-template"> <div class="starter-template">
<h1>Bootstrap starter template</h1> <h1>Bootstrap starter template</h1>
<p class="lead">Use this document as a way to quickly start any new project.<br> All you get is this text and a mostly barebones HTML document.</p> <p class="lead">Use this document as a way to quickly start any new project.<br> All you get is this text and a mostly barebones HTML document.</p>
</div> </div>
</main><!-- /.container --> </main><!-- /.container -->
{% endblock main_body %}
<!-- Bootstrap core JavaScript <!-- Bootstrap core JavaScript
================================================== --> ================================================== -->

49
templates/search.html Normal file
View File

@@ -0,0 +1,49 @@
{% extends 'base.html' %}
{% load bootstrap4 %}
{% block main_body %}
<div class="container">
<form method="post">
{% csrf_token %}
<div class="input-group mb-3">
{# <input type="text" class="form-control" placeholder="Search Text">#}
{{ form.search_text }}
{{ form.category }}
<div class="input-group-append">
<button class="btn btn-outline-secondary" type="submit">Search</button>
</div>
</div>
</form>
</div>
<div class="container">
{% for result in results %}
<div class="row">
<div class="col"></div>
<div class="col-md-6">
<div class="d-flex flex-row border rounded">
<div class="p-0 w-25">
{% if form.category.value == 'alliance' %}
<img src="//image.eveonline.com/Alliance/{{ result.id }}_128.png" class="img-thumbnail border-0" />
{% elif form.category.value == 'corporation' %}
<img src="//image.eveonline.com/Corporation/{{ result.id }}_128.png" class="img-thumbnail border-0" />
{% else %}
<img src="//image.eveonline.com/Character/{{ result.id }}_128.jpg" class="img-thumbnail border-0" />
{% endif %}
</div>
<div class="pl-3 pt-2 pr-2 pb-2 w-75 border-left">
<h4 class="text-primary">{{ result.name }}</h4>
{% if result.ticker %}<h5 class="text-info">&lt;{{ result.ticker }}&gt;</h5>{% endif %}
{% if result.security_status %}<h5 class="text-info">Sec Status: {{ result.security_status }}</h5>{% endif %}
<p class="text-left m-0">{{ result.description|safe }}</p>
<p class="text-right m-0"><a href="#" class="btn btn-primary"><i class="far fa-bookmark"></i> Add Rights</a></p>
</div>
</div>
</div>
<div class="col"></div>
</div>
{% endfor %}
</div>
{% endblock main_body %}