def delete_post(self, request): token = TokenMod() user = token.tokenAuth(request) if str(type(user)) == "<class 'tuple'>": return user[0], user[1] # 여기부터 글 삭제 post_id = request.query_params.get("post_id") if post_id is None: return { 'error_code': 0, 'error_msg': "Missing parameters" }, status.HTTP_400_BAD_REQUEST try: post_obj = Posts.objects.get(id=post_id) if post_obj.owner == user.id: # 삭제 post_obj.delete() return {'message': "success"}, status.HTTP_200_OK else: return { 'error_code': 2, 'error_msg': 'post is not yours' }, status.HTTP_400_BAD_REQUEST except ValueError: return { 'error_code': 1, 'error_msg': "Post does not exist" }, status.HTTP_400_BAD_REQUEST except Posts.DoesNotExist: return { 'error_code': 1, 'error_msg': "Post does not exist" }, status.HTTP_400_BAD_REQUEST
def write_comment(self, request): token = TokenMod() user = token.tokenAuth(request) if str(type(user)) == "<class 'tuple'>": return user[0], user[1] try: post = Posts.objects.get(id=request.data.get("post_id")) except Posts.DoesNotExist: return { 'error_code': 1, 'error_msg': "Post does not exist" }, status.HTTP_400_BAD_REQUEST if request.data.get("post_id") and request.data.get("description"): comm = Comments(post_id=request.data.get("post_id"), owner=user.id, description=request.data.get("description")) comm.save() else: return { 'error_code': 0, 'error_msg': "Missing parameters" }, status.HTTP_400_BAD_REQUEST self.put_push(post.owner, user.username + "님이 댓글을 달았습니다.", post.id) return {'message': "success"}, status.HTTP_200_OK
def list(self, request): token = TokenMod() user = token.tokenAuth(request) if str(type(user)) == "<class 'tuple'>": return Response(user[0], user[1]) username = request.query_params.get("username") if not username: return Response({'error_code': 0, 'error_msg': "Missing parameters"}, status=status.HTTP_400_BAD_REQUEST) search = authUser.objects.filter(username__contains=username) if not search: return Response({'error_code': 1, 'error_msg': 'This user does not exist'}, status=status.HTTP_404_NOT_FOUND) re_dict = [] for x in search: re_dict.append({ 'id': x.id, 'username': x.username, 'name': x.last_name, 'profile_img': self.snsmod.profile_img(x.id), }) return Response(re_dict, status=status.HTTP_200_OK)
def post(self, request): ''' Sample의 list를 불러오는 API postpost --- # 내용 - username : 회원 아이디 - password : 회원 패스워드 ''' token = TokenMod() tokenResult = token.createToken(request) return Response(tokenResult[0], status=tokenResult[1])
def list(self, request): token = TokenMod() user = token.tokenAuth(request) m = request.query_params.get("user_id") try: search_user_id = user.id if not m and user else m is_following_id = user.id if user else False except AttributeError: search_user_id = m is_following_id = False re_dict = self.snsmod.follow_list(search_user_id, is_following_id=is_following_id) return Response(re_dict, status.HTTP_200_OK)
def delete(self, request): token = TokenMod() user = token.tokenAuth(request) if str(type(user)) == "<class 'tuple'>": return Response(user[0], user[1]) post_id = request.query_params.get("post_id") if not post_id: return {'error_code': 0, 'error_msg': "Missing parameters"}, status.HTTP_400_BAD_REQUEST post = Posts(id=post_id) if not post: return {'error_code': 1, 'error_msg': "Post does not exist"}, status.HTTP_400_BAD_REQUEST self.snsmod.unlike_post(post_id, user.id) return Response({'message': "success"}, status.HTTP_200_OK)
def post(self, request): token = TokenMod() user = token.tokenAuth(request) if str(type(user)) == "<class 'tuple'>": return Response(user[0], user[1]) following = request.data.get("user_id") if not following: return Response({'error_code': 0, 'error_msg': "Missing parameters"}, status.HTTP_400_BAD_REQUEST) try: following_id = authUser.objects.get(id=following) f = Followers.objects.get_or_create(follower=user.id, following=following) return Response({'message': "success"}, status.HTTP_200_OK) except authUser.DoesNotExist: return Response({'error_code': 1, 'error_msg': "Following target is invalid"}, status.HTTP_400_BAD_REQUEST)
def list(self, request): # 토큰 인증 token = TokenMod() user = token.tokenAuth(request) user_id = 0 if str(type(user)) != "<class 'tuple'>": user_id = user.id post_id = request.query_params.get("post_id") if post_id is None: return Response({'error_code': 0, 'error_msg': "Missing parameters"}, status=status.HTTP_400_BAD_REQUEST) try: return_dict = self.snsmod.get_post(post_id, is_like_user=user_id) return Response(return_dict, status=status.HTTP_200_OK) except ValueError: return Response({'error_code': 1, 'error_msg': "Post does not exist"}, status=status.HTTP_400_BAD_REQUEST)
def post(self, request): token = TokenMod() user = token.tokenAuth(request) if str(type(user)) == "<class 'tuple'>": return Response(user[0], user[1]) user_id = user.id image = request.data.get('image') from django.core.files.storage import default_storage from django.core.files.base import ContentFile allow_type = ["image/png", "image/jpeg", "image/gif"] from PIL import Image if image.content_type in allow_type: import hashlib og_filename = str(image).split(".") types = og_filename[len(og_filename) - 1] hash_filename = hashlib.md5(str(image).encode("utf-8")).hexdigest() + "." + types path = default_storage.save(os.getcwd() + "/images/" + hash_filename, ContentFile(image.read())) url = path.replace(os.getcwd(), "") im = Image.open("." + url) x, y = im.size if x > y: new_size = x x_offset = 0 y_offset = int((x - y) / 2) elif x < y: new_size = y x_offset = int((y - x) / 2) y_offset = 0 if x != y: new_image = Image.new("RGB", (new_size, new_size), "white") new_image.paste(im, (x_offset, y_offset)) new_image.save("." + url) profile = Profile(user_id=user_id, profile_img=url) profile.save() return Response({"msg": "success"}, status.HTTP_200_OK) else: return Response({'error_code': 1, 'error_msg': 'Upload file format is incorrect', "error_file": str(image)}, \ status.HTTP_400_BAD_REQUEST)
def delete(self, request): token = TokenMod() user = token.tokenAuth(request) if str(type(user)) == "<class 'tuple'>": return Response(user[0], user[1]) unfollowing = request.query_params.get("user_id") if not unfollowing: return Response({'error_code': 0, 'error_msg': "Missing parameters"}, status.HTTP_400_BAD_REQUEST) try: f = Followers.objects.get(follower=user.id, following=unfollowing) f.delete() return Response({'message': "success"}, status.HTTP_200_OK) except authUser.DoesNotExist: return Response({'error_code': 1, 'error_msg': "Unfollowing target is invalid"}, status.HTTP_400_BAD_REQUEST) except Followers.DoesNotExist: return Response({'error_code': 2, 'error_msg': "Not Followed target"}, status.HTTP_400_BAD_REQUEST)
def list(self, request): # 토큰 인증 token = TokenMod() user = token.tokenAuth(request) user_id = 0 if str(type(user)) != "<class 'tuple'>": user_id = user.id post_id = int(request.query_params.get("post_id")) index = int(request.query_params.get("index")) if post_id is None: return Response({'error_code': 0, 'error_msg': "Missing parameters"}, status=status.HTTP_400_BAD_REQUEST) try: from django.contrib.gis.measure import Distance post = PostInfo.objects.filter(post_id=post_id) return Response({'msg': 1}, status=status.HTTP_200_OK) except ValueError: return Response({'error_code': 1, 'error_msg': "Post does not exist"}, status=status.HTTP_400_BAD_REQUEST)
def list(self, request): login_user = 0 token = TokenMod() user = token.tokenAuth(request) if str(type(user)) != "<class 'tuple'>": login_user = user.id user_id = request.query_params.get("user_id") start = request.query_params.get("start") count = request.query_params.get("count") if not start: start = 0 if not count: count = 9 start = int(start) count = int(count) next_start = int(start) + 9 next_count = int(count) if user_id is not None: timeline = self.snsmod.get_userTimeline(user_id, start, count, login_user=login_user) timelineCount = self.snsmod.get_timelineCount(user_id) if timelineCount - next_start < next_count: next_count = timelineCount - next_start if timelineCount > next_start: next_url = SERVER_URL + "/timeline/?user_id=" + str(user_id) + "&start=" + str(next_start) + "&count=" + str(next_count) else: next_url = False else: # 실제 타임라인 가져오기 token = TokenMod() user = token.tokenAuth(request) if str(type(user)) == "<class 'tuple'>": return Response(user[0], user[1]) timelineCount = self.snsmod.get_ftimelineCount(user.id) if timelineCount - next_start < next_count: next_count = timelineCount - next_start if timelineCount > next_start: next_url = SERVER_URL + "/timeline/?start=" + str(next_start) + "&count=" + str(next_count) else: next_url = False timeline = self.snsmod.get_followingTimeline(user.id, start, count, login_user=login_user) re_dict = { "count": timelineCount, "next": next_url, "results": timeline, } return Response(re_dict, status.HTTP_200_OK)
class Auth(viewsets.ViewSet): """ 로그인 인증 --- # 내용 - username : 회원 아이디 - password : 회원 패스워드 """ queryset = authUser.objects.all() serializer_class = UserSerializer token = TokenMod() user = token snsmod = SNS() @authentication_classes((TokenAuthentication,)) @permission_classes((IsAuthenticated,)) def list(self, request): user = self.token.tokenAuth(request) if str(type(user)) == "<class 'tuple'>": return Response(user[0], user[1]) following = Followers.objects.filter(follower=user.id) follower = Followers.objects.filter(following=user.id) return Response({ 'id': user.id, 'username': user.username, 'name': user.last_name, 'email': user.email, 'is_superuser': user.is_superuser, 'is_active': user.is_active, 'profile_img': self.snsmod.profile_img(user_id=user.id), 'follower': len(follower), 'following': len(following), }, status=status.HTTP_200_OK) def post(self, request): ''' Sample의 list를 불러오는 API postpost --- # 내용 - username : 회원 아이디 - password : 회원 패스워드 ''' token = TokenMod() tokenResult = token.createToken(request) return Response(tokenResult[0], status=tokenResult[1])
def write_post(self, request): """ :param request: body data in HTTP request :return: jsondata, HTTP STATUS """ # 토큰 인증 token = TokenMod() user = token.tokenAuth(request) if str(type(user)) == "<class 'tuple'>": return user[0], user[1] # 필수 파라미터 검사 for x in ["lx", "ly", "image", "content"]: if not request.data.get(x): return { 'error_code': 0, 'error_msg': "Missing parameters" }, status.HTTP_400_BAD_REQUEST # 이미지 업로드 처리 images = request.data.getlist('image') from django.core.files.storage import default_storage from django.core.files.base import ContentFile from urllib import parse allow_type = ["image/png", "image/jpeg", "image/gif"] uploaded_images = [] from PIL import ImageOps, Image for file in images: if file.content_type in allow_type: import hashlib og_filename = str(file).split(".") types = og_filename[len(og_filename) - 1] hash_filename = hashlib.md5( str(file).encode("utf-8")).hexdigest() + "." + types path = default_storage.save( os.getcwd() + "/images/" + hash_filename, ContentFile(file.read())) url = path.replace(os.getcwd(), "") im = Image.open("." + url) x, y = im.size if x > y: new_size = x x_offset = 0 y_offset = int((x - y) / 2) elif x < y: new_size = y x_offset = int((y - x) / 2) y_offset = 0 if x != y: new_image = Image.new("RGB", (new_size, new_size), "white") new_image.paste(im, (x_offset, y_offset)) new_image.save("." + url) uploaded_images.append(url) else: return {'error_code': 1, 'error_msg': 'Upload file format is incorrect', "error_file": str(file)}, \ status.HTTP_400_BAD_REQUEST lX = request.data.getlist('lx') lY = request.data.getlist('ly') mi = request.data.getlist('map_info') contxt = request.data.get("content") # 글쓰기 new_post = Posts.objects.create(owner=user.id, description=contxt) # 파일 및 지도 기록 for i in range(len(lX)): PostInfo.objects.create(post_id=new_post.id, lx=lX[i], ly=lY[i], map_info=mi[i], img=uploaded_images[i]) return {'message': 'success'}, status.HTTP_200_OK
class UserViewSet(viewsets.ViewSet): usermod = UserMod() snsmod = SNS() token = TokenMod() def list(self, request): user_id = request.query_params.get("user_id") username = request.query_params.get("username") if not user_id: u = authUser.objects.get(username=username) user_id = u.id try: user = self.usermod.user_profile(user_id) except authUser.DoesNotExist: return Response({'error_code': 1, 'error_msg': 'This user does not exist'}) following = Followers.objects.filter(follower=user_id) follower = Followers.objects.filter(following=user_id) return Response({ 'id': user.id, 'username': user.username, 'name': user.last_name, 'email': user.email, 'is_superuser': user.is_superuser, 'is_active': user.is_active, 'profile_img': self.snsmod.profile_img(user.id), 'follower': len(follower), 'following': len(following), }, status=status.HTTP_200_OK) # 회원정보 수정 @authentication_classes((TokenAuthentication,)) @permission_classes((IsAuthenticated,)) def post(self, request): token = TokenMod() user = token.tokenAuth(request) if str(type(user)) == "<class 'tuple'>": return Response(user[0], user[1]) user_id = user.id image = request.data.get('image') from django.core.files.storage import default_storage from django.core.files.base import ContentFile allow_type = ["image/png", "image/jpeg", "image/gif"] from PIL import Image if image.content_type in allow_type: import hashlib og_filename = str(image).split(".") types = og_filename[len(og_filename) - 1] hash_filename = hashlib.md5(str(image).encode("utf-8")).hexdigest() + "." + types path = default_storage.save(os.getcwd() + "/images/" + hash_filename, ContentFile(image.read())) url = path.replace(os.getcwd(), "") im = Image.open("." + url) x, y = im.size if x > y: new_size = x x_offset = 0 y_offset = int((x - y) / 2) elif x < y: new_size = y x_offset = int((y - x) / 2) y_offset = 0 if x != y: new_image = Image.new("RGB", (new_size, new_size), "white") new_image.paste(im, (x_offset, y_offset)) new_image.save("." + url) profile = Profile(user_id=user_id, profile_img=url) profile.save() return Response({"msg": "success"}, status.HTTP_200_OK) else: return Response({'error_code': 1, 'error_msg': 'Upload file format is incorrect', "error_file": str(image)}, \ status.HTTP_400_BAD_REQUEST) # 회원가입 def put(self, request): try: response_msg = self.usermod.add(username=request.data['username'], password=request.data['password'], name=request.data['name'], mail=request.data['mail']) except KeyError: return Response({'error_code': 0, 'error_msg': 'Missing parameters'}, status=status.HTTP_400_BAD_REQUEST) return Response(response_msg[0], status=response_msg[1]) def delete(self, request): # 탈퇴 따위 불가능 return Response({'message': 'get'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)