Skip to content

Commit

Permalink
Delete the corresponding activity after deleting the post
Browse files Browse the repository at this point in the history
  • Loading branch information
dongweiming committed Aug 25, 2021
1 parent 450b622 commit 38f41cf
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
10 changes: 10 additions & 0 deletions docs/quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ python app.py

如果你要部署到自己的服务器上,可以参考 [部署](deploying.md)

## v3.5不兼容问题

从v3.5开始,删除文章会同时删除对应的动态,可以通过如下命令完成迁移:

```bash
python manage.py migrate-for-v35
```

如果版本<=3.0看下面👇

## v3.0不兼容问题

在v3.0添加了动态功能,如果之前你已经使用的版本>2.7,可以通过如下命令完成迁移:
Expand Down
20 changes: 19 additions & 1 deletion manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import click
import cssmin
from tortoise import Tortoise, run_async
from tortoise.exceptions import IntegrityError
from tortoise.exceptions import IntegrityError, OperationalError

from config import HERE
from ext import init_db
Expand Down Expand Up @@ -34,6 +34,11 @@ async def init() -> None:
'show columns from `posts` like "pageview"'):
await migrate_for_v25()

try:
await migrate_for_v35()
except OperationalError:
...


async def _migrate_for_v25() -> None:
await init_db(create_db=False)
Expand Down Expand Up @@ -112,6 +117,13 @@ async def _migrate_for_v30() -> None:
'alter table comments add column `target_kind` smallint(6) DEFAULT 1001')


async def _migrate_for_v35() -> None:
await init_db(create_db=False)
client = Tortoise.get_connection('default')
await client.execute_script(
'alter table activity add index `idx_target_kind` (`target_id`, `target_kind`)') # noqa


@click.group()
def cli():
...
Expand Down Expand Up @@ -141,6 +153,12 @@ def migrate_for_v30():
click.echo('Migrate Finished!')


@cli.command()
def migrate_for_v35():
run_async(_migrate_for_v35())
click.echo('Migrate Finished!')


async def _adduser(**kwargs) -> None:
await init_db()
try:
Expand Down
6 changes: 5 additions & 1 deletion views/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@
from ext import mako
from forms import PostForm, TopicForm, UserForm
from models import Post, PostTag, SpecialTopic, Tag, User
from models.activity import create_status
from models.activity import create_status, Activity
from models.signals import post_created
from models.user import generate_password
from models.utils import generate_id
from models.consts import K_POST
from views.request import Request
from views.utils import json

Expand Down Expand Up @@ -140,6 +141,9 @@ async def post(request, post_id):
if request.method == 'DELETE':
await post.delete()
await PostTag.filter(Q(post_id=post_id)).delete()
activity = await Activity.filter(target_id=post_id, target_kind=K_POST).first()
if activity:
await activity.delete()
return response.json({'r': 1})

rv = await post.to_sync_dict()
Expand Down

0 comments on commit 38f41cf

Please sign in to comment.