def destroy(self, request, *args, **kwargs): try: obj = json.loads(request.body) token = obj.get('token') user = get_user_by_token(token) if user: event = self.get_object() if event: try: response = super(EventViewSet, self).destroy(request, *args, **kwargs) if response.status_code != status.HTTP_204_NO_CONTENT: event.id = -1 else: delete_event_file(event) except Exception as e: event.id = -1 save_error_log(request, e) event_json = EventSerializer(event).data return json_response(event_json, CODE_SUCCESS, MSG_DELETE_EVENT_SUCCESS) else: return simple_json_response(CODE_NO_CONTENT, MSG_204) else: return invalid_token_response() except Exception as e: return save_error_log(request, e)
def create(self, request, *args, **kwargs): try: token = request.data.get('token') text = request.data.get('text') event_id = request.data.get('event_id') user_id = request.data.get('user_id') source_comment_id = request.data.get('source_comment_id', -1) user = get_user_by_token(token) if not text or not event_id or not user_id: return simple_json_response(CODE_EMPTY_COMMENT, MSG_EMPTY_COMMENT_FIELD) if user: comment = Comment() comment.text = text comment.event = Event.objects.get(id=event_id) comment.baby = BabyUser.objects.get(id=user_id) if source_comment_id > 0 and Comment.objects.filter(id=source_comment_id): comment.source_comment = Comment.objects.get(id=source_comment_id) comment.save() response = CommentSerializer(comment).data return json_response(response, CODE_SUCCESS, MSG_POST_COMMENT_SUCCESS) else: return invalid_token_response() except Exception as e: return save_error_log(request, e)
def update(self, request, *args, **kwargs): token = request.data.get('token') baby_name = request.data.get('baby_name') phone = request.data.get('phone') email = request.data.get('email') gender = request.data.get('gender') birthday = request.data.get('birthday') hobbies = request.data.get('hobbies') base64 = request.data.get('base64') try: user = get_user_by_token(token) if user: if BabyUser.objects.filter(user=user): baby = BabyUser.objects.get(user=user) if email: if User.objects.filter( username__iexact=email) or User.objects.filter( email__iexact=email): return simple_json_response( CODE_DUPLICATE_USER, MSG_DUPLICATE_EMAIL) else: user.email = email user.save() if baby_name: baby.baby_name = baby_name if phone: if BabyUser.objects.filter(phone=phone): return simple_json_response( CODE_DUPLICATE_PHONE, MSG_DUPLICATE_PHONE) else: baby.phone = phone if gender: baby.gender = gender if birthday: baby.birth = birthday if hobbies: baby.hobbies = hobbies if base64: image_name = user.username + time.strftime( '%Y%m%d%H%M%S') + PROFILE_FOOTER_IMAGE profile = upload_file_to_oss( user.username + DIR_USER_PROFILE + image_name, base64) baby.profile = profile baby.save() response_data = BabyUserSerializer(baby).data response_data['token'] = Token.objects.get(user=user).key return json_response(response_data, CODE_SUCCESS, MSG_UPDATE_USER_INFO_SUCCESS) return invalid_token_response() except Exception as e: return save_error_log(request, e)
def create(self, request, *args, **kwargs): try: token = request.data.get('token') title = request.data.get('title') content = request.data.get('content') base64s = request.data.get('base64s') type = request.data.get('type', 0) user = get_user_by_token(token) if not title and not content and not base64s: return simple_json_response(CODE_EMPTY_EVENT, MSG_EMPTY_EVENT) if user: event = Event() event.baby = BabyUser.objects.get(user=user) event.created = timezone.now() event.type = type if title: event.title = title if content: event.content = content if base64s: # only one image for now if type == TYPE_IMAGE: for image in base64s: image_name = user.username + time.strftime( '%Y%m%d%H%M%S') + EVENT_FOOTER_IMAGE image = upload_file_to_oss( user.username + DIR_EVENT_IMAGE + image_name, image, type) event.image1 = image elif type == TYPE_VIDEO: for video in base64s: video_name = user.username + time.strftime( '%Y%m%d%H%M%S') + EVENT_FOOTER_VIDEO video = upload_file_to_oss( user.username + DIR_EVENT_VIDEO + video_name, video, type) event.video_url = video event.video_width = request.data.get('video_width') event.video_height = request.data.get( 'video_height') image_name = user.username + time.strftime( '%Y%m%d%H%M%S') + EVENT_FOOTER_VIDEO_THUMBNAIL image = upload_file_to_oss( user.username + DIR_EVENT_VIDEO + image_name, request.data.get('video_thumbnail'), TYPE_IMAGE) event.video_thumbnail = image event.save() response = EventSerializer(event).data return json_response(response, CODE_SUCCESS, MSG_POST_EVENT_SUCCESS) else: return invalid_token_response() except Exception as e: return save_error_log(request, e)
def list(self, request, *args, **kwargs): try: token = request.query_params.get('token') user = get_user_by_token(token) if user: response = super(CommentViewSet, self).list(request, *args, **kwargs).data return json_response(response, CODE_SUCCESS, MSG_GET_COMMENTS_SUCCESS) else: return invalid_token_response() except Exception as e: return save_error_log(request, e)
def list(self, request, *args, **kwargs): try: serializer = self.get_serializer(data=request.data) serializer.is_valid() token = request.data.get('token') user = get_user_by_token(token) if user: return json_response( super(UserViewSet, self).list(request, *args, **kwargs).data, CODE_SUCCESS, MSG_GET_USERS_SUCCESS) else: return invalid_token_response() except Exception as e: return save_error_log(request, e)
def get_app_info(request): try: token = request.data.get('token') user = get_user_by_token(token) if user: response_data = dict() if AppInfo.objects.all(): app_info = AppInfo.objects.all().order_by('-id')[0] response_data = AppInfoSerializer(app_info).data response_data['app_name'] = app_info.app_file.file.name.split( "/")[-1].replace('.apk', '') return json_response(response_data, CODE_SUCCESS, MSG_GET_APP_INFO_SUCCESS) else: return invalid_token_response() except Exception as e: return save_error_log(request, e)
def retrieve(self, request, *args, **kwargs): token = request.query_params.get('token') try: user = get_user_by_token(token) if user: response = super(UserViewSet, self).retrieve(request, *args, **kwargs).data baby = self.get_object() events = Event.objects.filter(baby=baby) event_list = list() for event in events: event_json = EventSerializer(event).data event_list.append(event_json) response['events'] = event_list return json_response(response, CODE_SUCCESS, MSG_GET_USER_DETAIL_SUCCESS) else: return invalid_token_response() except Exception as e: return save_error_log(request, e)
def list(self, request, *args, **kwargs): try: token = request.query_params.get('token') user = get_user_by_token(token) if user: response = super(EventViewSet, self).list(request, *args, **kwargs).data for eventDict in response['results']: like_list = list() likes = Like.objects.filter(event=eventDict['event_id']) for like in likes: data = LikeSerializer(like).data like_list.append(data) eventDict['likes'] = like_list return json_response(response, CODE_SUCCESS, MSG_GET_EVENTS_SUCCESS) else: return invalid_token_response() except Exception as e: return save_error_log(request, e)
def like_view(request): try: token = request.data.get('token') event_id = request.data.get("event_id") like_user_id = request.data.get("like_user_id") user = get_user_by_token(token) if user: baby = BabyUser.objects.get(id=like_user_id) event = Event.objects.get(id=event_id) if Like.objects.filter(event=event, baby=baby): Like.objects.filter(event=event, baby=baby).delete() like = Like() like.baby = baby like.event = event like.save() response = LikeSerializer(like).data return json_response(response, CODE_SUCCESS, MSG_ADD_LIKE_SUCCESS) else: return invalid_token_response() except Exception as e: return save_error_log(request, e)