From 9f5fe51c35720465d256913f1d6c8badecc0f436 Mon Sep 17 00:00:00 2001 From: Ajurna Date: Wed, 25 Apr 2018 15:57:11 +0100 Subject: [PATCH] added ability to search for corps, chars and alliances. --- .idea/dictionaries/Peter_Dwyer.xml | 8 ++++ eve_auth/data_classes.py | 67 ++++++++++++++++++++++++++++++ eve_auth/forms.py | 24 +++++++++++ eve_auth/views.py | 40 ++++++++++++++++++ eve_verify/urls.py | 3 +- requirements.txt | 4 +- templates/base.html | 10 +++-- templates/search.html | 49 ++++++++++++++++++++++ 8 files changed, 200 insertions(+), 5 deletions(-) create mode 100644 .idea/dictionaries/Peter_Dwyer.xml create mode 100644 eve_auth/data_classes.py create mode 100644 eve_auth/forms.py create mode 100644 templates/search.html diff --git a/.idea/dictionaries/Peter_Dwyer.xml b/.idea/dictionaries/Peter_Dwyer.xml new file mode 100644 index 0000000..a3de4e4 --- /dev/null +++ b/.idea/dictionaries/Peter_Dwyer.xml @@ -0,0 +1,8 @@ + + + + datasource + eveonline + + + \ No newline at end of file diff --git a/eve_auth/data_classes.py b/eve_auth/data_classes.py new file mode 100644 index 0000000..a8567fc --- /dev/null +++ b/eve_auth/data_classes.py @@ -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) diff --git a/eve_auth/forms.py b/eve_auth/forms.py new file mode 100644 index 0000000..12ee343 --- /dev/null +++ b/eve_auth/forms.py @@ -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' + } + )) \ No newline at end of file diff --git a/eve_auth/views.py b/eve_auth/views.py index 85f622e..ff874bb 100644 --- a/eve_auth/views.py +++ b/eve_auth/views.py @@ -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 + } + ) diff --git a/eve_verify/urls.py b/eve_verify/urls.py index f76486d..e62c6cf 100644 --- a/eve_verify/urls.py +++ b/eve_verify/urls.py @@ -21,5 +21,6 @@ import eve_auth.views urlpatterns = [ path('admin/', admin.site.urls), url(r'^accounts/', include('allauth.urls')), - path('', eve_auth.views.hello) + path('', eve_auth.views.hello), + path('search/', eve_auth.views.search) ] diff --git a/requirements.txt b/requirements.txt index 62741de..de8e9e4 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,6 @@ Django requests django-allauth -django-bootstrap4 \ No newline at end of file +django-bootstrap4 +python-dateutil +bleach \ No newline at end of file diff --git a/templates/base.html b/templates/base.html index 256361a..0e890bd 100644 --- a/templates/base.html +++ b/templates/base.html @@ -12,23 +12,27 @@ {% bootstrap_css %} - + + + {% include 'nav.html' %} -
+ {% block main_body %} +

Bootstrap starter template

Use this document as a way to quickly start any new project.
All you get is this text and a mostly barebones HTML document.

-
+ {% endblock main_body %} + diff --git a/templates/search.html b/templates/search.html new file mode 100644 index 0000000..6b31b47 --- /dev/null +++ b/templates/search.html @@ -0,0 +1,49 @@ +{% extends 'base.html' %} +{% load bootstrap4 %} + +{% block main_body %} +
+
+ {% csrf_token %} +
+ {# #} + {{ form.search_text }} + {{ form.category }} +
+ +
+
+
+
+
+ + {% for result in results %} +
+
+
+
+
+ {% if form.category.value == 'alliance' %} + + {% elif form.category.value == 'corporation' %} + + {% else %} + + {% endif %} +
+
+

{{ result.name }}

+ {% if result.ticker %}
<{{ result.ticker }}>
{% endif %} + {% if result.security_status %}
Sec Status: {{ result.security_status }}
{% endif %} +

{{ result.description|safe }}

+

Add Rights

+
+
+
+
+
+ {% endfor %} + +
+ +{% endblock main_body %} \ No newline at end of file