Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

评论审核后才能显示 #654

Merged
merged 3 commits into from
Apr 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions comments/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ class CommentAdmin(admin.ModelAdmin):
'link_to_article',
'is_enable',
'created_time')
list_display_links = ('id', 'body')
list_filter = ('author', 'article', 'is_enable')
list_display_links = ('id', 'body', 'is_enable')
list_filter = ('is_enable', 'author', 'article',)
exclude = ('created_time', 'last_mod_time')
actions = [disable_commentstatus, enable_commentstatus]

Expand Down
18 changes: 18 additions & 0 deletions comments/migrations/0002_alter_comment_is_enable.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.1.7 on 2023-04-24 13:48

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('comments', '0001_initial'),
]

operations = [
migrations.AlterField(
model_name='comment',
name='is_enable',
field=models.BooleanField(default=False, verbose_name='是否显示'),
),
]
5 changes: 1 addition & 4 deletions comments/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Comment(models.Model):
null=True,
on_delete=models.CASCADE)
is_enable = models.BooleanField(
'是否显示', default=True, blank=False, null=False)
'是否显示', default=False, blank=False, null=False)

class Meta:
ordering = ['-id']
Expand All @@ -36,6 +36,3 @@ class Meta:

def __str__(self):
return self.body

def save(self, *args, **kwargs):
super().save(*args, **kwargs)
12 changes: 11 additions & 1 deletion comments/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ def setUp(self):
self.client = Client()
self.factory = RequestFactory()

def update_article_comment_status(self, article):
comments = article.comment_set.all()
for comment in comments:
comment.is_enable = True
comment.save()

def test_validate_comment(self):
site = get_current_site().domain
user = BlogUser.objects.create_superuser(
Expand Down Expand Up @@ -53,6 +59,9 @@ def test_validate_comment(self):
self.assertEqual(response.status_code, 302)

article = Article.objects.get(pk=article.pk)
self.assertEqual(len(article.comment_list()), 0)
self.update_article_comment_status(article)

self.assertEqual(len(article.comment_list()), 1)

response = self.client.post(comment_url,
Expand All @@ -63,6 +72,7 @@ def test_validate_comment(self):
self.assertEqual(response.status_code, 302)

article = Article.objects.get(pk=article.pk)
self.update_article_comment_status(article)
self.assertEqual(len(article.comment_list()), 2)
parent_comment_id = article.comment_list()[0].id

Expand All @@ -85,7 +95,7 @@ def test_validate_comment(self):
})

self.assertEqual(response.status_code, 302)

self.update_article_comment_status(article)
article = Article.objects.get(pk=article.pk)
self.assertEqual(len(article.comment_list()), 3)
comment = Comment.objects.get(id=parent_comment_id)
Expand Down
40 changes: 20 additions & 20 deletions djangoblog/blog_signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,26 +88,26 @@ def model_post_save_callback(
clearcache = True

if isinstance(instance, Comment):

path = instance.article.get_absolute_url()
site = get_current_site().domain
if site.find(':') > 0:
site = site[0:site.find(':')]

expire_view_cache(
path,
servername=site,
serverport=80,
key_prefix='blogdetail')
if cache.get('seo_processor'):
cache.delete('seo_processor')
comment_cache_key = 'article_comments_{id}'.format(
id=instance.article.id)
cache.delete(comment_cache_key)
delete_sidebar_cache()
delete_view_cache('article_comments', [str(instance.article.pk)])

_thread.start_new(send_comment_email, (instance,))
if instance.is_enable:
path = instance.article.get_absolute_url()
site = get_current_site().domain
if site.find(':') > 0:
site = site[0:site.find(':')]

expire_view_cache(
path,
servername=site,
serverport=80,
key_prefix='blogdetail')
if cache.get('seo_processor'):
cache.delete('seo_processor')
comment_cache_key = 'article_comments_{id}'.format(
id=instance.article.id)
cache.delete(comment_cache_key)
delete_sidebar_cache()
delete_view_cache('article_comments', [str(instance.article.pk)])

_thread.start_new(send_comment_email, (instance,))

if clearcache:
cache.clear()
Expand Down
2 changes: 1 addition & 1 deletion djangoblog/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def env_to_bool(env, default):
'ENGINE': 'django.db.backends.mysql',
'NAME': os.environ.get('DJANGO_MYSQL_DATABASE') or 'djangoblog',
'USER': os.environ.get('DJANGO_MYSQL_USER') or 'root',
'PASSWORD': os.environ.get('DJANGO_MYSQL_PASSWORD') or 'djangoblog_123',
'PASSWORD': os.environ.get('DJANGO_MYSQL_PASSWORD') or 'root',
'HOST': os.environ.get('DJANGO_MYSQL_HOST') or '127.0.0.1',
'PORT': int(
os.environ.get('DJANGO_MYSQL_PORT') or 3306),
Expand Down
4 changes: 2 additions & 2 deletions servermanager/api/commonapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
logger = logging.getLogger(__name__)

openai.api_key = os.environ.get('OPENAI_API_KEY')
if os.environ.get('PROXY'):
openai.proxy = os.environ.get('PROXY')
if os.environ.get('HTTP_PROXY'):
openai.proxy = os.environ.get('HTTP_PROXY')


class ChatGPT:
Expand Down
2 changes: 1 addition & 1 deletion templates/comments/tags/post_comment.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ <h3 id="reply-title" class="comment-reply-title">发表评论
</p>
{{ form.parent_comment_id }}
<div class="form-submit">
<span class="comment-markdown"> 支持markdown</span>
<span class="comment-markdown"> 支持markdown,评论经审核后才会显示。</span>
<input name="submit" type="submit" id="submit" class="submit" value="发表评论"/>
<small class="cancel-comment" id="cancel_comment" style="display: none">
<a href="javascript:void(0)" id="cancel-comment-reply-link" onclick="cancel_reply()">取消回复</a>
Expand Down