Skip to content

Commit

Permalink
#43 グループに関する情報をトップページに表示など.
Browse files Browse the repository at this point in the history
  • Loading branch information
yoneyan committed Sep 8, 2024
1 parent 8e39145 commit 6534b17
Show file tree
Hide file tree
Showing 8 changed files with 194 additions and 159 deletions.
18 changes: 18 additions & 0 deletions custom_auth/migrations/0005_alter_group_status.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 5.1.1 on 2024-09-08 15:50

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('custom_auth', '0004_group_users_alter_user_groups'),
]

operations = [
migrations.AlterField(
model_name='group',
name='status',
field=models.IntegerField(choices=[(1, '有効'), (10, 'ユーザより廃止'), (11, '運営委員より廃止'), (12, '審査落ち')], default=1, verbose_name='ステータス'),
),
]
2 changes: 1 addition & 1 deletion custom_auth/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class Group(models.Model): # noqa: F811
name = models.CharField("name", max_length=150, unique=True)
name_jp = models.CharField("name(japanese)", max_length=150, unique=True)
comment = models.CharField("comment", max_length=250, default="", blank=True)
status = models.IntegerField("ステータス", default=0, choices=GROUP_STATUS_CHOICES)
status = models.IntegerField("ステータス", default=1, choices=GROUP_STATUS_CHOICES)
allow_service_add = models.BooleanField("サービス追加許可", default=False)
membership_type = models.IntegerField("会員種別", default=1, choices=MEMBERSHIP_TYPE_CHOICES)
membership_expired_at = models.DateTimeField("有効期限", blank=True, null=True)
Expand Down
61 changes: 15 additions & 46 deletions custom_auth/signals.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from django.db.models.signals import post_save, pre_delete, pre_save
from django.dispatch import receiver

from custom_auth.models import User
from custom_auth.models import Group, User
from custom_auth.tool import SignalTool
from dsbd.notify import notify_db_save


Expand All @@ -16,58 +17,26 @@ def user_model_pre_save(sender, instance, **kwargs):
@receiver(post_save, sender=User)
def post_user(sender, instance, created, **kwargs):
if created:
text = get_create_user(True, instance)
text = SignalTool().get_create_user(True, instance)
notify_db_save(table_name="User", type=0, data=text)
else:
text = get_update_user(instance._pre_save_instance, instance)
text = SignalTool().get_update_user(instance._pre_save_instance, instance)
notify_db_save(table_name="User", type=1, data=text)


@receiver(pre_delete, sender=User)
def delete_user(sender, instance, **kwargs):
text = get_create_user(False, instance)
text = SignalTool().get_create_user(False, instance)
notify_db_save(table_name="User", type=2, data=text)


def get_create_user(short, instance):
text = "--%d[%s]--\n" % (
instance.id,
instance.username,
)
return text if not short else text + "有効: %r\nE-Mail: %s\n" % (instance.is_active, instance.email)


def get_update_user(before, after):
text = "%s----更新状況----\n" % (get_create_user(True, before),)
if before.username != after.username:
text += "username: %s => %s\n" % (before.username, after.username)
if before.username_jp != after.username_jp:
text += "username(jp): %s => %s\n" % (before.username, after.username)
if before.email != after.email:
text += "E-Mail: %s => %s\n" % (before.email, after.email)
if before.is_active != after.is_active:
text += "有効: %r => %r\n" % (before.is_active, after.is_active)
text += "------------\n"
return text


def get_create_signup_key(short, instance):
text = "--%d[%s]--\n" % (
instance.id,
instance.key,
)
return text if not short else text + "利用済み: %r\n有効期限: %s\n" % (instance.is_used, instance.expired_at)


def get_update_signup_key(before, after):
text = "%s----更新状況----\n" % (get_create_signup_key(True, before),)
if before.key != after.key:
text += "key: %s => %s\n" % (before.key, after.key)
if before.is_used != after.is_used:
text += "使用済み: %r => %r\n" % (before.is_used, after.is_used)
if before.expired_at != after.expired_at:
text += "有効期限: %s => %s\n" % (before.expired_at, after.expired_at)
if before.comment != after.comment:
text += "comment: %r => %r\n" % (before.comment, after.comment)
text += "------------\n"
return text
@receiver(post_save, sender=Group)
def update_group(sender, instance, created, **kwargs):
# 審査OKステータス変更時にサービス追加許可にする
if instance.is_pass and not instance.allow_service_add:
instance.allow_service_add = True
instance.save(update_fields=["allow_service_add"])
# 審査NGステータス変更時にサービス追加拒否設定にする
if not instance.is_pass and instance.allow_service_add:
instance.allow_service_add = False
instance.save(update_fields=["allow_service_add"])
42 changes: 42 additions & 0 deletions custom_auth/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,45 @@
def random_string(num):
random_list = [random.choice(string.ascii_letters + string.digits) for i in range(num)]
return "".join(random_list)


class SignalTool:
def get_create_user(self, short, instance):
text = "--%d[%s]--\n" % (
instance.id,
instance.username,
)
return text if not short else text + "有効: %r\nE-Mail: %s\n" % (instance.is_active, instance.email)

def get_update_user(self, before, after):
text = "%s----更新状況----\n" % (self.get_create_user(True, before),)
if before.username != after.username:
text += "username: %s => %s\n" % (before.username, after.username)
if before.username_jp != after.username_jp:
text += "username(jp): %s => %s\n" % (before.username, after.username)
if before.email != after.email:
text += "E-Mail: %s => %s\n" % (before.email, after.email)
if before.is_active != after.is_active:
text += "有効: %r => %r\n" % (before.is_active, after.is_active)
text += "------------\n"
return text

def get_create_signup_key(self, short, instance):
text = "--%d[%s]--\n" % (
instance.id,
instance.key,
)
return text if not short else text + "利用済み: %r\n有効期限: %s\n" % (instance.is_used, instance.expired_at)

def get_update_signup_key(self, before, after):
text = "%s----更新状況----\n" % (self.get_create_signup_key(True, before),)
if before.key != after.key:
text += "key: %s => %s\n" % (before.key, after.key)
if before.is_used != after.is_used:
text += "使用済み: %r => %r\n" % (before.is_used, after.is_used)
if before.expired_at != after.expired_at:
text += "有効期限: %s => %s\n" % (before.expired_at, after.expired_at)
if before.comment != after.comment:
text += "comment: %r => %r\n" % (before.comment, after.comment)
text += "------------\n"
return text
40 changes: 23 additions & 17 deletions dsbd/templates/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,32 @@
<div class="container py-4 mb-auto">
<div class="h-900 p-2 rounded-3">
<h2>お知らせ</h2>
<div class="card text-bg-warning mb-3">
<div class="card-body">
<h5 class="card-title">Warning card title</h5>
<p class="card-text">Some quick example text to build on
the card title and make up the
bulk of the card's content.</p>
{% if request.user.groups.count == 0 %}
<div class="card text-bg-light mb-3">
<div class="card-body">
<h5 class="card-title">グループの新規申請手続き</h5>
<p class="card-text">グループの新規申請手続きを行ってください</p>
<a href="{% url 'custom_auth_group:add' %}"
class="btn btn-sm btn-primary">申請はこちらから</a>
</div>
</div>
</div>
{% endif %}
{% if is_new_group_request %}
<div class="card text-bg-warning mb-3">
<div class="card-body">
<h5 class="card-title"><b>グループの申請処理中</b></h5>
<p class="card-text">現在、グループの申請処理中です。<br/>この作業には時間が掛かる可能性があります。</p>
</div>
</div>
{% endif %}
{% for notice in notices %}
<div class="card mb-2">
<div class="card-body">
<h6 class="card-subtitle mb-2 text-body-secondary">
<span
class="badge bg-secondary">{{ notice.start_at }}~
{% if notice.end_at != None %}
{{ notice.end_at }}{% else %}
無期限{% endif %}
</span>
<span class="badge bg-secondary">{{ notice.start_at }} ~
{% if notice.end_at != None %}{{ notice.end_at }}{% else %}
無期限{% endif %}
</span>
{% if notice.is_info %}
<span class="badge text-bg-primary">情報</span>
{% endif %}
Expand All @@ -60,10 +68,8 @@ <h5 class="card-title">{{ notice.title }}</h5>
<div class="modal-header">
<h1 class="modal-title fs-5"
id="detailLabel">{{ notice.title }}</h1>
<button type="button"
class="btn-close"
data-bs-dismiss="modal"
aria-label="Close"></button>
<button type="button" class="btn-close"
data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
{{ notice.body | linebreaksbr }}
Expand Down
3 changes: 3 additions & 0 deletions dsbd/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ def activate_user(request, activate_token):

@login_required
def index(request):
# 申請中のグループが存在するかしないか識別
is_new_group_request = request.user.groups.filter(is_pass=False, status=1).exists()
notice_objects = Notice.objects.get_notice()
# ticket_objects = Ticket.objects.get_ticket(user=request.user).filter(is_solved=False)

Expand Down Expand Up @@ -204,6 +206,7 @@ def index(request):
"notices": notices,
# "tickets": tickets,
"services": services,
"is_new_group_request": is_new_group_request,
}
return render(request, "home.html", context)

Expand Down
102 changes: 7 additions & 95 deletions ticket/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from dsbd.notify import notify_db_save
from ticket.models import Chat, Ticket
from ticket.tool import SignalTool


@receiver(pre_save, sender=Ticket)
Expand All @@ -16,75 +17,19 @@ def ticket_model_pre_save(sender, instance, **kwargs):
@receiver(post_save, sender=Ticket)
def post_ticket(sender, instance, created, **kwargs):
if created:
text = get_create_ticket(True, instance)
text = SignalTool().get_create_ticket(True, instance)
notify_db_save(table_name="Ticket", type=0, data=text)
else:
text = get_update_ticket(instance._pre_save_instance, instance)
text = SignalTool().get_update_ticket(instance._pre_save_instance, instance)
notify_db_save(table_name="Ticket", type=1, data=text)


@receiver(pre_delete, sender=Ticket)
def delete_ticket(sender, instance, **kwargs):
text = get_create_ticket(False, instance)
text = SignalTool().get_create_ticket(False, instance)
notify_db_save(table_name="Ticket", type=2, data=text)


def get_create_ticket(short, instance):
text = "--%d[%s]--\nUser: %s| Group: %s\n" % (instance.id, instance.title, instance.user, instance.group)
return (
text
if not short
else text
+ "title: %s\nbody: %s\n解決済み: %s\n承認済み: %s\n拒否済み: %s\n運営委員から起票: %s\n"
% (
instance.title,
instance.body,
solved_to_str(instance.is_solved),
approve_to_str(instance.is_approve),
reject_to_str(instance.is_reject),
from_admin_to_str(instance.from_admin),
)
)


def get_update_ticket(before, after):
text = "%s----更新状況----\n" % (get_create_ticket(True, before),)
if before.user != after.user:
text += "user: %s => %s\n" % (before.user, after.user)
if before.group != after.group:
text += "group: %s => %s\n" % (before.group, after.group)
if before.title != after.title:
text += "title: %s => %s\n" % (before.title, after.title)
if before.body != after.body:
text += "body: %s => %s\n" % (before.body, after.body)
if before.is_solved != after.is_solved:
text += "解決済み: %s => %s\n" % (solved_to_str(before.is_solved), solved_to_str(after.is_solved))
if before.is_approve != after.is_approve:
text += "承認済み: %s => %s\n" % (approve_to_str(before.is_approve), approve_to_str(after.is_approve))
if before.is_reject != after.is_reject:
text += "拒否済み: %s => %s\n" % (reject_to_str(before.is_reject), reject_to_str(after.is_reject))
if before.from_admin != after.from_admin:
text += "運営委員から起票: %s => %s\n" % (from_admin_to_str(before.from_admin), reject_to_str(after.from_admin))
text += "------------\n"
return text


def solved_to_str(is_solved):
return "解決済み" if is_solved else "未解決"


def approve_to_str(is_approve):
return "承認" if is_approve else "未承認"


def reject_to_str(is_reject):
return "拒否済み" if is_reject else "not 拒否"


def from_admin_to_str(from_admin):
return "運営委員から起票" if from_admin else "not 運営委員から起票"


@receiver(pre_save, sender=Chat)
def chat_model_pre_save(sender, instance, **kwargs):
try:
Expand All @@ -96,47 +41,14 @@ def chat_model_pre_save(sender, instance, **kwargs):
@receiver(post_save, sender=Chat)
def post_chat(sender, instance, created, **kwargs):
if created:
text = get_create_chat(True, instance)
text = SignalTool().get_create_chat(True, instance)
notify_db_save(table_name="Chat", type=0, data=text)
else:
text = get_update_chat(instance._pre_save_instance, instance)
text = SignalTool().get_update_chat(instance._pre_save_instance, instance)
notify_db_save(table_name="Chat", type=1, data=text)


@receiver(pre_delete, sender=Chat)
def delete_chat(sender, instance, **kwargs):
text = get_create_chat(False, instance)
text = SignalTool().get_create_chat(False, instance)
notify_db_save(table_name="Chat", type=2, data=text)


def get_create_chat(short, instance):
text = "--%d--\n[%s] User: %s| Group: %s\nTicket: %s\n" % (
instance.id,
"ユーザ投稿" if instance.is_admin else "管理者投稿",
instance.user,
instance.group,
instance.ticket,
)
return text if not short else text + "body: %s\n" % (instance.body,)


def get_update_chat(before, after):
text = "%s----更新状況----\n" % (get_create_chat(True, before),)
if before.user != after.user:
text += "user: %s => %s\n" % (before.user, after.user)
if before.group != after.group:
text += "group: %s => %s\n" % (before.group, after.group)
if before.body != after.body:
text += "body: %s => %s\n" % (before.body, after.body)
if before.is_admin != after.is_admin:
text += "運営委員回答: %s => %s\n" % (admin_to_str(before.is_admin), admin_to_str(after.is_admin))
text += "------------\n"
return text


def admin_to_str(is_admin):
return "運営委員回答" if is_admin else "not 運営委員回答"


def active_to_str(is_admin):
return "有効" if is_admin else "無効"
Loading

0 comments on commit 6534b17

Please sign in to comment.