1
0
Fork 0
mirror of https://github.com/Findus23/se-simulator.git synced 2024-09-19 15:53:45 +02:00

improve server

This commit is contained in:
Lukas Winkler 2018-03-23 21:28:37 +01:00
parent d2e7daf331
commit f4cc43fa81
12 changed files with 172 additions and 54 deletions

View file

@ -9,7 +9,9 @@ from parsexml import parse_posts, parse_comments, parse_usernames
from utils import *
files = get_files()
# os.chdir("/mydir")
# TODO: name sites/id after real url
for file in glob.glob("downloads/**/*.7z"):
if "meta" in file:
continue

View file

@ -39,6 +39,7 @@ class Question(BaseModel):
user = ForeignKeyField(User)
site = ForeignKeyField(Site)
datetime = DateTimeField()
random = IntegerField()
class Answer(BaseModel):

View file

@ -3,18 +3,30 @@ backports.csv==1.0.5
beautifulsoup4==4.6.0
certifi==2018.1.18
chardet==3.0.4
click==6.7
clint==0.5.1
docopt==0.6.2
Flask==0.13.dev0
gunicorn==19.7.1
html2text==2018.1.9
idna==2.6
internetarchive==1.7.7
itsdangerous==0.24
Jinja2==2.10
jsonlines==1.2.0
jsonpatch==1.21
jsonpointer==2.0
libsass==0.14.2
lxml==4.2.0
markovify==0.7.1
MarkupSafe==1.0
mysqlclient==1.3.12
peewee==3.1.3
nltk==3.2.5
peewee==3.1.5
python-slugify==1.2.4
requests==2.18.4
schema==0.6.7
six==1.11.0
Unidecode==1.0.22
urllib3==1.22
Werkzeug==0.14.1

View file

@ -1,7 +1,6 @@
from datetime import datetime
from flask import render_template, flash
from playhouse.shortcuts import model_to_dict
import sass
from flask import render_template
from playhouse.flask_utils import PaginatedQuery
from sassutils.wsgi import SassMiddleware
import utils
@ -10,45 +9,27 @@ from models import *
app.jinja_env.globals.update(prettydate=utils.prettydate)
def query_to_response(query, limit=10, key=False, sort=False, offset=None, list=None, **kwargs):
"""
:param sort: boolean
:param offset: int
:type key: str
:type limit: int|boolean
:param **kwargs
:type query: peewee.ModelSelect
"""
if limit:
query = query.limit(limit)
print(query.sql())
data = {} if key is not False else []
order = int(offset) if offset else 0
for i in query:
element = model_to_dict(i, **kwargs)
if list:
element = element[list]
if sort:
element["order"] = order
order += 1
if key is not False:
if "." in key:
key1, key2 = key.split(".")
data[getattr(getattr(i, key1), key2)] = element
else:
data[getattr(i, key)] = element
else:
data.append(element)
return data
@app.route('/')
def index():
query = Question.select().limit(10)
# return query_to_response(Question.select().limit(10), limit=False, max_depth=1)
# return query_to_response(query, max_depth=1)
return render_template('list.html', questions=query_to_response(query, max_depth=1))
select = """
*,
((upvotes + 1.9208) / (upvotes + downvotes) -
1.96 * SQRT((upvotes * downvotes) / (upvotes + downvotes) + 0.9604) /
(upvotes + downvotes)) / (1 + 3.8416 / (upvotes + downvotes))
AS ci_lower_bound
"""
query = Question.select(SQL(select)).order_by(SQL("ci_lower_bound DESC, random"))
print(query.sql())
paginated_query = PaginatedQuery(query, paginate_by=10, check_bounds=True)
pagearray = utils.create_pagination(paginated_query.get_page_count(), paginated_query.get_page())
return render_template(
'list.html',
pagearray=pagearray,
num_pages=paginated_query.get_page_count(),
page=paginated_query.get_page(),
questions=paginated_query.get_object_list()
)
@app.route('/q/<string:slug>')
@ -66,3 +47,5 @@ if __name__ == '__main__':
'web': ('static/sass', 'static/css', '/static/css')
})
app.run()
else:
sass.compile(dirname=('web/static/sass', 'web/static/css'), output_style='compressed')

View file

@ -1,6 +1,10 @@
<!doctype html>
<title>Flaskr</title>
<link href="{{ url_for('static', filename='css/style.scss.css') }}" rel="stylesheet" type="text/css">
{% if config['DEBUG'] %}
<link href="{{ url_for('static', filename='css/style.scss.css') }}" rel="stylesheet" type="text/css">
{% else %}
<link href="{{ url_for('static', filename='css/style.css') }}" rel="stylesheet" type="text/css">
{% endif %}
<div class="container">
<h1>Flaskr</h1>
{% for message in get_flashed_messages() %}

View file

@ -1,5 +1,7 @@
{% extends "base.html" %}
{% from 'macros.html' import pagination %}
{% block body %}
{{ pagination(pagearray, num_pages, page, True) }}
{% for question in questions %}
<div class="question"
style="border-right-color:{{ question.site.tag_foreground_color }};background-color:{{ question.site.tag_background_color }}">
@ -18,12 +20,9 @@
</div>
{{ question.text }}
<div class="date">
asked {{ prettydate(question.datetime) }} by {{ question.user.username }}
asked {{ prettydate(question.datetime) }} by {{ question.user.username }}
</div>
</div>
</div>
{% else %}
<em>Unbelievable. No entries here so far</em>
{% endfor %}
<pre><code>{{ questions[0]|pprint }}</code></pre>
{% endblock %}

44
templates/macros.html Normal file
View file

@ -0,0 +1,44 @@
{% macro printpage(i,active) %}
{# <a href="/projekte/{{ i }}/" class="{{ "active" if i == active else "other" }}">{{ i }}</a>#}
<a href="{{url_for("index",page=i)}}" class="{{ "active" if i == active|string() else "other" }}">{{ i }}</a>
{% endmacro %}
{% macro pagination(pagearray, num_pages, page,top) %}
<!-- License of svg icons - http://fontawesome.io/license (Font: SIL OFL 1.1, CSS: MIT License) -->
<div class="pagination {{ "top" if top else "bottom" }}">
{% if page > 1 %}
<a rel="prev" href="{{ url_for("index",page=page-1) }}">
<svg viewBox="0 0 1792 1792" xmlns="http://www.w3.org/2000/svg">
<path d="M1203 544q0 13-10 23l-393 393 393 393q10 10 10 23t-10 23l-50 50q-10 10-23 10t-23-10l-466-466q-10-10-10-23t10-23l466-466q10-10 23-10t23 10l50 50q10 10 10 23z"></path>
</svg>
</a>
{% else %}
<a rel="prev" class="disabled">
<svg viewBox="0 0 1792 1792" xmlns="http://www.w3.org/2000/svg">
<path d="M1203 544q0 13-10 23l-393 393 393 393q10 10 10 23t-10 23l-50 50q-10 10-23 10t-23-10l-466-466q-10-10-10-23t10-23l466-466q10-10 23-10t23 10l50 50q10 10 10 23z"></path>
</svg>
</a>
{% endif %}
{% for i in pagearray %}
{% if i == "d" %}
<a class="disabled other">&hellip;</a>
{% else %}
{{ printpage(i, page) }}
{% endif %}
{% endfor %}
{% if page < num_pages %}
<a rel="next" href="{{ url_for("index",page=page+1) }}">
<svg viewBox="0 0 1792 1792" xmlns="http://www.w3.org/2000/svg">
<path d="M1171 960q0 13-10 23l-466 466q-10 10-23 10t-23-10l-50-50q-10-10-10-23t10-23l393-393-393-393q-10-10-10-23t10-23l50-50q10-10 23-10t23 10l466 466q10 10 10 23z"></path>
</svg>
</a>
{% else %}
<a rel="next" class="disabled">
<svg viewBox="0 0 1792 1792" xmlns="http://www.w3.org/2000/svg">
<path d="M1171 960q0 13-10 23l-466 466q-10 10-23 10t-23-10l-50-50q-10-10-10-23t10-23l393-393-393-393q-10-10-10-23t10-23l50-50q10-10 23-10t23 10l466 466q10 10 10 23z"></path>
</svg>
</a>
{% endif %}
</div>
{%- endmacro %}

13
todb.py
View file

@ -2,6 +2,7 @@ from datetime import datetime
from slugify import slugify
import utils
from models import *
from text_generator import get_chain, generate_text
@ -37,12 +38,12 @@ def add_question(site, count=100):
user = users[i]
print(user.username)
time = datetime.now()
Question.create(text=text, title_id=title, user_id=user, site_id=site, datetime=time)
Question.create(text=text, title_id=title, user_id=user, site_id=site, datetime=time, random=utils.rand())
if __name__ == "__main__":
query = Site.select().where(Site.last_download.is_null(False)).limit(1)
s = query.get()
add_username(s)
add_title(s)
add_question(s)
query = Site.select().where(Site.last_download.is_null(False))
for s in query:
add_username(s)
add_title(s)
add_question(s)

View file

@ -62,7 +62,6 @@ def get_random_string(length):
def prettydate(d):
diff = datetime.now() - d
s = diff.seconds
print(diff)
if diff.days > 7 or diff.days < 0:
return d.strftime('%d %b %y')
elif diff.days == 1:
@ -81,3 +80,22 @@ def prettydate(d):
return '1 hour ago'
else:
return '{} hours ago'.format(int(s / 3600))
def create_pagination(num_pages, page, padding=2):
pages = ["1"]
i = 2
while i <= num_pages:
if i < (page - padding - 1):
pages.append("d")
i = page - padding
elif (i > (page + padding)) and (num_pages > (page + padding + 2)):
pages.append("d")
i = num_pages
pages.append(str(i))
i += 1
return pages
def rand():
return random.randint(-2**31, 2**31-1)

View file

@ -0,0 +1,52 @@
.pagination {
&.top {}
&.bottom {
text-align: right;
@media (max-width: $mobile-width) {
text-align: center;
}
}
a {
.mobile {
display: none;
}
font-size: 18px;
display: inline-block;
padding: 8px 20px;
margin-left: -5px;
svg {
height: 18px;
vertical-align: middle;
}
@media (min-width: 500px) {
&:hover:not(.disabled), &.active {
color: $primary-dark;
border-bottom: 3px solid;
}
}
&.disabled {
color: $lightTextGray;
svg {
fill: $lightTextGray;
}
}
@media (max-width: $mobile-width) {
&.active::before {
content: "Seite ";
}
&.other {
display: none;
}
}
}
}

View file

@ -3,6 +3,8 @@ $link-hover-color: #3af;
@import "../../milligram/src/milligram";
@import "pagination";
body {
font-family: Arial, "Helvetica Neue", Helvetica, sans-serif;
color: #111;