refactor model #3

Merged
southfox merged 3 commits from 1-pr into master 2023-02-17 06:31:41 +01:00
4 changed files with 15 additions and 59 deletions
Showing only changes of commit fbfd8a1505 - Show all commits

View file

@ -2,7 +2,7 @@
import click import click
from BDSM import app, db from BDSM import app, db
from BDSM.models import Settings, Toot, Other from BDSM.models import Settings, Toot
from BDSM.toot import app_login, toot_process from BDSM.toot import app_login, toot_process
from mastodon import MastodonNotFoundError from mastodon import MastodonNotFoundError
@ -19,7 +19,7 @@ def initdb(drop):
@app.cli.command() @app.cli.command()
def analysis(): def analysis():
"""Analysis current Year""" """Analysis current Year"""
from BDSM.models import Toot, Other from BDSM.models import Toot
from sqlalchemy.sql import extract from sqlalchemy.sql import extract
from sqlalchemy import func from sqlalchemy import func
from sqlalchemy import desc from sqlalchemy import desc
@ -42,8 +42,8 @@ def analysis():
) )
print("2022 年互动最多帐号排名" + print("2022 年互动最多帐号排名" +
str(db.session.query(Other.acct.label('count'),func.count('count') str(db.session.query(Toot.acct.label('count'),func.count('count')
).filter(extract('year', Other.created_at) == 2022 ).filter(extract('year', Toot.created_at) == 2022
).group_by('count' ).group_by('count'
).order_by(desc(func.count('count')) ).order_by(desc(func.count('count'))
).all()[:3]) ).all()[:3])
@ -201,8 +201,7 @@ def graball():
toots = Toot.query.filter(Toot.in_reply_to_id.isnot(None)).all() toots = Toot.query.filter(Toot.in_reply_to_id.isnot(None)).all()
toots_id = [] toots_id = []
for i in toots: for i in toots:
if (Toot.query.get(i.in_reply_to_id) != None if (Toot.query.get(i.in_reply_to_id) != None):
or Other.query.get(i.in_reply_to_id) != None):
continue continue
#context api excluding itself #context api excluding itself
toots_id.append(i.id) toots_id.append(i.id)

View file

@ -26,29 +26,6 @@ class Toot(db.Model):
favourites_count = db.Column(db.Integer) favourites_count = db.Column(db.Integer)
language = db.Column(db.Text) language = db.Column(db.Text)
class Other(db.Model):
id = db.Column(db.Integer, primary_key=True)
acct = db.Column(db.Text)
url = db.Column(db.Text)
created_at = db.Column(db.DateTime)
edited_at = db.Column(db.DateTime)
in_reply_to_id = db.Column(db.Integer)
in_reply_to_account_id = db.Column(db.Integer)
content = db.Column(db.Text)
media_list = db.Column(db.Text)
emoji_list = db.Column(db.Text)
spoiler_text = db.Column(db.Text)
poll_id = db.Column(db.Integer)
visibility = db.Column(db.Text)
reblogged = db.Column(db.Boolean)
favourited = db.Column(db.Boolean)
bookmarked = db.Column(db.Boolean)
sensitive = db.Column(db.Boolean)
replies_count = db.Column(db.Integer)
reblogs_count = db.Column(db.Integer)
favourites_count = db.Column(db.Integer)
language = db.Column(db.Text)
class Tag(db.Model): class Tag(db.Model):
__table_args__ = {'sqlite_autoincrement': True} __table_args__ = {'sqlite_autoincrement': True}
tag_id = db.Column(db.Integer, primary_key=True) tag_id = db.Column(db.Integer, primary_key=True)

View file

@ -3,7 +3,7 @@
from mastodon import Mastodon from mastodon import Mastodon
from tenacity import * from tenacity import *
from BDSM import db from BDSM import db
from BDSM.models import Other, Toot, Tag, Media, Emoji, Poll, Settings from BDSM.models import Toot, Tag, Media, Emoji, Poll, Settings
import sys import sys
import dateutil.parser import dateutil.parser
@ -53,7 +53,6 @@ def get_context(url, toot_id):
def toot_process(statuses, my_acct, duplicates_counter=0): def toot_process(statuses, my_acct, duplicates_counter=0):
for status in statuses: for status in statuses:
is_reblog = False is_reblog = False
is_myself = False
if status['reblog'] != None: if status['reblog'] != None:
if my_acct == status['reblog']['account']['acct']: if my_acct == status['reblog']['account']['acct']:
reblog_myself = True reblog_myself = True
@ -79,11 +78,6 @@ def toot_process(statuses, my_acct, duplicates_counter=0):
id = status['id'] id = status['id']
acct = status['account']['acct'] acct = status['account']['acct']
if my_acct == acct:
is_myself = True
else:
is_myself = False
url = status['url'] url = status['url']
created_at = status['created_at'] created_at = status['created_at']
@ -182,9 +176,6 @@ def toot_process(statuses, my_acct, duplicates_counter=0):
favourites_count = status['favourites_count'] favourites_count = status['favourites_count']
language = status['language'] language = status['language']
if is_reblog or not is_myself:
table = Other()
else:
table = Toot() table = Toot()
table.id=id table.id=id
@ -209,7 +200,7 @@ def toot_process(statuses, my_acct, duplicates_counter=0):
table.favourites_count=favourites_count table.favourites_count=favourites_count
table.language=language table.language=language
if Toot.query.get(id) != None or Other.query.get(id) != None: if Toot.query.get(id) != None:
duplicates_counter += 1 duplicates_counter += 1
db.session.merge(table) db.session.merge(table)

View file

@ -6,7 +6,7 @@ import json
from flask import render_template, request, url_for, redirect, flash, abort from flask import render_template, request, url_for, redirect, flash, abort
from flask_sqlalchemy import Pagination from flask_sqlalchemy import Pagination
from BDSM import app, db from BDSM import app, db
from BDSM.models import Media, Poll, Settings, Toot, Emoji, Other from BDSM.models import Media, Poll, Settings, Toot, Emoji
from BDSM.toot import app_login, app_register, archive_toot, get_context from BDSM.toot import app_login, app_register, archive_toot, get_context
from mastodon import Mastodon from mastodon import Mastodon
from types import SimpleNamespace from types import SimpleNamespace
@ -70,8 +70,6 @@ def search():
def context(toot_id): def context(toot_id):
def get_reply(reply_id): def get_reply(reply_id):
toots = Toot.query.order_by(Toot.created_at.desc()).filter_by(in_reply_to_id=reply_id).all() toots = Toot.query.order_by(Toot.created_at.desc()).filter_by(in_reply_to_id=reply_id).all()
other_toots = Other.query.order_by(Other.created_at.desc()).filter_by(in_reply_to_id=reply_id).all()
toots = process_toot(toots) + process_toot(other_toots)
for i in toots: for i in toots:
if i.in_reply_to_id != None: if i.in_reply_to_id != None:
@ -82,8 +80,6 @@ def context(toot_id):
toots = [] toots = []
toot_ = Toot.query.get(toot_id) toot_ = Toot.query.get(toot_id)
if toot_ == None:
toot_ = Other.query.get(toot_id)
if toot_ == None: if toot_ == None:
abort(404) abort(404)
@ -95,8 +91,6 @@ def context(toot_id):
while(in_reply_to_id != None): while(in_reply_to_id != None):
toot = [] toot = []
toot_ = Toot.query.get(toots[0].in_reply_to_id) toot_ = Toot.query.get(toots[0].in_reply_to_id)
if toot_ == None:
toot_ = Other.query.get(toots[0].in_reply_to_id)
if toot_ == None: if toot_ == None:
break break
@ -213,17 +207,12 @@ def process_toot(toots_):
toot.created_at = toot.created_at.replace(tzinfo=timezone.utc) toot.created_at = toot.created_at.replace(tzinfo=timezone.utc)
toot.created_at = toot.created_at.astimezone(user_timezone).strftime(fmt) toot.created_at = toot.created_at.astimezone(user_timezone).strftime(fmt)
if hasattr(toot, 'reblog_id'): if toot.acct == settings.account:
toot.is_myself = True toot.is_myself = True
if toot.reblog_id != None: if toot.reblog_id != None:
if toot.reblog_myself:
toot = Toot.query.get(toot.reblog_id) toot = Toot.query.get(toot.reblog_id)
toot = SimpleNamespace(**toot.__dict__) toot = SimpleNamespace(**toot.__dict__)
toot.is_reblog = True toot.is_reblog = True
else:
toot = Other.query.get(toot.reblog_id)
toot = SimpleNamespace(**toot.__dict__)
toot.is_reblog = True
else: else:
toot.is_myself = False toot.is_myself = False