Skip to content

Commit

Permalink
Merge pull request #347 from sparcs-kaist/develop
Browse files Browse the repository at this point in the history
Add pre-commit hook & Remove board pagination
  • Loading branch information
injoonH authored Oct 13, 2022
2 parents 6a384c5 + dbde1f8 commit a8dcb82
Show file tree
Hide file tree
Showing 21 changed files with 553 additions and 988 deletions.
35 changes: 21 additions & 14 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.3.0
- repo: https://github.com/psf/black
rev: 22.10.0
hooks:
- id: check-yaml
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/psf/black
rev: 19.10b0
- id: black
args: [--safe]

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.3.0
hooks:
- id: black
- repo: https://github.com/pre-commit/mirrors-pylint
rev: v2.4.4
- id: check-added-large-files
- id: check-ast
- id: check-yaml
- id: end-of-file-fixer
- id: mixed-line-ending
args: [--fix=lf]
- id: name-tests-test
args: [--pytest-test-first]
- id: trailing-whitespace

- repo: https://github.com/PyCQA/isort
rev: 5.10.1
hooks:
- id: pylint
additional_dependencies: [django, pylint-django]
args:
- --rcfile=./.pylintrc
- id: isort
args: [--profile=black]
4 changes: 2 additions & 2 deletions .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,8 @@ max-module-lines=1000
# separator` is used to allow tabulation in dicts, etc.: {1 : 1,\n222: 2}.
# `trailing-comma` allows a space between comma and closing bracket: (a, ).
# `empty-line` allows space-only lines.
no-space-check=trailing-comma,
dict-separator
#no-space-check=trailing-comma,
# dict-separator

# Allow the body of a class to be on the same line as the declaration if body
# contains single statement.
Expand Down
9 changes: 5 additions & 4 deletions apps/core/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
Comment,
CommunicationArticle
)
from ara.settings import MIN_TIME


class HiddenContentListFilter(admin.SimpleListFilter):
Expand All @@ -34,11 +35,11 @@ def lookups(self, request, model_admin):
def queryset(self, request, queryset):
if self.value() == 'hidden':
return queryset.exclude(
hidden_at=timezone.datetime.min.replace(tzinfo=timezone.utc)
hidden_at=MIN_TIME
)
if self.value() == 'not_hidden':
return queryset.filter(
hidden_at=timezone.datetime.min.replace(tzinfo=timezone.utc)
hidden_at=MIN_TIME
)


Expand Down Expand Up @@ -121,7 +122,7 @@ class ArticleAdmin(MetaDataModelAdmin):
@admin.action(description=gettext('Restore hidden articles'))
def restore_hidden_articles(self, request, queryset):
rows_updated = queryset.update(
hidden_at=timezone.datetime.min.replace(tzinfo=timezone.utc)
hidden_at=MIN_TIME
)
self.message_user(request, f'{rows_updated}개의 게시물(들)이 성공적으로 복구되었습니다.')

Expand Down Expand Up @@ -154,7 +155,7 @@ class CommentAdmin(MetaDataModelAdmin):
@admin.action(description=gettext('Restore hidden comments'))
def restore_hidden_comments(self, request, queryset):
rows_updated = queryset.update(
hidden_at=timezone.datetime.min.replace(tzinfo=timezone.utc)
hidden_at=MIN_TIME
)
self.message_user(request, f'{rows_updated}개의 댓글(들)이 성공적으로 복구되었습니다.')

Expand Down
8 changes: 4 additions & 4 deletions apps/core/models/article.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from ara.classes.decorator import cache_by_user
from ara.db.models import MetaDataModel
from ara.sanitizer import sanitize
from ara.settings import HASH_SECRET_VALUE, SCHOOL_RESPONSE_VOTE_THRESHOLD, ANSWER_PERIOD
from ara.settings import HASH_SECRET_VALUE, SCHOOL_RESPONSE_VOTE_THRESHOLD, ANSWER_PERIOD, MIN_TIME
from .block import Block
from .report import Report
from .comment import Comment
Expand Down Expand Up @@ -148,7 +148,7 @@ class Meta(MetaDataModel.Meta):
)

hidden_at = models.DateTimeField(
default=timezone.datetime.min.replace(tzinfo=timezone.utc),
default=MIN_TIME,
verbose_name='숨김 시간',
)

Expand Down Expand Up @@ -178,7 +178,7 @@ def update_hit_count(self):

def update_comment_count(self):
self.comment_count = Comment.objects.filter(
deleted_at=timezone.datetime.min.replace(tzinfo=timezone.utc)).filter(
deleted_at=MIN_TIME).filter(
models.Q(parent_article=self) |
models.Q(parent_comment__parent_article=self)
).count()
Expand Down Expand Up @@ -209,7 +209,7 @@ def update_vote_status(self):
self.save()

def is_hidden_by_reported(self) -> bool:
return self.hidden_at != timezone.datetime.min.replace(tzinfo=timezone.utc)
return self.hidden_at != MIN_TIME

@property
def created_by_nickname(self):
Expand Down
104 changes: 46 additions & 58 deletions apps/core/models/board.py
Original file line number Diff line number Diff line change
@@ -1,138 +1,126 @@
from enum import IntEnum, auto

from django.db import models
from enum import IntEnum
from django_extensions.db.fields import AutoSlugField

from ara.db.models import MetaDataModel


class BoardNameType(IntEnum):
REGULAR = 0
ANONYMOUS = 1
REALNAME = 2
REGULAR = auto()
ANONYMOUS = auto()
REALNAME = auto()


class BoardAccessPermissionType(IntEnum):
READ = 0
WRITE = 1
COMMENT = 2
READ = auto()
WRITE = auto()
COMMENT = auto()


class Board(MetaDataModel):
class Meta(MetaDataModel.Meta):
verbose_name = '게시판'
verbose_name_plural = '게시판 목록'
verbose_name = "게시판"
verbose_name_plural = "게시판 목록"
unique_together = (
('ko_name', 'deleted_at'),
('en_name', 'deleted_at'),
("ko_name", "deleted_at"),
("en_name", "deleted_at"),
)

slug = AutoSlugField(
populate_from=[
'en_name',
"en_name",
],
)
ko_name = models.CharField(
max_length=32,
verbose_name='게시판 국문 이름',
verbose_name="게시판 국문 이름",
)
en_name = models.CharField(
max_length=32,
verbose_name='게시판 영문 이름',
verbose_name="게시판 영문 이름",
)
ko_description = models.TextField(
verbose_name='게시판 국문 소개',
verbose_name="게시판 국문 소개",
)
en_description = models.TextField(
verbose_name='게시판 영문 소개',
verbose_name="게시판 영문 소개",
)

# 사용자 그룹에 대해 접근 권한을 제어하는 bit mask 입니다.
# access_mask & (1 << user.group) > 0 일 때 접근이 가능합니다.
# 사용자 그룹의 값들은 `UserGroup`을 참고하세요.
read_access_mask = models.SmallIntegerField(
# UNAUTHORIZED, EXTERNAL_ORG 제외 모든 사용자 읽기 권한 부여
default=0b11011110,
default=0b011011110,
null=False,
verbose_name='읽기 권한'
verbose_name="읽기 권한",
)
write_access_mask = models.SmallIntegerField(
# UNAUTHORIZED, STORE_EMPLOYEE, EXTERNAL_ORG 제외 모든 사용자 쓰기 권한 부여
default=0b11011010,
default=0b011011010,
null=False,
verbose_name='쓰기 권한'
verbose_name="쓰기 권한",
)
comment_access_mask = models.SmallIntegerField(
# UNAUTHORIZED 제외 모든 사용자 댓글 권한 부여
default=0b11111110,
null=False,
verbose_name='댓글 권한'
default=0b011111110,
null=False,
verbose_name="댓글 권한",
)

is_readonly = models.BooleanField(
verbose_name='읽기 전용 게시판',
help_text='활성화했을 때 관리자만 글을 쓸 수 있습니다. (ex. 포탈공지)',
default=False
verbose_name="읽기 전용 게시판",
help_text="활성화했을 때 관리자만 글을 쓸 수 있습니다. (ex. 포탈공지)",
default=False,
)
is_hidden = models.BooleanField(
verbose_name='리스트 숨김 게시판',
help_text='활성화했을 때 메인페이지 상단바 리스트에 나타나지 않습니다. (ex. 뉴아라공지)',
verbose_name="리스트 숨김 게시판",
help_text="활성화했을 때 메인페이지 상단바 리스트에 나타나지 않습니다. (ex. 뉴아라공지)",
default=False,
db_index=True,
)

name_type = models.SmallIntegerField(
verbose_name='익명/실명 게시판',
help_text='게시판의 글과 댓글들이 익명 혹은 실명이도록 합니다.',
verbose_name="익명/실명 게시판",
help_text="게시판의 글과 댓글들이 익명 혹은 실명이도록 합니다.",
default=BoardNameType.REGULAR,
db_index=True
db_index=True,
)

is_school_communication = models.BooleanField(
verbose_name='학교와의 소통 게시판',
help_text='학교 소통 게시판 글임을 표시',
verbose_name="학교와의 소통 게시판",
help_text="학교 소통 게시판 글임을 표시",
default=False,
db_index=True
)

group_id = models.IntegerField(
verbose_name='그룹 ID',
default=1
db_index=True,
)

group_id = models.IntegerField(verbose_name="그룹 ID", default=1)
banner_image = models.ImageField(
default='default_banner.png',
upload_to='board_banner_images',
verbose_name='게시판 배너 이미지',
default="default_banner.png",
upload_to="board_banner_images",
verbose_name="게시판 배너 이미지",
)

ko_banner_description = models.TextField(
null=True,
blank=True,
default=None,
verbose_name='게시판 배너에 삽입되는 국문 소개',
verbose_name="게시판 배너에 삽입되는 국문 소개",
)

en_banner_description = models.TextField(
null=True,
blank=True,
default=None,
verbose_name='게시판 배너에 삽입되는 영문 소개',
verbose_name="게시판 배너에 삽입되는 영문 소개",
)

banner_url = models.TextField(
null=True,
blank=True,
default=None,
verbose_name='게시판 배너를 클릭 시에 이동하는 링크',
verbose_name="게시판 배너를 클릭 시에 이동하는 링크",
)

def __str__(self) -> str:
return self.ko_name

def group_has_access_permission(
self,
access_type: BoardAccessPermissionType,
group: int) -> bool:
self, access_type: BoardAccessPermissionType, group: int
) -> bool:
mask = None
if access_type == BoardAccessPermissionType.READ:
mask = self.read_access_mask
Expand All @@ -143,5 +131,5 @@ def group_has_access_permission(
else:
# TODO: Handle error
return False

return (mask & (1 << group)) > 0
8 changes: 4 additions & 4 deletions apps/core/models/comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from ara.classes.decorator import cache_by_user
from ara.db.models import MetaDataModel, MetaDataQuerySet
from ara.sanitizer import sanitize
from ara.settings import HASH_SECRET_VALUE
from ara.settings import HASH_SECRET_VALUE, MIN_TIME
from .block import Block
from .report import Report
from .board import BoardNameType
Expand Down Expand Up @@ -95,7 +95,7 @@ class Meta(MetaDataModel.Meta):
verbose_name='댓글',
)
hidden_at = models.DateTimeField(
default=timezone.datetime.min.replace(tzinfo=timezone.utc),
default=MIN_TIME,
verbose_name='숨김 시간',
)

Expand Down Expand Up @@ -139,10 +139,10 @@ def get_parent_article(self):
return self.parent_comment.parent_article

def is_hidden_by_reported(self) -> bool:
return self.hidden_at != timezone.datetime.min.replace(tzinfo=timezone.utc)
return self.hidden_at != MIN_TIME

def is_deleted(self) -> bool:
return self.deleted_at != timezone.datetime.min.replace(tzinfo=timezone.utc)
return self.deleted_at != MIN_TIME

@transaction.atomic
def update_report_count(self):
Expand Down
8 changes: 5 additions & 3 deletions apps/core/models/communication_article.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

from enum import IntEnum

from ara.settings import MIN_TIME


class SchoolResponseStatus(IntEnum):
BEFORE_UPVOTE_THRESHOLD = 0
Expand All @@ -31,12 +33,12 @@ class Meta(MetaDataModel.Meta):
)

response_deadline = models.DateTimeField(
default=timezone.datetime.min.replace(tzinfo=timezone.utc),
default=MIN_TIME,
verbose_name='답변 요청 기한',
)

answered_at = models.DateTimeField(
default=timezone.datetime.min.replace(tzinfo=timezone.utc),
default=MIN_TIME,
verbose_name='학교측 답변을 받은 시각',
)

Expand All @@ -52,7 +54,7 @@ def get_status_string(self) -> str:

@cached_property
def days_left(self) -> int:
if self.response_deadline == timezone.datetime.min.replace(tzinfo=timezone.utc):
if self.response_deadline == MIN_TIME:
return sys.maxsize
else:
return (self.response_deadline.astimezone(timezone.localtime().tzinfo) - timezone.localtime()).days
Expand Down
3 changes: 2 additions & 1 deletion apps/core/models/signals/block.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from apps.core.models import Block
from ara import redis
from ara.settings import MIN_TIME


@receiver(models.signals.post_save, sender=Block)
Expand All @@ -22,7 +23,7 @@ def delete_block_from_redis(block):
pipe.zrem(redis_key, f'{block.user_id}')
pipe.execute(raise_on_error=True)

deleted = instance.deleted_at != timezone.datetime.min.replace(tzinfo=timezone.utc)
deleted = instance.deleted_at != MIN_TIME

if created:
add_block_to_redis(instance)
Expand Down
Loading

0 comments on commit a8dcb82

Please sign in to comment.