-
Notifications
You must be signed in to change notification settings - Fork 0
/
PostsAPI.py
270 lines (225 loc) · 9.13 KB
/
PostsAPI.py
1
2
3
4
5
6
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
__author__ = 'rohit'
import endpoints
from protorpc import messages
from protorpc import message_types
from protorpc import remote
from google.appengine.api import memcache
from google.appengine.api import taskqueue
from google.appengine.ext import ndb
from Models import Post_Request,CollegeDb,Post,Profile,Club
from Models import PostForm,LikePost,CommentsForm,Comments,GetPostRequestsForm
def postRequest(requestentity=None):
post_request = Post_Request()
#college = CollegeDb(name = 'NITK',student_sup='Anirudh',collegeId='NITK-123')
#college_key = college.put()
query = CollegeDb.query()
key1 = ndb.Key('Club',int(requestentity.club_id))
key2 = ndb.Key('Profile',int(requestentity.from_pid))
club_key = key1
print "Club key"
print key1
profile_key = key2
#change the ID portion when merging with front end
#setattr(clubRequest, field, profile_key)
if requestentity:
for field in ('to_pid','club_id','description','status','post_request_id','collegeId','title','from_pid','likers'):
if hasattr(requestentity, field):
print(field,"is there")
val = getattr(requestentity, field)
if(field=="club_id"):
setattr(post_request, field, club_key)
elif(field=="from_pid"):
setattr(post_request, field, profile_key)
elif val:
print("Value is",val)
setattr(post_request, field, str(val))
elif field == "to_pid":
query = club_key.get()
print query
admin_id = query.admin
person = admin_id.get()
print "Person's email-id ", person.email
person_collegeId = person.collegeId
print "His college Id ", person.collegeId
college_details = person_collegeId.get()
print "The sup is ", college_details.student_sup
print("Finished to-pid")
setattr(post_request, field,admin_id)
elif field == "status":
setattr(post_request, field, "Yes")
elif field == "post_request_id":
setattr(post_request, field, "ABCD123")
elif field == "collegeId":
setattr(post_request, field, person_collegeId)
print("About to createClubRequest")
print(post_request)
post_request.put()
return post_request
def postEntry(requestentity=None,check=0):
newPost = Post()
#college = CollegeDb(name = 'NITK',student_sup='Anirudh',collegeId='NITK-123')
#college_key = college.put()
query = CollegeDb.query()
club_name = Club.query()
if check==0:
print "The request entity key is " + requestentity.club_id
key1 = ndb.Key('Club',int(requestentity.club_id))
key2 = ndb.Key('Profile',int(requestentity.from_pid))
else:
key1 = requestentity.club_id
key2 = requestentity.from_pid
persons = Profile.query()
#print club_name[0]
#print "The key is " + club_name[0].key
club_key = key1
profile_key = key2
flag = 0
print "Profile Key " + str(profile_key)
for x in persons:
print x.key
if(x.key == profile_key):
print "Same"
flag=1
else:
print "NOPE"
#setattr(clubRequest, field, profile_key)
if(flag==1):
if requestentity:
for field in ('title','description','club_id','from_pid','likes','views'):
if hasattr(requestentity, field):
print(field,"is there")
val = getattr(requestentity, field)
if(field=="club_id"):
print "Club_Id stage"
setattr(newPost, field, club_key)
elif field == "from_pid":
print "Entered here"
person = profile_key.get()
print "Person's email-id ", person.email
person_collegeId = person.collegeId
print "His college Id ", person.collegeId
college_details = person_collegeId.get()
print "The sup is ", college_details.student_sup
setattr(newPost, field, profile_key)
print "Put PID"
setattr(newPost,'collegeId',person_collegeId)
print "Put college id"
elif val:
print("Value is",val)
setattr(newPost, field, str(val))
else:
setattr(newPost, "likes", 0)
setattr(newPost, "views", 0)
print("About to create Post")
print(newPost)
newPost.put()
else:
print "Invalid Person"
return
def deletePost(request):
post_id = ndb.Key('Post',int(request.postId))
from_pid = ndb.Key('Profile',int(request.from_pid))
post = post_id.get()
club_admin = post.club_id.get().admin
flag=0
if (post.from_pid==from_pid or club_admin==from_pid):
print "Same"
flag=1
else:
print "Different"
if flag==1:
post_id.delete()
return
def unlikePost(request):
post_id = ndb.Key('Post',int(request.postId))
from_pid = ndb.Key('Profile',int(request.from_pid))
post = post_id.get()
pylist = post.likers
if (from_pid in pylist):
post.likes = post.likes -1
pylist.remove(from_pid)
post.likers = pylist
post.put()
print pylist
else:
print "Nope"
return
def copyPostToForm(post):
pf = PostForm()
for field in pf.all_fields():
if hasattr(post, field.name):
setattr(pf, field.name, str(getattr(post, field.name)))
if field.name == 'postId':
setattr(pf, field.name, str(post.key.id()))
return pf
def likePost(request):
lp = LikePost()
post_id = ndb.Key('Post',int(request.postId))
from_pid = ndb.Key('Profile',int(request.from_pid))
post = post_id.get()
pylist = post.likers
if(from_pid not in pylist):
post.likes = post.likes+1
post.likers.append(from_pid)
post.put()
else:
print "Sorry Already liked"
return message_types.VoidMessage
def editpost(request):
temp = getattr(request,'postId') #or do request.postId
key = ndb.Key('Post',int(temp))
updatedPost = key.get()
updatedPost.title = request.title
updatedPost.description = request.description
updatedPost.put() #Include photo when it comes.
return
def commentForm(request):
cf = CommentsForm()
comment = Comments()
for field in cf.all_fields():
if field.name=='pid':
print "Entered PID portion"
key = ndb.Key('Profile',int(getattr(request,field.name)))
print key
setattr(comment,field.name,key)
person = key.get()
print "Second key"
key2 = person.collegeId
print key2
setattr(comment,'collegeId',key2)
elif field.name=='postId':
key = ndb.Key('Post',int(getattr(request,field.name)))
setattr(comment,field.name,key)
else:
setattr(comment,field.name,str(getattr(request,field.name)))
setattr(comment,'likes',0)
comment.put()
return message_types.VoidMessage()
def copyPostRequestToForm(request):
reply = GetPostRequestsForm()
for field in reply.all_fields():
print field
if(hasattr(request,field.name)):
setattr(reply,field.name,str(getattr(request,field.name)))
elif(field.name=="person_from"):
person = request.from_pid.get().name
setattr(reply,field.name,person)
elif(field.name=="club_name"):
club = request.club_id.get().name
setattr(reply,field.name,club)
else:
setattr(reply,"postRequestId",str(request.key.id()))
return reply
def update(request):
requestId = ndb.Key('Post_Request',int(request.postRequestId))
action = request.action
if(action.lower()=="accept"):
post = requestId.get()
flag=1
print "Going to post ENtry"
postEntry(post,flag)
requestId.delete()
elif(action.lower()=="reject"):
print "Oops"
requestId.delete()
return