Refactor directory structure

This commit is contained in:
Ilya Zverev 2016-06-07 12:29:57 +03:00
parent c7f90781cd
commit fbeeb39f3d
13 changed files with 51 additions and 73 deletions

0
mmwatch/__init__.py Normal file
View file

8
mmwatch/config.py Normal file
View file

@ -0,0 +1,8 @@
DEBUG = True
import os
BASE_DIR = os.path.abspath(os.path.dirname(__file__))
DATABASE_URI = 'sqlite:///' + os.path.join(BASE_DIR, 'server', 'mapsme-changes.db')
PAGE_SIZE = 100
TOP = 10

View file

@ -1,9 +1,8 @@
import os.path, sys
import config, json
from peewee import *
from playhouse.db_url import connect
#path = os.path.dirname(sys.argv[0]) if len(sys.argv) < 2 else sys.argv[1]
path = os.path.dirname(sys.argv[0])
database = SqliteDatabase(os.path.join(path, 'mapsme-changes.db'))
database = connect(config.DATABASE_URI)
class Change(Model):
"""A model for the change. Just a single table."""
@ -19,6 +18,37 @@ class Change(Model):
processed = IntegerField(null=True) # number of hours between modifying and an external fix of the object
changes = TextField()
def explain_action(self):
explains = { 'a': 'done smth strange', 'c': 'created', 'd': 'deleted', 'm': 'modified', 'n': 'left a note' }
return explains[self.action]
def changed_coord(self):
if self.action == 'a':
return None
c = json.loads(self.changes)[0]
if self.action == 'm' and c is not None:
return c[1]
return c
def changed_tags(self):
if self.action == 'a':
return {}
tags = json.loads(self.changes)[1]
for t in tags:
if self.action == 'c':
tags[t] = [None, tags[t]]
elif self.action == 'd':
tags[t] = [tags[t], None]
if tags[t][0] is None:
tags[t].append('create')
elif tags[t][1] is None:
tags[t].append('delete')
elif tags[t][0] != tags[t][1]:
tags[t].append('modify')
else:
tags[t].append('nothing')
return tags
class Meta:
database = database

3
mmwatch/run.py Executable file
View file

@ -0,0 +1,3 @@
#!/usr/bin/env python
from www import mmwatch
mmwatch.app.run(debug=True)

67
www/mmwatch.py → mmwatch/www/mmwatch.py Executable file → Normal file
View file

@ -1,75 +1,15 @@
#!/usr/bin/env python
import os, json, peewee
from flask import Flask, send_file, request, render_template, url_for, abort, jsonify, Response
from flask.ext.compress import Compress
from flask_compress import Compress
from datetime import datetime, timedelta
from StringIO import StringIO
import config
from db import database, Change, User
app = Flask(__name__)
app.debug = config.DEBUG
Compress(app)
database = peewee.SqliteDatabase(os.path.join(config.DATABASE_PATH, 'mapsme-changes.db'))
class Change(peewee.Model):
"""A model for the change. Just a single table."""
changeset = peewee.IntegerField()
user = peewee.CharField(max_length=250, index=True)
version = peewee.CharField(max_length=250)
timestamp = peewee.DateTimeField(index=True)
action = peewee.FixedCharField(max_length=1, index=True) # c, d, m, a, a
obj_type = peewee.FixedCharField(max_length=1, null=True)
obj_id = peewee.IntegerField(null=True)
main_tag = peewee.CharField(max_length=100, null=True)
address = peewee.BooleanField(default=False)
processed = peewee.IntegerField(null=True)
changes = peewee.TextField()
def explain_action(self):
explains = { 'a': 'done smth strange', 'c': 'created', 'd': 'deleted', 'm': 'modified', 'n': 'left a note' }
return explains[self.action]
def changed_coord(self):
if self.action == 'a':
return None
c = json.loads(self.changes)[0]
if self.action == 'm' and c is not None:
return c[1]
return c
def changed_tags(self):
if self.action == 'a':
return {}
tags = json.loads(self.changes)[1]
for t in tags:
if self.action == 'c':
tags[t] = [None, tags[t]]
elif self.action == 'd':
tags[t] = [tags[t], None]
if tags[t][0] is None:
tags[t].append('create')
elif tags[t][1] is None:
tags[t].append('delete')
elif tags[t][0] != tags[t][1]:
tags[t].append('modify')
else:
tags[t].append('nothing')
return tags
class Meta:
database = database
class User(peewee.Model):
"""A model for user stats."""
user = peewee.CharField(max_length=250, unique=True)
edits = peewee.IntegerField()
rank = peewee.IntegerField(default=0)
joined = peewee.DateField()
class Meta:
database = database
@app.before_request
def before_request():
database.connect()
@ -217,6 +157,3 @@ def the_one_and_only_page():
stats['users'] = q['users'].count(clear_limit=True)
return render_template('index.html', stats=stats, changes=q['changes'], users=q['users'], tags=q['tags'], versions=q['versions'], dates=q['dates'], params=params, purl=purl)
if __name__ == '__main__':
app.run(threaded=True)

4
requirements.txt Normal file
View file

@ -0,0 +1,4 @@
peewee>=2.8.0
lxml
flask>=0.11
flask-Compress

View file

@ -1,4 +0,0 @@
DATABASE_PATH = '../server'
DEBUG = True
PAGE_SIZE = 30
TOP = 10