質問編集履歴
2
views.pyを途中まで追記致しました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -250,6 +250,320 @@
|
|
250
250
|
|
251
251
|
friends=friends, vals=vlist)
|
252
252
|
|
253
|
+
#GETアクセス時の処理
|
254
|
+
|
255
|
+
else:
|
256
|
+
|
257
|
+
#フォームの用意
|
258
|
+
|
259
|
+
groupsform = GroupSelectForm(request.user)
|
260
|
+
|
261
|
+
friendsform = FriendsForm(request.user, friends=friends, \
|
262
|
+
|
263
|
+
vals=[])
|
264
|
+
|
265
|
+
sel_group = '-'
|
266
|
+
|
267
|
+
|
268
|
+
|
269
|
+
#共通処理
|
270
|
+
|
271
|
+
createform = CreateGroupForm()
|
272
|
+
|
273
|
+
params = {
|
274
|
+
|
275
|
+
'login_user':request.user,
|
276
|
+
|
277
|
+
'groups_form':groupsform,
|
278
|
+
|
279
|
+
'friends_form':friendsform,
|
280
|
+
|
281
|
+
'create_form':createform,
|
282
|
+
|
283
|
+
'group':sel_group,
|
284
|
+
|
285
|
+
}
|
286
|
+
|
287
|
+
return render(request, 'sns/groups.html', params)
|
288
|
+
|
289
|
+
|
290
|
+
|
291
|
+
#Friendの追加処理
|
292
|
+
|
293
|
+
@login_required(login_url='/admin/login/')
|
294
|
+
|
295
|
+
def add(request):
|
296
|
+
|
297
|
+
#追加するUser取得
|
298
|
+
|
299
|
+
add_name = request.GET['name']
|
300
|
+
|
301
|
+
add_user = User.objects.filter(username=add_name).first()
|
302
|
+
|
303
|
+
#Userが本人の場合の処理
|
304
|
+
|
305
|
+
if add_user == request.user:
|
306
|
+
|
307
|
+
messages.info(request, "自分自身をFriendにすることは\
|
308
|
+
|
309
|
+
できません。")
|
310
|
+
|
311
|
+
return redirect(to='/sns')
|
312
|
+
|
313
|
+
#publicの取得
|
314
|
+
|
315
|
+
(public_user, public_group) = get_public()
|
316
|
+
|
317
|
+
#add_userのFriendの数を調べる
|
318
|
+
|
319
|
+
frd_num = Friend.objects.filter(owner=request.user) \
|
320
|
+
|
321
|
+
.filter(user=add_user).count()
|
322
|
+
|
323
|
+
#0より大きければすでに登録済み
|
324
|
+
|
325
|
+
if frd_num > 0:
|
326
|
+
|
327
|
+
messages.info(request, add_user.username + \
|
328
|
+
|
329
|
+
'は既に追加されています。')
|
330
|
+
|
331
|
+
return redirect(to='/sns')
|
332
|
+
|
333
|
+
|
334
|
+
|
335
|
+
#friend登録処理
|
336
|
+
|
337
|
+
frd = Friend()
|
338
|
+
|
339
|
+
frd.owner = request.user
|
340
|
+
|
341
|
+
frd.user = add_user
|
342
|
+
|
343
|
+
frd.group = public_group
|
344
|
+
|
345
|
+
frd.save()
|
346
|
+
|
347
|
+
#メッセージを設定
|
348
|
+
|
349
|
+
messages.success(request, add_user.username + 'を追加しました! \
|
350
|
+
|
351
|
+
groupページに移動して、追加したFriendをメンバーに設定してください')
|
352
|
+
|
353
|
+
return redirect(to='/sns')
|
354
|
+
|
355
|
+
|
356
|
+
|
357
|
+
#グループの作成処理
|
358
|
+
|
359
|
+
@login_required(login_url='/admin/login/')
|
360
|
+
|
361
|
+
def creategroup(request):
|
362
|
+
|
363
|
+
#Groupを作り、Userとtitleを設定して保存する
|
364
|
+
|
365
|
+
gp = Group()
|
366
|
+
|
367
|
+
gp.owner = request.user
|
368
|
+
|
369
|
+
gp.title = request.POST['group_name']
|
370
|
+
|
371
|
+
gp.save()
|
372
|
+
|
373
|
+
messages.info(request,'新しいグループを作成しました。')
|
374
|
+
|
375
|
+
return redirect(to='/sns/groups')
|
376
|
+
|
377
|
+
|
378
|
+
|
379
|
+
#メッセージのポスト処理
|
380
|
+
|
381
|
+
@login_required(login_url='/admin/login/')
|
382
|
+
|
383
|
+
def post(request):
|
384
|
+
|
385
|
+
#POST送信の処理
|
386
|
+
|
387
|
+
if request.method == 'POST':
|
388
|
+
|
389
|
+
#送信内容の取得
|
390
|
+
|
391
|
+
gr_name = request.POST['groups']
|
392
|
+
|
393
|
+
content = request.POST['content']
|
394
|
+
|
395
|
+
#Groupの取得
|
396
|
+
|
397
|
+
group = Group.objects.filter(owner=request.user)\
|
398
|
+
|
399
|
+
.filter(title=gr_name).first()
|
400
|
+
|
401
|
+
if group == None:
|
402
|
+
|
403
|
+
(pub_user, group) = get_public()
|
404
|
+
|
405
|
+
#Messageを作成し設定して保存
|
406
|
+
|
407
|
+
msg = Message()
|
408
|
+
|
409
|
+
msg.owner = request.user
|
410
|
+
|
411
|
+
msg.group = group
|
412
|
+
|
413
|
+
msg.content = content
|
414
|
+
|
415
|
+
msg.save()
|
416
|
+
|
417
|
+
#メッセージを取得
|
418
|
+
|
419
|
+
messages.success(request,'新しいメッセージを投稿しました。')
|
420
|
+
|
421
|
+
return redirect(to='/sns')
|
422
|
+
|
423
|
+
|
424
|
+
|
425
|
+
#GETアクセス時の処理
|
426
|
+
|
427
|
+
else:
|
428
|
+
|
429
|
+
form = PostForm(request.user)
|
430
|
+
|
431
|
+
|
432
|
+
|
433
|
+
params = {
|
434
|
+
|
435
|
+
'login_user':request.user,
|
436
|
+
|
437
|
+
'form':form,
|
438
|
+
|
439
|
+
}
|
440
|
+
|
441
|
+
return render(request, 'sns/post.html', params)
|
442
|
+
|
443
|
+
|
444
|
+
|
445
|
+
#投稿をシェアする
|
446
|
+
|
447
|
+
@login_required(login_url='/admin/login/')
|
448
|
+
|
449
|
+
def share(request, share_id):
|
450
|
+
|
451
|
+
#シェアするメッセージの取得
|
452
|
+
|
453
|
+
share = Message.objects.get(id=share_id)
|
454
|
+
|
455
|
+
|
456
|
+
|
457
|
+
#POST送信時の処理
|
458
|
+
|
459
|
+
if request.method == 'POST':
|
460
|
+
|
461
|
+
#送信内容を取得
|
462
|
+
|
463
|
+
gr_name = request.POST['groups']
|
464
|
+
|
465
|
+
content = request.POST['content']
|
466
|
+
|
467
|
+
#Groupの取得
|
468
|
+
|
469
|
+
group = Group.objects.filter(owner=request.user) \
|
470
|
+
|
471
|
+
.filter(title=gr_name).first()
|
472
|
+
|
473
|
+
|
474
|
+
|
475
|
+
if group == None:
|
476
|
+
|
477
|
+
(pub_user, group) = get_public()
|
478
|
+
|
479
|
+
#メッセージを作成し、設定して保存
|
480
|
+
|
481
|
+
msg = Message()
|
482
|
+
|
483
|
+
msg.owner = request.user
|
484
|
+
|
485
|
+
msg.group = group
|
486
|
+
|
487
|
+
msg.content = content
|
488
|
+
|
489
|
+
msg.share_id = share.id
|
490
|
+
|
491
|
+
msg.save()
|
492
|
+
|
493
|
+
share_msg = msg.get_share()
|
494
|
+
|
495
|
+
share_msg.share_count += 1
|
496
|
+
|
497
|
+
share_msg.save()
|
498
|
+
|
499
|
+
#メッセージを設定
|
500
|
+
|
501
|
+
messages.success(request,'メッセージをシェアしました')
|
502
|
+
|
503
|
+
return redirect(to='/sns')
|
504
|
+
|
505
|
+
|
506
|
+
|
507
|
+
#共通処理
|
508
|
+
|
509
|
+
form = PostForm(request.user)
|
510
|
+
|
511
|
+
params = {
|
512
|
+
|
513
|
+
'login_user':request.user,
|
514
|
+
|
515
|
+
'form':form,
|
516
|
+
|
517
|
+
'share':share,
|
518
|
+
|
519
|
+
}
|
520
|
+
|
521
|
+
return render(request, 'sns/share.html',params)
|
522
|
+
|
523
|
+
|
524
|
+
|
525
|
+
#Goodボタンの処理
|
526
|
+
|
527
|
+
@login_required(login_url='/admin/login/')
|
528
|
+
|
529
|
+
def good(request, good_id):
|
530
|
+
|
531
|
+
good_msg = Message.objects.get(id=good_id)
|
532
|
+
|
533
|
+
is_good = Good.objects.filter(owner=request.user)\
|
534
|
+
|
535
|
+
.filter(message=good_msg).count()
|
536
|
+
|
537
|
+
|
538
|
+
|
539
|
+
if is_good > 0:
|
540
|
+
|
541
|
+
messages.success(request, '既にメッセージをGoodしています。')
|
542
|
+
|
543
|
+
return redirect(to='/sns')
|
544
|
+
|
545
|
+
|
546
|
+
|
547
|
+
good_msg.good_count += 1
|
548
|
+
|
549
|
+
good_msg.save()
|
550
|
+
|
551
|
+
good = Good()
|
552
|
+
|
553
|
+
good.owner = request.user
|
554
|
+
|
555
|
+
good.message = good_msg
|
556
|
+
|
557
|
+
good.save()
|
558
|
+
|
559
|
+
|
560
|
+
|
561
|
+
messages.success(request,'メッセージをGoodしました!')
|
562
|
+
|
563
|
+
return redirect(to='/sns')
|
564
|
+
|
565
|
+
|
566
|
+
|
253
567
|
|
254
568
|
|
255
569
|
|
1
views.pyを途中まで追記致しました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -2,128 +2,256 @@
|
|
2
2
|
|
3
3
|
![イメージ説明](fe4d3b9f01b8cc3a45965f9ad9ab742a.png)
|
4
4
|
|
5
|
+
|
6
|
+
|
7
|
+
|
8
|
+
|
9
|
+
|
10
|
+
|
5
11
|
```python
|
6
12
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
{% endblock %}
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
{% block content %}
|
46
|
-
|
47
|
-
<form action="{% url 'index' %}" method="post">
|
48
|
-
|
49
|
-
{% csrf_token %}
|
50
|
-
|
51
|
-
<input type="hidden" name="mode" value="__search_form__">
|
52
|
-
|
53
|
-
{{search_form}}
|
54
|
-
|
55
|
-
<button>Search</button>
|
56
|
-
|
57
|
-
</form>
|
58
|
-
|
59
|
-
<hr>
|
60
|
-
|
61
|
-
<form action="{% url 'index' %}" method="post">
|
62
|
-
|
63
|
-
{% csrf_token %}
|
64
|
-
|
65
|
-
<input type="hidden" name="mode" value="__check_form__">
|
66
|
-
|
67
|
-
{{check_form}}
|
68
|
-
|
69
|
-
<button>update</button>
|
70
|
-
|
71
|
-
</form>
|
72
|
-
|
73
|
-
{% for item in contens %}
|
74
|
-
|
75
|
-
<hr>
|
76
|
-
|
77
|
-
<p class="message_public">
|
78
|
-
|
79
|
-
{% if item.group.title == 'public' %}
|
80
|
-
|
81
|
-
[public]
|
82
|
-
|
83
|
-
{% endif %}</p>
|
84
|
-
|
85
|
-
<p class="message_content">{{item.content}}</p>
|
86
|
-
|
87
|
-
<p class="message_date">({{item.pub_date}})</p>
|
88
|
-
|
89
|
-
{% if item.share_id > 0 %}
|
90
|
-
|
91
|
-
<hr class="share_line">
|
92
|
-
|
93
|
-
<p class="share_item_content">{{item.get_share}}</p>
|
94
|
-
|
95
|
-
{% endif %}
|
96
|
-
|
97
|
-
<p class="message_owner">
|
98
|
-
|
99
|
-
<span class="message_counter"> share={{item.share_count}}\
|
100
|
-
|
101
|
-
good={{item.good_count}}</span>
|
102
|
-
|
103
|
-
"{{item.owner}}"(<a href="{% url 'add' %}?name={{item.owner}}">
|
104
|
-
|
105
|
-
add friend</a>)
|
106
|
-
|
107
|
-
<a href="{% url 'share' item.id %}"><button>share</button></a>
|
108
|
-
|
109
|
-
<a href="{% url 'good' item.id %}"><button>good!</button></a>
|
110
|
-
|
111
|
-
</p>
|
112
|
-
|
113
|
-
{% endfor %}
|
114
|
-
|
115
|
-
{% endblock %}
|
13
|
+
from django.shortcuts import render
|
14
|
+
|
15
|
+
from django.shortcuts import redirect
|
16
|
+
|
17
|
+
from django.contrib.auth.models import User
|
18
|
+
|
19
|
+
from django.contrib import messages
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
from .models import Message,Friend,Group,Good
|
24
|
+
|
25
|
+
from .forms import GroupCheckForm,GroupSelectForm, \
|
26
|
+
|
27
|
+
SearchForm,FriendsForm,CreateGroupForm,PostForm
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
from django.db.models import Q
|
32
|
+
|
33
|
+
from django.contrib.auth.decorators import login_required
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
#indexのビュー関数
|
38
|
+
|
39
|
+
@login_required(login_url='/admin/login/')
|
40
|
+
|
41
|
+
def index(request):
|
42
|
+
|
43
|
+
#publicのuserを取得
|
44
|
+
|
45
|
+
(public_user, public_group) = get_public()
|
116
46
|
|
117
47
|
|
118
48
|
|
49
|
+
#POST送信時の処理
|
50
|
+
|
51
|
+
if request.method == 'POST':
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
#Groupのチェックを更新した時の処理
|
56
|
+
|
57
|
+
if request.POST['mode'] == '__check_form__':
|
58
|
+
|
59
|
+
#フォームの用意
|
60
|
+
|
61
|
+
searchform = SearchForm()
|
62
|
+
|
63
|
+
checkform = GroupCheckForm(request.user, request.POST)
|
64
|
+
|
65
|
+
#チェックされたGroup名をリストにする
|
66
|
+
|
67
|
+
glist = []
|
68
|
+
|
69
|
+
for item in request.POST.getlist('groups'):
|
70
|
+
|
71
|
+
glist.append(item)
|
72
|
+
|
73
|
+
#Messageの取得
|
74
|
+
|
75
|
+
messages = get_your_group_message(request.user, \
|
76
|
+
|
77
|
+
glist, None)
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
if request.POST['mode'] == '__search_form__':
|
82
|
+
|
83
|
+
#フォームの用意
|
84
|
+
|
85
|
+
searchform = SearchForm(request.POST)
|
86
|
+
|
87
|
+
checkform = GroupCheckForm(request.user)
|
88
|
+
|
89
|
+
#Groupのリストを取得
|
90
|
+
|
91
|
+
gps = Group.objects.filter(owner=request.user)
|
92
|
+
|
93
|
+
glist = [public_group]
|
94
|
+
|
95
|
+
for item in gps:
|
96
|
+
|
97
|
+
glist.append(item)
|
98
|
+
|
99
|
+
#メッセージ取得
|
100
|
+
|
101
|
+
messages = get_your_group_message(request.user, glist, \
|
102
|
+
|
103
|
+
request.POST['search'])
|
104
|
+
|
105
|
+
#GETアクセス時の処理
|
106
|
+
|
107
|
+
else:
|
108
|
+
|
109
|
+
#フォームの用意
|
110
|
+
|
111
|
+
searchform = SearchForm()
|
112
|
+
|
113
|
+
checkform = GroupCheckForm(request.user)
|
114
|
+
|
115
|
+
#Groupのリストを取得
|
116
|
+
|
117
|
+
gps = Group.objects.filter(owner=request.user)
|
118
|
+
|
119
|
+
glist = [public_group]
|
120
|
+
|
121
|
+
for item in gps:
|
122
|
+
|
123
|
+
glist.append(item)
|
124
|
+
|
125
|
+
#メッセージの取得
|
126
|
+
|
127
|
+
messages = get_your_group_message(request.user, glist, None)
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
#共通処理
|
132
|
+
|
133
|
+
params = {
|
134
|
+
|
135
|
+
'login_user':request.user,
|
136
|
+
|
137
|
+
'contents':messages,
|
138
|
+
|
139
|
+
'check_form':checkform,
|
140
|
+
|
141
|
+
'search_form':searchform,
|
142
|
+
|
143
|
+
}
|
144
|
+
|
145
|
+
return render(request, 'sns/index.html',params)
|
146
|
+
|
119
147
|
|
120
148
|
|
149
|
+
@login_required(login_url='/admin/login/')
|
150
|
+
|
151
|
+
def groups(request):
|
152
|
+
|
153
|
+
#自分が登録したFriendを取得
|
154
|
+
|
155
|
+
friends = Friend.objects.filter(owner=request.user)
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
#POST送信時の処理
|
160
|
+
|
161
|
+
if request.method == 'POST':
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
#Groupメニュー選択肢の処理
|
166
|
+
|
167
|
+
if request.POST['mode'] == '__groups_form__':
|
168
|
+
|
169
|
+
#選択したGroupを取得
|
170
|
+
|
171
|
+
sel_group = request.POST['groups']
|
172
|
+
|
173
|
+
#Group取得
|
174
|
+
|
175
|
+
gp = Group.objects.filter(owner=request.user) \
|
176
|
+
|
177
|
+
.filter(title=sel_group).first()
|
178
|
+
|
179
|
+
#Groupに含まれるFriendを取得
|
180
|
+
|
181
|
+
fds = Friend.objects.filter(owner=request.user) \
|
182
|
+
|
183
|
+
.filter(group=gp)
|
184
|
+
|
185
|
+
#FriendのUserをリストにまとめる
|
186
|
+
|
187
|
+
vlist = []
|
188
|
+
|
189
|
+
for item in fds:
|
190
|
+
|
191
|
+
vlist.append(item.user.username)
|
192
|
+
|
193
|
+
#フォームの用意
|
194
|
+
|
195
|
+
groupsform = GroupSelectForm(request.user,request.POST)
|
196
|
+
|
197
|
+
friendsform = FriendsForm(request.user, \
|
198
|
+
|
199
|
+
friends=friends, vals=vlist)
|
200
|
+
|
201
|
+
#Friendsのチェック更新時の処理
|
202
|
+
|
203
|
+
if request.POST['mode'] == '__friends_form__':
|
204
|
+
|
205
|
+
#選択したGroupの取得
|
206
|
+
|
207
|
+
sel_group = request.POST['group']
|
208
|
+
|
209
|
+
group_obj = Group.objects.filter(title=sel_group).first()
|
210
|
+
|
211
|
+
#チェックしたFrindsを取得
|
212
|
+
|
213
|
+
sel_fds = request.POST.getlist('friends')
|
214
|
+
|
215
|
+
#FriendsのUserを取得
|
216
|
+
|
217
|
+
sel_users = User.objects.filter(username__in=sel_fds)
|
218
|
+
|
219
|
+
#Userのリストに含まれるユーザーが登録したFriendsを取得
|
220
|
+
|
221
|
+
fds = Friend.objects.filter(owner=request.user) \
|
222
|
+
|
223
|
+
.filter(user__in=sel_users)
|
224
|
+
|
225
|
+
#すべてのFriendにGroupを設定し保存する
|
226
|
+
|
227
|
+
vlist = []
|
228
|
+
|
229
|
+
for item in fds:
|
230
|
+
|
231
|
+
item.group = group_obj
|
232
|
+
|
233
|
+
item.save()
|
234
|
+
|
235
|
+
vlist.append(item.user.username)
|
236
|
+
|
237
|
+
#メッセージを設定
|
238
|
+
|
239
|
+
messages.success(request,'チェックされたFriendを' + \
|
240
|
+
|
241
|
+
sel_group + 'に登録しました。')
|
242
|
+
|
243
|
+
#フォームの用意
|
244
|
+
|
245
|
+
groupsform = GroupSelectForm(request.user, \
|
246
|
+
|
247
|
+
{'groups':sel_group})
|
248
|
+
|
249
|
+
friendsform = FriendsForm(request.user, \
|
250
|
+
|
251
|
+
friends=friends, vals=vlist)
|
252
|
+
|
253
|
+
|
254
|
+
|
255
|
+
|
256
|
+
|
121
257
|
```
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
過去の質問で似ているものをみつけたのですが、よくわからず。
|
126
|
-
|
127
|
-
よろしくお願いします。
|
128
|
-
|
129
|
-
![イメージ説明](da144bd9445e69a168fe07c9060eed8d.png)
|