Skip to content
Snippets Groups Projects
Commit bf1d799a authored by jan.bednarik's avatar jan.bednarik
Browse files

Early application implementation.

parent 0fb69a4f
No related branches found
No related tags found
No related merge requests found
#!/usr/bin/env python
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "olapp.settings")
try:
from django.core.management import execute_from_command_line
except ImportError:
# The above import may fail for some other reason. Ensure that the
# issue is really that Django is missing to avoid masking other
# exceptions on Python 2.
try:
import django
except ImportError:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
)
raise
execute_from_command_line(sys.argv)
from django.apps import AppConfig
class CoreConfig(AppConfig):
name = 'core'
import arrow
import requests
import json
class NotFoundError(Exception):
pass
def post_query(api_url, query):
response = requests.post(api_url, json={'query': query})
content = response.json()
if 'errors' in content:
raise Exception(content['errors'])
return content['data']
def parse_dates(reports):
for r in reports:
r['date'] = arrow.get(r['date'])
r['published'] = arrow.get(r['published'])
return reports
def parse_extra(reports):
for r in reports:
r['extra'] = json.loads(r['extra'])
r['author']['extra'] = json.loads(r['author']['extra'])
return reports
def get_all_reports(api_url):
query = """
query {
reports {
id
date
published
title
body
receivedBenefit
providedBenefit
extra
author {
id
name
extra
}
}
}
"""
data = post_query(api_url, query)
reports = data['reports']
reports = parse_dates(reports)
reports = parse_extra(reports)
return reports
def get_report(api_url, id):
query = """
query {{
node (id:"{id}") {{
... on Report {{
id
date
published
title
body
receivedBenefit
providedBenefit
extra
author {{
id
name
extra
}}
}}
}}
}}
""".format(id=id)
data = post_query(api_url, query)
report = data['node']
if report is None:
raise NotFoundError()
reports = parse_dates([report])
reports = parse_extra(reports)
return reports[0]
def get_author(api_url, id):
query = """
query {{
node (id:"{id}") {{
... on Author {{
id
name
extra
}}
}}
}}
""".format(id=id)
data = post_query(api_url, query)
author = data['node']
if author is None:
raise NotFoundError()
author['extra'] = json.loads(author['extra'])
return author
{% extends "core/skeleton.html" %}
{% block content %}
<div class="m-4">
<h5>Profil autora</h5>
<div class="border rounded bg-light py-2">
<div class="d-flex">
<div class="px-3 text-right" style="min-width: 14em"><small class="text-muted">jméno:</small></div>
<div class="">{{ author.name }}</div>
</div>
{% if author.extra.login %}
<div class="d-flex pt-1">
<div class="px-3 text-right" style="min-width: 14em"><small class="text-muted">profil na Pirátském fóru:</small></div>
<div class=""><a href="{{ author.extra.link }}">{{ author.extra.login }}</a></div>
</div>
{% endif %}
</div>
</div>
{% endblock %}
{% extends "core/skeleton.html" %}
{% block content %}
{% for report in reports %}
{% include "core/report_snippet.html" %}
{% endfor %}
{% endblock %}
{% extends "core/skeleton.html" %}
{% block content %}
{% include "core/report_snippet.html" %}
{% endblock %}
<div class="row m-4">
<div class="col-9 p-0 border rounded-left">
<div class="d-flex justify-content-start py-1 border border-left-0 border-right-0 border-top-0 bg-light">
<div class="pl-3 pr-2">{{ report.date|date:"j. n. Y" }}</div>
<div class="px-2"><b><a href="{% url 'report' id=report.id %}">{{ report.title }}</a></b></div>
</div>
<div class="d-flex">
<div class="px-3 pt-2">
{{ report.body|linebreaks }}
</div>
</div>
{% if report.receivedBenefit %}
<div class="d-flex border border-left-0 border-right-0 border-bottom-0 bg-light">
<div class="px-3 py-1 text-right" style="min-width: 11em"><small class="text-muted">přijaté výhody:</small></div>
<div class="py-1">{{ report.receivedBenefit }}</div>
</div>
{% endif %}
{% if report.providedBenefit %}
<div class="d-flex border border-left-0 border-right-0 border-bottom-0 bg-light">
<div class="px-3 py-1 text-right" style="min-width: 11em"><small class="text-muted">poskytnuté výhody:</small></div>
<div class="py-1">{{ report.providedBenefit }}</div>
</div>
{% endif %}
</div>
<div class="col-3 pt-2 border border-left-0 rounded-right d-flex flex-column bg-light">
<div><a href="{% url 'author' id=report.author.id %}">{{ report.author.name }}</a></div>
{% if report.extra.link %}
<div class="mt-auto py-1 small text-muted">
<div class="border rounded p-1">
report zkopírován z <a href="{{ report.extra.link }}">Pirátského fóra</a><br>
</div>
</div>
<div class="py-1">
{% else %}
<div class="mt-auto py-1">
{% endif %}
<span class="small text-muted">
publikováno: {{ report.published|date:"j. n. Y H:i" }}
<a class="ml-2" href="{% url 'report' id=report.id %}">permalink</a>
</span>
</div>
</div>
</div>
<!doctype html>
<html lang="en">
<head>
<title>Registr lobbistických schůzek (beta)</title>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" integrity="sha384-PsH8R72JQ3SOdhVi3uxftmaW6Vc51MKb0q5P2rRUpPvrszuE4W1povHYgTpBfshb" crossorigin="anonymous">
</head>
<body style="font-size: 90%">
<nav class="navbar navbar-expand-sm navbar-dark bg-dark">
<div class="container-fluid px-4">
<a class="navbar-brand" href="/">Registr lobbistických schůzek <sup class="text-muted">beta</sup></a>
<!--
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav ml-auto">
<li class="nav-item">
<a class="nav-link" href="#">registrace</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">přihlášení</a>
</li>
</ul>
</div>
-->
</div>
</nav>
<div class="container-fluid">
{% block content %}{% endblock %}
</div>
<div class="container-fluid pt-3 pb-2 border border-left-0 border-right-0 border-bottom-0 bg-light">
<p class="text-muted text-center"><small>
Toto je veřejný beta test systému Open Lobby pro Českou pirátskou stranu. Cokoliv se může změnit.<br>
Podněty a připomínky piště <a href="https://forum.pirati.cz/administrativa-f498/plnohodnotna-evidence-lobbistickych-kontaktu-t37613.html">na pirátském fóru</a>. Kontakt: <a href="mailto:jan.bednarik@gmail.com">jan.bednarik@gmail.com</a>
</small></p>
</div>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.3/umd/popper.min.js" integrity="sha384-vFJXuSJphROIrBnz7yo7oB41mKfc8JzQZiCq4NCceLEaO4IHwicKwpJf9c9IpFgh" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js" integrity="sha384-alpBpkh1PFOepccYVYDB4do5UnbKysX5WZXm3XxPqe5iKTfUKjNkCk9SaVuEZflJ" crossorigin="anonymous"></script>
</body>
</html>
from django.test import TestCase
# Create your tests here.
from django.conf import settings
from django.http import Http404
from django.views.generic.base import TemplateView
from .queries import NotFoundError, get_all_reports, get_report, get_author
class IndexView(TemplateView):
template_name = 'core/index.html'
def get_context_data(self, **kwargs):
context = super(IndexView, self).get_context_data(**kwargs)
context['reports'] = get_all_reports(settings.OPENLOBBY_API_URL)
return context
class ReportView(TemplateView):
template_name = 'core/report.html'
def get_context_data(self, **kwargs):
context = super(ReportView, self).get_context_data(**kwargs)
try:
context['report'] = get_report(settings.OPENLOBBY_API_URL, kwargs['id'])
except NotFoundError:
raise Http404()
return context
class AuthorView(TemplateView):
template_name = 'core/author.html'
def get_context_data(self, **kwargs):
context = super(AuthorView, self).get_context_data(**kwargs)
try:
context['author'] = get_author(settings.OPENLOBBY_API_URL, kwargs['id'])
except NotFoundError:
raise Http404()
return context
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'e$smv7zgp9aldrw^%mvy0)(2=*4t-gpp0$5%(94r_pu4zd*ti%'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
# 'django.contrib.admin',
# 'django.contrib.auth',
# 'django.contrib.contenttypes',
# 'django.contrib.sessions',
'django.contrib.staticfiles',
'olapp.core',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
# 'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
# 'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'olapp.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
# 'django.contrib.auth.context_processors.auth',
# 'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'olapp.wsgi.application'
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': ':memory:',
}
}
# Password validation
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
STATIC_URL = '/static/'
openlobby_server_dsn = os.environ.get('OPENLOBBY_SERVER_DSN', 'http://localhost:8010')
OPENLOBBY_API_URL = '{}/graphql'.format(openlobby_server_dsn)
from django.conf.urls import url
from olapp.core.views import IndexView, ReportView, AuthorView
urlpatterns = [
url(r'^$', IndexView.as_view(), name='index'),
url(r'^report/(?P<id>[0-9A-Za-z-_]+)/$', ReportView.as_view(), name='report'),
url(r'^author/(?P<id>[0-9A-Za-z-_]+)/$', AuthorView.as_view(), name='author'),
]
"""
WSGI config for olapp project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/1.11/howto/deployment/wsgi/
"""
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "olapp.settings")
application = get_wsgi_application()
Django>=1.11.7,<1.12
requests>=2.18.4,<2.19
arrow==0.10.0
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment