示例#1
0
def upload_file_view(request):
    if request.method == "POST":
        # print "deal with post request"
        # print request.POST
        # print request.FILES
        form = UploadFileForm(request.POST, request.FILES)

        if form.is_valid():
            f = request.FILES['file']
            # title = request.POST["title"]

            # ## Handled by model with filefiled or imagefield
            # ## File is saved to MEDIA_ROOT/upload_to or storage root/upload_to
            # ## upload_to is a parameter when creating the model
            profile = Profile.objects.get(profileuser = request.user)
            # 改变文件的名字
            f.name = utils.hash_file_name(f.name)
            instance = AvatarPhoto(owner = profile, avatar=f)
            instance.save()

            return HttpResponse('image upload success')
            ## or redirect to succuess url
            # return HttpResponseRedirect("/main/upload/success")

        else:
            print (u"form is invalid")

    elif request.method == "GET":
        form = UploadFileForm()
        return render(request, 'upload_file_page.html', {'form':form})
示例#2
0
def confirm_send_card_with_photo(request):
    """
    用户确定发送,创建明信片, 可以有图片, 或者没有
    :param request:
    :return:
    """
    try:
        profile_of_logged_user = Profile.objects.get(profileuser=request.user)
    except Profile.DoesNotExist:
        return HttpResponse(json.dumps({
            "details":
            "Can not find profile of the logger user, possibly not address"
        }),
                            status=status.HTTP_400_BAD_REQUEST)

    if request.method == "POST":
        # print request.data ## <QueryDict
        # print request.FILES # <MultiValueDict: {}>

        # has_photo_int = request.data["has_photo_int"]
        #
        # if has_photo_int == 1 or has_photo_int == "1":
        #     print "has photo"
        # else:
        #     print "not photo"

        card_name = request.data["card_name"]
        torecipient_id = request.data["torecipient_id"]

        # create a card, cardphoto; sent postcard action with photo
        # activity_type_id = 2, SPP
        card_photo_file = request.FILES['card_photo']
        card_photo_file.name = hash_file_name(card_photo_file.name)

        # photo = CardPhoto(owner=profile_of_logged_user,
        #                   card_host=card,
        #                   card_photo=card_photo_file)
        # photo.save()

        card, sent_card_has_photo_action, card_photo = \
            Card.objects.create_with_card_action_photo_objs_returned(
            card_name=card_name, torecipient_id=torecipient_id, fromsender_id=profile_of_logged_user.id,
            card_photo_file=card_photo_file
        )

        card_s = CardSerializer(card, context={'request': request})
        action_s = SentCardActionSerializer(sent_card_has_photo_action)
        card_photo_s = CardPhotoSerializer(card_photo)

        return Response(
            {
                "card": card_s.data,
                "sent_card_photo_action": action_s.data,
                "card_photo": card_photo_s.data
            },
            status=status.HTTP_201_CREATED)
示例#3
0
def upload_cardphoto_afterwards_by_id(request, pk):
    if request.method == "POST":
        try:
            profile_from_request = Profile.objects.get(
                profileuser=request.user)

            # pk为明信片的Id
            card_host = Card.objects.get(pk=int(pk))

            ### get the recipient and sender of the card
            recipient = card_host.torecipient
            sender = card_host.fromsender

            # 确保目前登录用户就是明信片的接受者
            if profile_from_request.id == recipient.id or profile_from_request.id == sender.id:
                f = request.FILES['cardphoto']
                f.name = utils.hash_file_name(f.name)

                photo = CardPhoto(owner=profile_from_request,
                                  card_host=card_host,
                                  card_photo=f)
                photo.save()

                name = photo.card_photo.name
                url = photo.card_photo.url

                serializer = CardPhotoSerializer(photo,
                                                 context={'request': request})
                data = serializer.data
                data["name_on_server"] = name
                data["url_on_server"] = url

                return Response(data, status=status.HTTP_201_CREATED)
            else:
                Response(
                    {
                        "details":
                        "card recipient!= profileuser, sender !=profileuser",
                        "id1": recipient.id,
                        "id2": profile_from_request.id
                    },
                    status=status.HTTP_200_OK)

        except Profile.DoesNotExist:
            return Response(
                {"details": "Current logged user's profile does not exist"},
                status=status.HTTP_404_NOT_FOUND)

        except Card.DoesNotExist:
            return Response(
                {"details": "Postcard with id %s does not exist" % pk},
                status=status.HTTP_404_NOT_FOUND)
示例#4
0
def upload_avatarphoto(request):
    if request.method == "POST":
        try:
            profile = Profile.objects.get(profileuser=request.user)

            # DJANGO REST, parse request.body
            # print request.data

            # DJANGO
            # print request.FILES
            # print request.POST

            ## 前端按照有表单Form, 文件在request.FILES里面, 也在request.data里面

            # 上传的文件
            f = request.FILES['avatar']
            # 改变文件名,注意不要改变文件格式后缀
            f.name = utils.hash_file_name(f.name)

            photo = AvatarPhoto(owner=profile, avatar=f)
            photo.save()

            serializer = AvatarPhotoSerializer(photo,
                                               context={'request': request})
            data = serializer.data

            data["avatarHasBaseUrl"] = 1

            return Response(data=data, status=status.HTTP_201_CREATED)

        except Profile.DoesNotExist:
            return Response(
                {
                    "details":
                    "Internal server error! Can not find Profile object."
                },
                status=status.HTTP_500_INTERNAL_SERVER_ERROR)
示例#5
0
def upload_cardphoto_afterwards_by_cardname(request, card_name):
    """
    Upload a photo for an card
    :param request:
    :param card_name:
    :return:
    """
    try:
        profile_from_request_user = Profile.objects.get(
            profileuser=request.user)
    except Profile.DoesNotExist:
        return Response({"details": "Can not find profile"},
                        status=status.HTTP_404_NOT_FOUND)

    if card_name != request.data["card_name"]:
        return Response(
            {
                "details":
                "card name in url %s != card name in request %s" %
                (card_name, request.data["card_name"])
            },
            status=status.HTTP_400_BAD_REQUEST)

    if request.method == "POST":
        card_name = request.data["card_name"]
        # f = request.data['card_photo']
        card_photo_file = request.FILES['card_photo']
        card_photo_file.name = hash_file_name(card_photo_file.name)

        try:
            card = Card.objects.get(card_name=card_name)
        except Card.DoesNotExist:
            return Response(
                {"details": "card with card name %s is invalid" % card_name},
                status=status.HTTP_404_NOT_FOUND)

        if profile_from_request_user.id == card.torecipient.id or \
            profile_from_request_user.id == card.fromsender.id:

            photo = CardPhoto(owner=profile_from_request_user,
                              card_host=card,
                              card_photo=card_photo_file)
            photo.save()

            card_photo_serializer = CardPhotoSerializer(
                photo, context={'request': request})

            action = UploadCardPhotoAction(subject=request.user,
                                           card_actioned=card,
                                           card_photo_uploaded=photo)
            action.save()

            action_s = UploadCardPhotoActionSerializer(action)

            return Response(
                {
                    "card_photo": card_photo_serializer.data,
                    "action": action_s.data
                },
                status=status.HTTP_201_CREATED)

        else:
            return Response(
                {
                    "details":
                    "You are neither sender nor recipient of the card!"
                },
                status=status.HTTP_403_FORBIDDEN)
示例#6
0
def upload_cardphoto(request):
    """
    Upload a photo for an card
    :param request:
    :return:
    """
    try:
        profile_from_request_user = Profile.objects.get(
            profileuser=request.user)
    except Profile.DoesNotExist:
        return Response({"details": "Can not find profile"},
                        status=status.HTTP_404_NOT_FOUND)

    if request.method == "POST":

        card_name = request.data["card_name"]
        # f = request.data['card_photo']
        card_photo_file = request.FILES['card_photo']
        card_photo_file.name = hash_file_name(card_photo_file.name)

        # print card_name

        try:
            card = Card.objects.get(card_name=card_name)
        except Card.DoesNotExist:
            return Response(
                {"details": "Card with post id %s is invalid" % card_name},
                status=status.HTTP_404_NOT_FOUND)


        if profile_from_request_user.id == card.torecipient.id or \
            profile_from_request_user.id == card.fromsender.id:

            photo = CardPhoto(owner=profile_from_request_user,
                              card_host=card,
                              card_photo=card_photo_file)
            photo.save()

            name = photo.card_photo.name
            url = photo.card_photo.url
            card_photo = photo.card_photo

            response_data = {}
            response_data['card_name'] = card.card_name
            response_data["name_on_server"] = name
            response_data["url_on_server"] = url
            response_data["card_photo"] = card_photo
            response_data["card_host_id"] = card.id
            response_data["owner_id"] = profile_from_request_user.id

            print response_data

            return Response(response_data, status=status.HTTP_201_CREATED)

        else:
            return Response(
                {
                    "details":
                    "You are neither sender nor recipient of the card!"
                },
                status=status.HTTP_403_FORBIDDEN)
示例#7
0
def receive_a_card_with_photo(request):
    """
    recevie a card from others, register a card
    :param request:
    :return:
    """
    # print request.data
    ## In REST, request.data = request.POST + request.FILES in django
    ## In REST, request.query_params = request.GET

    try:
        profile_from_request_user = Profile.objects.get(
            profileuser=request.user)
    except Profile.DoesNotExist:
        return Response(
            {
                "details":
                "profile is invalid! not registered user or not address"
            },
            status=status.HTTP_404_NOT_FOUND)

    if request.method == "POST":
        #### add photo
        card_name = request.data["card_name"]
        # f = request.data['card_photo']
        card_photo_file = request.FILES['card_photo']
        card_photo_file.name = hash_file_name(card_photo_file.name)

        try:
            card = Card.objects.filter(card_name=card_name).first()
            if not card:
                return Response({"details": "card with post id is invalid"},
                                status=status.HTTP_404_NOT_FOUND)
        except Card.DoesNotExist:
            return Response({"details": "card with post id is invalid"},
                            status=status.HTTP_404_NOT_FOUND)

        ## has_arrived is true, respond with ok
        if card.has_arrived:
            return Response({"details": "%s already registered" % card_name},
                            status=status.HTTP_400_BAD_REQUEST)

        ## check wether the recipient id is the one of the request
        if profile_from_request_user.id == card.torecipient.id:
            receive_card_action, card_photo = card.mark_arrived(
                has_photo=True,
                card_photo_file=card_photo_file)  # VERY IMPORTANT

            response_data = {}
            response_data['card_name'] = card.card_name
            response_data['arrived_time'] = card.arrived_time
            response_data['has_arrived'] = card.has_arrived
            response_data["card_host_id"] = card.id
            response_data["owner_id"] = profile_from_request_user.id

            action_s = ReceiveCardActionSerializer(receive_card_action)
            response_data["action"] = action_s.data

            # photo = CardPhoto(owner=profile_from_request_user,
            #                   card_host=card,
            #                   card_photo=card_photo_file)
            # photo.save()

            response_data["card_photo_name"] = card_photo.card_photo.name
            response_data["card_photo_url"] = card_photo.card_photo.url

            return Response(response_data, status=status.HTTP_201_CREATED)

        else:
            return Response(
                {
                    "details":
                    "You are not the recipient of the card %s " % card_name
                },
                status=status.HTTP_403_FORBIDDEN)