Skip to content

Commit

Permalink
Part 26 Notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
zeliangyao committed Aug 29, 2021
1 parent 6e74399 commit 901cfd2
Show file tree
Hide file tree
Showing 103 changed files with 5,311 additions and 1,983 deletions.
20 changes: 20 additions & 0 deletions book/migrations/0028_auto_20210827_2055.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 2.2.10 on 2021-08-27 18:55

import datetime
from django.db import migrations, models
from django.utils.timezone import utc


class Migration(migrations.Migration):

dependencies = [
('book', '0027_auto_20210825_2026'),
]

operations = [
migrations.AlterField(
model_name='borrowrecord',
name='end_day',
field=models.DateTimeField(default=datetime.datetime(2021, 9, 3, 18, 55, 30, 923271, tzinfo=utc)),
),
]
20 changes: 20 additions & 0 deletions book/migrations/0029_auto_20210827_2144.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 2.2.10 on 2021-08-27 19:44

import datetime
from django.db import migrations, models
from django.utils.timezone import utc


class Migration(migrations.Migration):

dependencies = [
('book', '0028_auto_20210827_2055'),
]

operations = [
migrations.AlterField(
model_name='borrowrecord',
name='end_day',
field=models.DateTimeField(default=datetime.datetime(2021, 9, 3, 19, 44, 35, 459382, tzinfo=utc)),
),
]
14 changes: 14 additions & 0 deletions book/notification.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from notifications.signals import notify
from django.contrib.auth.models import User,Group




def send_notification(sender,target,verb):
if not sender.is_superuser:
notify.send(
sender,
recipient=User.objects.filter(is_superuser=1),
verb=verb,
target=target,
)
19 changes: 19 additions & 0 deletions book/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@



def mydeco(f):
# This function is what we "replace" hello with
def wrapper(*args, **kw):
print(args[0])
print(args[1])
return f(*args, **kw) # Call hello

return wrapper



@mydeco
def person(name,age,address,code):
print(name,age,address,code)

person("test",12,333,444)
7 changes: 5 additions & 2 deletions book/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from django.conf.urls.static import static
from .views import BorrowRecordListView,BorrowRecordCreateView,BorrowRecordDeleteView,BorrowRecordDetailView,auto_member,auto_book,BorrowRecordClose
from .views import DataCenterView,download_data
from .views import ChartView,global_serach,EmployeeView,EmployeeDetailView,EmployeeUpdate
from .views import ChartView,global_serach,EmployeeView,EmployeeDetailView,EmployeeUpdate,CatNoticeListView,CatNoticeUpdateView

urlpatterns = [

Expand Down Expand Up @@ -74,8 +74,11 @@
# Employee
path('employees/',EmployeeView.as_view(),name="employees_list"),
path('employees-detail/<int:pk>',EmployeeDetailView.as_view(),name="employees_detail"),
path('employees-update/<int:pk>',EmployeeUpdate,name='employee_update')
path('employees-update/<int:pk>',EmployeeUpdate,name='employee_update'),

# Notice
path('notice-list/', CatNoticeListView.as_view(), name='notice_list'),
path('notice-update/', CatNoticeUpdateView.as_view(), name='notice_update'),
]


Expand Down
31 changes: 0 additions & 31 deletions book/utils.py

This file was deleted.

46 changes: 44 additions & 2 deletions book/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
from django.contrib.contenttypes.models import ContentType
from comment.models import Comment
from comment.forms import CommentForm

from notifications.signals import notify
from .notification import send_notification


TODAY=get_n_days_ago(0,"%Y%m%d")
Expand Down Expand Up @@ -317,6 +318,12 @@ class CategoryCreateView(LoginRequiredMixin,CreateView):
fields=['name']
template_name='book/category_create.html'

def form_valid(self, form):
new_cat = form.save(commit=False)
new_cat.save()
send_notification(self.request.user,new_cat,verb=f'Add New Category << {new_cat.name} >>')
return super(CategoryCreateView, self).form_valid(form)


def post(self,request, *args, **kwargs):
super(CategoryCreateView,self).post(request)
Expand All @@ -325,6 +332,7 @@ def post(self,request, *args, **kwargs):
UserActivity.objects.create(created_by=self.request.user.username,
target_model=self.model.__name__,
detail =f"Create {self.model.__name__} << {new_cat_name} >>")

return redirect('category_list')

class CategoryDeleteView(LoginRequiredMixin,View):
Expand All @@ -336,6 +344,7 @@ def get(self,request,*args,**kwargs):
model_name = delete_cat.__class__.__name__
messages.error(request, f"Category << {delete_cat.name} >> Removed")
delete_cat.delete()
send_notification(self.request.user,delete_cat,verb=f'Delete Category << {delete_cat.name} >>')
UserActivity.objects.create(created_by=self.request.user.username,
operation_type="danger",
target_model=model_name,
Expand Down Expand Up @@ -563,6 +572,8 @@ def form_valid(self, form):
self.object = form.save()
self.object.created_by = self.request.user.username
self.object.save(update_fields=['created_by'])
send_notification(self.request.user,self.object,f'Add new memeber {self.object.name}')

return HttpResponseRedirect(self.get_success_url())


Expand Down Expand Up @@ -601,6 +612,9 @@ def get(self,request,*args,**kwargs):
model_name = delete_member.__class__.__name__
messages.error(request, f"Member << {delete_member.name} >> Removed")
delete_member.delete()
send_notification(self.request.user,delete_member,f'Delete member {delete_member.name} ')


UserActivity.objects.create(created_by=self.request.user.username,
operation_type="danger",
target_model=model_name,
Expand Down Expand Up @@ -911,4 +925,32 @@ def EmployeeUpdate(request,pk):
current_user.groups.add(group)
messages.success(request, f"Group for << {current_user.username} >> has been updated")
return redirect('employees_detail', pk=pk)




# Notice

class CatNoticeListView(SuperUserRequiredMixin, ListView):
context_object_name = 'notices'
template_name = 'notice_list.html'
login_url = 'login'

# 未读通知的查询集
def get_queryset(self):
return self.request.user.notifications.unread()


class CatNoticeUpdateView(SuperUserRequiredMixin,View):
"""Update Status of Notification"""
# 处理 get 请求
def get(self, request):
# 获取未读消息
notice_id = request.GET.get('notice_id')
# 更新单条通知
if notice_id:
request.user.notifications.get(id=notice_id).mark_as_read()
return redirect('category_list')
# 更新全部通知
else:
request.user.notifications.mark_all_as_read()
return redirect('notice_list')
55 changes: 55 additions & 0 deletions comment/migrations/0003_auto_20210827_2055.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Generated by Django 2.2.10 on 2021-08-27 18:55

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
import mptt.fields


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('comment', '0002_auto_20210825_2026'),
]

operations = [
migrations.AlterModelOptions(
name='comment',
options={},
),
migrations.AddField(
model_name='comment',
name='level',
field=models.PositiveIntegerField(default=0, editable=False),
preserve_default=False,
),
migrations.AddField(
model_name='comment',
name='lft',
field=models.PositiveIntegerField(default=0, editable=False),
preserve_default=False,
),
migrations.AddField(
model_name='comment',
name='parent',
field=mptt.fields.TreeForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='children', to='comment.Comment'),
),
migrations.AddField(
model_name='comment',
name='reply_to',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='replyers', to=settings.AUTH_USER_MODEL),
),
migrations.AddField(
model_name='comment',
name='rght',
field=models.PositiveIntegerField(default=0, editable=False),
preserve_default=False,
),
migrations.AddField(
model_name='comment',
name='tree_id',
field=models.PositiveIntegerField(db_index=True, default=0, editable=False),
preserve_default=False,
),
]
41 changes: 41 additions & 0 deletions comment/migrations/0004_auto_20210827_2144.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Generated by Django 2.2.10 on 2021-08-27 19:44

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('comment', '0003_auto_20210827_2055'),
]

operations = [
migrations.AlterModelOptions(
name='comment',
options={'ordering': ('created_at',)},
),
migrations.RemoveField(
model_name='comment',
name='level',
),
migrations.RemoveField(
model_name='comment',
name='lft',
),
migrations.RemoveField(
model_name='comment',
name='parent',
),
migrations.RemoveField(
model_name='comment',
name='reply_to',
),
migrations.RemoveField(
model_name='comment',
name='rght',
),
migrations.RemoveField(
model_name='comment',
name='tree_id',
),
]
5 changes: 2 additions & 3 deletions core/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
# 'app',
'book',
'crispy_forms',
'crispy_tailwind',
Expand All @@ -42,8 +41,8 @@
'rest_framework',
'ckeditor',
'comment',
'mptt',
]
'notifications',
]

CRISPY_ALLOWED_TEMPLATE_PACKS = "tailwind"
CRISPY_TEMPLATE_PACK = "tailwind"
Expand Down
3 changes: 2 additions & 1 deletion core/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
from django.contrib import admin
from django.urls import path, include # add this
# from django.conf.urls import handler400, handler403, handler404, handler500
import notifications.urls

urlpatterns = [
path('admin/', admin.site.urls),
path("auth/", include("authentication.urls")), # Auth routes - login / register
path("", include("book.urls")),
path('api/', include('Api.urls')),
path('comment/', include('comment.urls', namespace='comment')),

path('inbox/notifications/', include(notifications.urls, namespace='notifications')),
]


Expand Down
Binary file modified db.sqlite3
Binary file not shown.
Loading

0 comments on commit 901cfd2

Please sign in to comment.