mirror of
https://github.com/house-of-vanity/libopenanal.git
synced 2025-07-07 05:34:08 +00:00
User overview page implemented.
This commit is contained in:
117
database.py
117
database.py
@ -1,8 +1,10 @@
|
||||
import sqlite3
|
||||
from datetime import datetime
|
||||
from dateutil import parser
|
||||
import logging
|
||||
|
||||
class DataBase:
|
||||
def __init__(self, basefile):
|
||||
import sqlite3
|
||||
#import datetime as dt
|
||||
import logging
|
||||
self.log = logging.getLogger("pycrm." + __name__)
|
||||
try:
|
||||
self.conn = sqlite3.connect(
|
||||
@ -29,7 +31,28 @@ class DataBase:
|
||||
sql = "SELECT * FROM `meme` ORDER BY rowid DESC "
|
||||
return(self.execute(sql))
|
||||
|
||||
def get_user_count(self):
|
||||
sql = "SELECT count(*) FROM `user`"
|
||||
return(self.execute(sql)[0])
|
||||
|
||||
def get_word_count(self):
|
||||
sql = "SELECT count(*) FROM `word`"
|
||||
return(self.execute(sql)[0])
|
||||
|
||||
def get_relations_count(self):
|
||||
sql = "SELECT count(*) FROM `relations`"
|
||||
return(self.execute(sql)[0])
|
||||
|
||||
def get_confs_count(self):
|
||||
sql = "SELECT count(*) FROM `conf`"
|
||||
return(self.execute(sql)[0])
|
||||
|
||||
def get_users(self, order='id', sorting='ASC'):
|
||||
# sql injection prevention
|
||||
if sorting == 'ASC':
|
||||
sorting = 'ASC'
|
||||
else:
|
||||
sorting = 'DESC'
|
||||
if order == 'id':
|
||||
order = 'id'
|
||||
elif order == 'first_name':
|
||||
@ -42,23 +65,99 @@ class DataBase:
|
||||
order = 'dt'
|
||||
elif order == 'last_activity':
|
||||
order = 'last_seen'
|
||||
elif order == 'count':
|
||||
order = 'count'
|
||||
else:
|
||||
order = 'id'
|
||||
sql = """
|
||||
SELECT * FROM
|
||||
(SELECT u.id,
|
||||
SELECT * FROM (
|
||||
SELECT u.id,
|
||||
u.username,
|
||||
u.first_name,
|
||||
u.last_name,
|
||||
datetime(u.date, 'unixepoch') as dt,
|
||||
max(datetime(r.date, 'unixepoch')) as last_seen
|
||||
date(u.date, 'unixepoch') as dt,
|
||||
max(date(r.date, 'unixepoch')) as last_seen,
|
||||
count(u.id) as count
|
||||
FROM `user` u LEFT JOIN `relations` r ON
|
||||
r.user_id == u.id
|
||||
GROUP BY u.id)
|
||||
GROUP BY u.id
|
||||
)
|
||||
ORDER BY %s %s""" % (
|
||||
order, sorting)
|
||||
|
||||
return(self.execute(sql))
|
||||
|
||||
def get_confs(self):
|
||||
sql = """
|
||||
SELECT c.title,
|
||||
c.id,
|
||||
date(c.date, 'unixepoch') as dt,
|
||||
count(r.conf_id) as count,
|
||||
t1.users
|
||||
FROM `conf` c
|
||||
LEFT JOIN `relations` r
|
||||
ON c.id = r.conf_id
|
||||
LEFT JOIN `user` u
|
||||
ON u.id = r.user_id
|
||||
LEFT JOIN (
|
||||
SELECT id, title, count(user_id) as users FROM (
|
||||
SELECT c.id, c.title, r.user_id, count(r.conf_id) as words
|
||||
FROM `conf` c
|
||||
LEFT JOIN `relations` r
|
||||
ON c.id = r.conf_id
|
||||
GROUP BY c.id, r.user_id
|
||||
)
|
||||
GROUP BY title) as t1
|
||||
ON t1.id = c.id
|
||||
GROUP BY c.id
|
||||
"""
|
||||
return(self.execute(sql))
|
||||
|
||||
def get_user_info(self, user_id):
|
||||
if not user_id.isdigit():
|
||||
return False
|
||||
raw1 = self.execute("""
|
||||
SELECT u.id,
|
||||
first_name,
|
||||
last_name,
|
||||
username,
|
||||
datetime(u.date, 'unixepoch') as date,
|
||||
count(u.id) as words,
|
||||
datetime(max(r.date), 'unixepoch') as last_message
|
||||
FROM `user` u
|
||||
LEFT JOIN `relations` r ON r.user_id = u.id
|
||||
WHERE u.id = %s""" % user_id)[0]
|
||||
top = self.execute("""
|
||||
SELECT w.word, count(w.id) as count FROM `relations` r
|
||||
LEFT JOIN `user` u ON u.id = r.user_id
|
||||
LEFT JOIN `word` w ON w.id = r.word_id
|
||||
WHERE u.id = %s
|
||||
GROUP BY w.id
|
||||
ORDER BY count DESC
|
||||
LIMIT 10
|
||||
""" % user_id)
|
||||
chats = self.execute("""SELECT c.title,
|
||||
count(c.id) count,
|
||||
min(date(r.date, 'unixepoch'))
|
||||
FROM `relations` r
|
||||
LEFT JOIN `user` u ON u.id = r.user_id
|
||||
LEFT JOIN `conf` c ON c.id = r.conf_id
|
||||
WHERE u.id = %s
|
||||
GROUP BY c.id""" % user_id)
|
||||
|
||||
day_known = (parser.parse(raw1[6]) - parser.parse(raw1[4])).days
|
||||
user_info = {
|
||||
'id': raw1[0],
|
||||
'first_name': raw1[1],
|
||||
'last_name': raw1[2],
|
||||
'username': raw1[3],
|
||||
'first_date': raw1[4],
|
||||
'word_count': raw1[5],
|
||||
'last_message': raw1[6],
|
||||
'day_known': day_known,
|
||||
'top': top,
|
||||
'chats': chats,
|
||||
}
|
||||
return user_info
|
||||
|
||||
def close(self):
|
||||
self.conn.close()
|
||||
|
Reference in New Issue
Block a user