Пример #1
0
 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)
Пример #2
0
    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)
Пример #3
0
    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)
Пример #4
0
    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)
Пример #5
0
 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)
Пример #6
0
    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)
Пример #7
0
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)
Пример #8
0
def reset_password_with_verify_code_view(request):
    code = request.data.get('verify_code')
    email = request.data.get('email')
    password = request.data.get('password')

    try:
        if not code:
            return simple_json_response(CODE_EMPTY_VERIFY_CODE,
                                        MSG_EMPTY_VERIFY_CODE)
        elif not email:
            return simple_json_response(CODE_EMPTY_EMAIL, MSG_EMPTY_EMAIL)
        elif not password:
            return simple_json_response(CODE_EMPTY_PASSWORD,
                                        MSG_EMPTY_PASSWORD)
        elif not validate_email(email):
            return simple_json_response(CODE_INVALID_EMAIL, MSG_INVALID_EMAIL)
        elif len(password) < MIN_PASSWORD_LEN:
            return simple_json_response(CODE_INVALID_PASSWORD,
                                        MSG_INVALID_PASSWORD)

        user = get_user(email=email)
        if user:
            baby = BabyUser.objects.get(user=user)
            if Verify.objects.filter(baby=baby,
                                     email_verify_code=code.lower()):
                verify = Verify.objects.get(baby=baby,
                                            email_verify_code=code.lower())
                if (time.time() - float(format(
                        verify.created, 'U'))) > VERIFY_CODE_EXPIRED_TIME:
                    return simple_json_response(CODE_EXPIRED_VERIFY_CODE,
                                                MSG_EXPIRED_VERIFY_CODE)
                else:
                    user.set_password(password)
                    user.save()
                    return simple_json_response(CODE_SUCCESS,
                                                MSG_SEND_VERIFY_CODE_SUCCESS)
            else:
                return simple_json_response(CODE_INCORRECT_VERIFY_CODE,
                                            MSG_INCORRECT_VERIFY_CODE)
        else:
            return simple_json_response(CODE_USER_NOT_EXISTS,
                                        MSG_USER_NOT_EXISTS)
    except Exception as e:
        return save_error_log(request, e)
Пример #9
0
def login_view(request):
    username = request.data.get('username', '').lower()
    password = request.data.get('password')
    token = request.data.get('token')

    try:
        if token:
            user = Token.objects.get(key=token).user
        else:
            user = authenticate(username=username, password=password)
            if not user and validate_email(username):
                user = get_user(email=username)
                if not user:
                    user = authenticate(username=user.username,
                                        password=password)
        if user:
            baby = BabyUser.objects.get(user=user)
            if baby:
                if user.is_active:
                    response_data = BabyUserSerializer(baby).data
                    if Token.objects.filter(user=user):
                        response_data['token'] = Token.objects.get(
                            user=user).key
                        response_data['password'] = password
                        return json_response(response_data, CODE_SUCCESS,
                                             MSG_LOGIN_SUCCESS)
                    else:
                        return invalid_token_response()
                else:
                    return simple_json_response(CODE_NOT_ACTIVE,
                                                MSG_NOT_ACTIVE_USER)
            else:
                user.delete()
                return simple_json_response(
                    CODE_INCORRECT_USER_NAME_OR_PASSWORD,
                    MSG_INCORRECT_USER_NAME_OR_PASSWORD)
        else:
            return simple_json_response(CODE_INCORRECT_USER_NAME_OR_PASSWORD,
                                        MSG_INCORRECT_USER_NAME_OR_PASSWORD)
    except Exception as e:
        return save_error_log(request, e)
Пример #10
0
    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)
Пример #11
0
    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)
Пример #12
0
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)
Пример #13
0
def send_verify_code_view(request):
    email = request.data.get('email')
    try:
        if not email:
            return simple_json_response(CODE_EMPTY_EMAIL, MSG_EMPTY_EMAIL)
        elif not User.objects.filter(email=email.lower()) and \
                not User.objects.filter(username=email.lower()):
            return simple_json_response(CODE_INVALID_EMAIL, MSG_NO_SUCH_EMAIL)

        baby = None
        if User.objects.filter(email=email.lower()):
            user = User.objects.get(email=email.lower())
            baby = BabyUser.objects.get(user=user)
        elif User.objects.filter(username=email.lower()):
            user = User.objects.get(username=email.lower())
            baby = BabyUser.objects.get(user=user)

        verify_code = get_random_string(length=6,
                                        allowed_chars='0123456789').lower()
        send_email(baby, email, verify_code)

        return simple_json_response(CODE_SUCCESS, MSG_SEND_VERIFY_CODE_SUCCESS)
    except Exception as e:
        return save_error_log(request, e)
Пример #14
0
    def create(self, request, *args, **kwargs):
        try:
            serializer = self.get_serializer(data=request.data)
            username = request.data.get('username')
            baby_name = request.data.get('baby_name')
            password = request.data.get('password')
            email = request.data.get('email')
            first_name = request.data.get('first_name', '')
            last_name = request.data.get('last_name', '')
            base64 = request.data.get('base64', '')

            if not username:
                return simple_json_response(CODE_EMPTY_USER,
                                            MSG_EMPTY_USERNAME)
            elif not baby_name:
                return simple_json_response(CODE_EMPTY_BABY_NAME,
                                            MSG_EMPTY_BABY_NAME)
            elif not email:
                return simple_json_response(CODE_EMPTY_EMAIL, MSG_EMPTY_EMAIL)
            elif not password:
                return simple_json_response(CODE_EMPTY_PASSWORD,
                                            MSG_EMPTY_PASSWORD)
            elif not validate_email(email):
                return simple_json_response(CODE_INVALID_EMAIL,
                                            MSG_INVALID_EMAIL)
            elif len(password) < MIN_PASSWORD_LEN:
                return simple_json_response(CODE_INVALID_PASSWORD,
                                            MSG_INVALID_PASSWORD)
            elif User.objects.filter(
                    username__iexact=username) or User.objects.filter(
                        username__iexact=email):
                return simple_json_response(CODE_DUPLICATE_USER,
                                            MSG_DUPLICATE_USER)
            elif User.objects.filter(
                    email__iexact=email) or User.objects.filter(
                        email__iexact=username):
                return simple_json_response(CODE_DUPLICATE_EMAIL,
                                            MSG_DUPLICATE_EMAIL)
            elif serializer.is_valid():
                user = User()
                user.email = email.lower()
                user.is_active = True
                user.is_staff = True
                user.set_password(password)
                user.username = username.lower()
                user.first_name = first_name
                user.last_name = last_name
                self.request.user = user
                self.perform_create(serializer)
                response_data = serializer.data
                response_data['token'] = Token.objects.create(user=user).key
                baby_user = BabyUser.objects.get(user=user)
                baby_user.locale = response_data['locale']
                baby_user.created = timezone.now()
                if base64:
                    image_name = username + time.strftime(
                        '%Y%m%d%H%M%S') + PROFILE_FOOTER_IMAGE
                    response_data['profile'] = upload_file_to_oss(
                        user.username + DIR_USER_PROFILE + image_name, base64)
                    baby_user.profile = response_data['profile']
                baby_user.save()
                return json_response(response_data, CODE_SUCCESS,
                                     MSG_CREATE_USER_SUCCESS)
            else:
                return simple_json_response(CODE_INVALID_REQUEST, MSG_400)
        except Exception as e:
            return save_error_log(request, e)