Skip to content

Commit fca10ca

Browse files
author
Raunak Ritesh
committed
For Key added(CommentsTable) + Patches
1 parent 3b856d5 commit fca10ca

17 files changed

+97
-10
lines changed

Notes/admin.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ class CommentModelAdmin(admin.ModelAdmin):
2929

3030
# Show Filters on the side bar and list only those Tasks which are referenced by the Foreign Key in Comments Table.
3131
list_filter = (
32-
('taskId_id', admin.RelatedOnlyFieldListFilter),
32+
('taskId', admin.RelatedOnlyFieldListFilter),
33+
('createdBy', admin.RelatedOnlyFieldListFilter),
3334
)
3435

3536
class Meta:

Notes/admin.pyc

19 Bytes
Binary file not shown.

Notes/migrations/0007_user.pyc

0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.

Notes/migrations/0011_comment_user.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# -*- coding: utf-8 -*-
2+
from __future__ import unicode_literals
3+
4+
from django.db import models, migrations
5+
6+
7+
class Migration(migrations.Migration):
8+
9+
dependencies = [
10+
('Notes', '0010_auto_20180718_1835'),
11+
]
12+
13+
operations = [
14+
migrations.AddField(
15+
model_name='comment',
16+
name='user',
17+
field=models.ForeignKey(to='Notes.User', null=True),
18+
),
19+
]
903 Bytes
Binary file not shown.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# -*- coding: utf-8 -*-
2+
from __future__ import unicode_literals
3+
4+
from django.db import models, migrations
5+
6+
7+
class Migration(migrations.Migration):
8+
9+
dependencies = [
10+
('Notes', '0011_comment_user'),
11+
]
12+
13+
operations = [
14+
migrations.RemoveField(
15+
model_name='comment',
16+
name='createdBy',
17+
),
18+
migrations.RenameField(
19+
model_name='comment',
20+
old_name='user',
21+
new_name='createdBy',
22+
),
23+
]
913 Bytes
Binary file not shown.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# -*- coding: utf-8 -*-
2+
from __future__ import unicode_literals
3+
4+
from django.db import models, migrations
5+
6+
7+
class Migration(migrations.Migration):
8+
9+
dependencies = [
10+
('Notes', '0012_remove_comment_createdby'),
11+
]
12+
13+
operations = [
14+
migrations.AlterField(
15+
model_name='comment',
16+
name='createdBy',
17+
field=models.ForeignKey(to='Notes.User'),
18+
),
19+
migrations.AlterField(
20+
model_name='task',
21+
name='createdBy',
22+
field=models.ForeignKey(to='Notes.User'),
23+
),
24+
]
966 Bytes
Binary file not shown.

Notes/models.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class Task(models.Model):
3939
# createdBy = models.CharField(max_length=10, blank=False)
4040
dueDate = models.DateField(null=False)
4141
isDeleted = models.BooleanField(default=False)
42-
createdBy = models.ForeignKey(User, null=True)
42+
createdBy = models.ForeignKey(User)
4343

4444
def __str__(self):
4545
return '%s ' % (self.title)
@@ -63,8 +63,9 @@ class Comment(models.Model):
6363
taskId = models.ForeignKey(Task, null=False, blank=False) #UNFORTUNATELY RENAMED TO taskId_id
6464
createdOn = models.DateTimeField(auto_now_add=True)
6565
UpdatedOn = models.DateTimeField(auto_now_add=True)
66-
createdBy = models.CharField(max_length=10, blank=False) #CANNOT be blank.
66+
# createdBy = models.CharField(max_length=10, blank=False) #CANNOT be blank.
6767
commentText = models.CharField(max_length=100, blank=False)
68+
createdBy = models.ForeignKey(User)
6869

6970
def __str__(self):
7071
return '%s' % (self.commentText)

Notes/models.pyc

-15 Bytes
Binary file not shown.

Notes/views.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
# Imports from models.py of this app
1010
from models import Task
1111
from models import Comment
12+
from models import User
1213

1314
# Create your views here.
1415
class Pages(View):
@@ -93,9 +94,12 @@ def post(self, request): # - Create a task (POST /tasks/
9394
if t == '' or l == '' or cby == '' or due == '':
9495
return HttpResponse('title, label, author, due Date are mandatory Fields.', status=200)
9596

96-
entry = Task(title = t, description = d, label = l, color = col, comments = co, createdBy = cby, dueDate = due)
97+
user = User.objects.get(username = cby) #Get User Instance from given name
98+
entry = Task(title = t, description = d, label = l, color = col, comments = co, createdBy = user, dueDate = due)
9799
entry.save()
98100
return HttpResponse('Success', status=200)
101+
except User.DoesNotExist, e:
102+
return HttpResponse('User does not exists in the User Table', status=200)
99103
except Exception, e:
100104
error = "Please provide all the details:- 'title', 'desc',\
101105
'label', 'color', 'comments', 'author', 'due' where\
@@ -194,15 +198,21 @@ def post(self, request): # Post a new Comment
194198
tid = c['tid']
195199
cby = c['author']
196200
ctext = c['text']
197-
201+
198202
if ctext == '' or tid == '' or cby == '':
199203
return HttpResponse('Comment, TaskId, Author are mandatory Fields.')
200204

201-
entry = Comment(taskId_id = tid, createdBy = cby, commentText = ctext)
205+
user = User.objects.get(username = cby) #Get User Instance from given name
206+
207+
# It should be Noted that taskId_id is set by the exact id as defined in the database
208+
# but createdBy attribute is set by an instance of the User.
209+
entry = Comment(taskId_id = tid, createdBy = user, commentText = ctext)
202210
entry.save()
203211
return HttpResponse('Success', status=200)
212+
except User.DoesNotExist as e:
213+
return HttpResponse('User does not exists in the User Table', status=200)
204214
except Exception as e:
205-
return HttpResponse('Please Provide a Task Id which exists in the Database. text, tid, author are mandatory Fields.', status=200)
215+
return HttpResponse('Please Provide a Task Id, and Author which exists in the Database. text, tid, author are mandatory Fields.', status=200)
206216

207217
def put(self, request): # Edit a comment
208218
# http://127.0.0.1:8000/Notes/comments/?id=1

Notes/views.pyc

329 Bytes
Binary file not shown.

README.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,16 @@ INDEX PAGE: > http://127.0.0.1:8000/Notes/index/
7272
```
7373

7474
## DATABASE EXPLAINED
75-
75+
### User Table
76+
Username and Active Status can be changed later.
77+
```
78+
userId = models.AutoField(primary_key=True)
79+
username = models.CharField(max_length=20, blank=False)
80+
createdOn = models.DateTimeField(auto_now_add=True)
81+
isActive = models.BooleanField(default=True)
82+
```
7683
### Task Table
84+
Has a Foriegn Key Field to User Table.
7785
* FIELDS THAT CANNOT BE BLANK = Title. Label. CreatedBy. DueDate
7886
* CAN BE BLANK = Description. Color. Comments. Attachment
7987
```
@@ -96,19 +104,20 @@ INDEX PAGE: > http://127.0.0.1:8000/Notes/index/
96104
color = CharField(max_length=7, choices=COLOURS, blank=True) #STORE A HEX FIELD.
97105
attachment = FileField(upload_to='attachments', blank=True)
98106
comments = CharField(max_length=255, blank=True)
99-
createdBy = CharField(max_length=10, blank=False)
107+
createdBy = models.ForeignKey(User, null=True)
100108
dueDate = DateField(null=False)
101109
```
102110

103111
### Comments Table
112+
Has 2 Foreign Key Fields - To Comments Table and User Table.
104113
* FIELDS THAT CANNOT BE BLANK = createdBy, CommentText.
105114
* Rest all are auto inserted.
106115
```
107116
commentId = AutoField(primary_key=True)
108117
taskId = ForeignKey(Task, null=False, blank=False)
109118
createdOn = DateTimeField(auto_now_add=True)
110119
UpdatedOn = DateTimeField(auto_now_add=True)
111-
createdBy = CharField(max_length=10, blank=False)
120+
createdBy = models.ForeignKey(User)
112121
commentText = CharField(max_length=100, blank=False)
113122
```
114123
## Acknowledgments

0 commit comments

Comments
 (0)