페이지

2016년 6월 4일 토요일

Django Rest Framework 파일만 별도로 업로드 하는 기능 구현

Django Rest Framework Image Upload Download
에서 이어집니다.

functional_tests/tests_upload.py

  • 우선 기존테스트를 복사해서 test_upload_file 로 테스트를 하나 만듭니다.
  • title만 먼저 등록하고
  • /imageuploads/{pk}/upload 로 파일만 전송하도록 수정합니다.
  • 업로드한 파일을 지우고
  • 파일없이 요청하면 400 코드를 반환하게 합니다.

  def test_upload_file(self):
    # title 만 입력하여 추가한다.
    r = requests.post(self.live_server_url + '/imageuploads/',
        data={
            'title':'Test Image'
        }
    )
    self.assertEqual(201, r.status_code)  # created
    url = r.json()['url']
    # /imageuploads/{pk}/upload 로 파일을 전송한다.
    file = open('functional_tests/test_image.png','rb')
    files = [
        ('imagefile', ('test_image.png', file, 'image/png'))
    ]
    r = requests.post(url + 'upload/',
        files=files
    )
    file.close()
    self.assertEqual(200, r.status_code)
    self.assertEqual('upload success', r.json()['status'])
    # 업로드된 파일을 지운다.
    r = requests.get(url)
    imagefile = r.json()['imagefile']
    imagefilepath = imagefile.__str__().replace(url,'')
    imagefile_realpath = os.path.abspath(os.path.join(MEDIA_ROOT, imagefilepath))
    os.remove(imagefile_realpath)
    # 파일없이 요청하면 400 코드를 반환한다.
    r = requests.post(url + 'upload/')
    self.assertEqual(400, r.status_code)
    self.assertEqual('no file', r.json()['status'])

views.py

  • request.data 에서 첫번째 키 값을 찾고
  • 키 값이 있으면 저장해주고
  • 없으면 400 코드를 반환합니다.

from rest_framework.response import Response
from rest_framework import status
  @detail_route(methods=['post'])
  def upload(self, request, pk=None):
    key = None
    for k in request.data:
      key = k
      break
    if key:
      r = self.get_object()
      r.imagefile = request.data[key]
      r.save()
      return Response({'status': 'upload success'})
    else:
      return Response({'status': 'no file'}, status=status.HTTP_400_BAD_REQUEST)
전체 소스코드는 아래 링크를 확인하세요.
https://github.com/kyuhyung-park/djangorestframework_practice/tree/only_file_upload

2016년 6월 3일 금요일

Django Rest Framework Image Upload Download


Django Rest Framework Simple Start
에서 이어집니다.

업로드 테스트

command
(virtualenv) tutorial>pip install requests


functional_tests/test_image.png 준비

functional_tests/tests_upload.py
from django.test import LiveServerTestCase
import requests
class UploadTest(LiveServerTestCase):
    def test_upload(self):
        file = open('functional_tests/test_image.png','rb')
        files = [
            ('imagefile', ('test_image.png', file, 'image/png'))
        ]
        r = requests.post(self.live_server_url + '/imageuploads/',
            data={
                'title':'Test Image'
            },
            files=files
        )
        file.close()
        self.assertEqual(201, r.status_code)  # created

command
(virtualenv) tutorial>python manage.py test functional_tests

테스트 실행하면 업로드된 파일이 tutorial 폴더에 저장된것을 확인할 수 있습니다.

git tag upload_test

업로드시 파일 저장위치 변경

settings.py 에 다음을 추가합니다.
MEDIA_ROOT = os.path.abspath(os.path.join(BASE_DIR, '../uploadfiles'))

업로드 되는 파일이 프로젝트 루트 기준으로 /uploadfiles 가 됩니다.

models.py 에서 imagefile 에 upload_to 를 추가합니다. 
imagefile = models.FileField(upload_to='imagefile/%Y/%m/%d', null=True)

이 필드로 인해 업로드되는 파일은 /uploadfiles/imagefile/2016/06/02 형태가 됩니다.

functional_tests/tests_upload.py
from tutorial.settings import MEDIA_ROOT
import os
class UploadTest(LiveServerTestCase):
    def test_upload(self):
        # 기존 코드 이후에 추가
        imagefile = r.json()['imagefile']
        imagefilepath = imagefile.__str__().replace(self.live_server_url + '/imageuploads/','')
        imagefile_realpath = os.path.abspath(os.path.join(MEDIA_ROOT, imagefilepath))
        os.remove(imagefile_realpath)

업로드된 파일을 지우는 코드입니다.
파일이 없을 경우 os.remove(imagefile_realpath) 에서 에러가 발생합니다.

command
(virtualenv) tutorial>python manage.py test functional_tests
테스트를 돌려 확인해 봅니다.

git tag change_upload_path

다운로드 구현

serializers.py
class ImageUploadSerializer(serializers.HyperlinkedModelSerializer):
    imagefile_url = serializers.HyperlinkedIdentityField(view_name='imageupload-imagefile', read_only=True)
   
    class Meta:
        model = ImageUpload
        fields = ('url', 'pk', 'title', 'imagefile', 'imagefile_url')
views.py
from rest_framework.decorators import detail_route
from django.http import FileResponse
class ImageUploadViewSet(viewsets.ModelViewSet):
    queryset = ImageUpload.objects.all()
    serializer_class = ImageUploadSerializer
   
    @detail_route(methods=['get'])
    def imagefile(self, request, pk=None):
        r = self.get_object()
        # 확장자 추출
        ext = '*'
        if r.imagefile.path:
            ext = r.imagefile.path.split('.')[-1]
        content_type = 'image/' + ext
        # 다운로드용 Response 반환
        response = FileResponse(open(r.imagefile.path, 'rb'), content_type=content_type)
        return response
tests_upload.py
test_upload 메소드 명을 test_upload_download 로 바꿈.
아래 내용 추가
import shutil
import filecmp
        # 파일의 마지막 부분을 아래와 같이 수정
        # --------------------------------
        # 다운로드 경로를 알아내고 요청을 보낸다.
        imagefile_url = r.json()['imagefile_url']
        r = requests.get(imagefile_url, stream=True)
        # 다운로드한 파일을 저장한다.
        with open('functional_tests/download.png', 'wb') as out_file:
            shutil.copyfileobj(r.raw, out_file)
        # 업로드 한 파일과 다운로드 한 파일이 같은지 비교한다.
        self.assertTrue(filecmp.cmp('functional_tests/test_image.png', 'functional_tests/download.png'))
        # 다운로드한 파일을 삭제한다.
        os.remove('functional_tests/download.png')
        # --------------------------------
       
        # 업로드된 파일을 지운다.
        imagefilepath = imagefile.__str__().replace(self.live_server_url + '/imageuploads/','')
        imagefile_realpath = os.path.abspath(os.path.join(MEDIA_ROOT, imagefilepath))
        os.remove(imagefile_realpath)

git tag download

전체 소스코드는 아래 링크를 확인하세요.
https://github.com/kyuhyung-park/djangorestframework_practice/tree/download


Django Rest Framework Simple Start

Django Project Start


>virtualenv virtualenv --python=f:\Anaconda3\python.exe
>virtualenv\Scripts\activate.bat
(virtualenv) >pip install django
(virtualenv) >pip install djangorestframework
(virtualenv) >django-admin.py startproject tutorial
(virtualenv) >cd tutorial
(virtualenv) tutorial>django-admin.py startapp quickstart
(virtualenv) tutorial>python manage.py migrate
>git init
>type .gitignore
*.pyc
/virtualenv
/tutorial/db.sqlite3
>git add -A
>git commit -m "project start"


Django Rest Framework Start and ImageUpload Model Create

settings.py


INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'quickstart',
    'rest_framework',
]
REST_FRAMEWORK = {
    'DEFAULT_PERMISSION_CLASSES': ('rest_framework.permissions.AllowAny',),
    'PAGE_SIZE': 10
}

models.py

class ImageUpload(models.Model):
    title = models.CharField(max_length=100)
    imagefile = models.FileField(null=True)


serializers.py

class ImageUploadSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = ImageUpload
        fields = ('url', 'pk', 'title', 'imagefile')


views.py

class ImageUploadViewSet(viewsets.ModelViewSet):
    queryset = ImageUpload.objects.all()
    serializer_class = ImageUploadSerializer


urls.py

router = routers.DefaultRouter()
router.register(r'imageuploads', views.ImageUploadViewSet)
urlpatterns = [
    url(r'^', include(router.urls))
]


command

(virtualenv) tutorial>python manage.py makemigrations
(virtualenv) tutorial>python manage.py migrate
(virtualenv) tutorial>python manage.py runserver


여기까지 진행하면 http://localhost:8000/ 에 접속해서 작동하는 모습을 볼 수 있습니다.
import 문은 생략하였습니다. 전체 소스코드는 아래 링크를 확인하세요.
https://github.com/kyuhyung-park/djangorestframework_practice/tree/rest_basic

2016년 5월 25일 수요일

Xamarin Android 프로젝트 시작하기

  • Visual Studio 2015 를 실행하고 New Project > Visual C# > Android > Blank 을 선택합니다.
  • 프로젝트 생성시 문제가 있다면 tools > options > xamarin > android settings 에서 jdk, android sdk, android ndk location을 확인합니다.
  • 실행버튼을 눌러 Xamarin Android Player 가 뜨고 앱이 실행되는것을 확인합니다. 단말기가 컴과 연결되어있다면 단말기에서 실행됩니다.
  • Visual Studio 에서 솔루션을 소스컨트롤에 추가 (솔루션디렉토리 우클릭 Add Solution to Source Control) 합니다.
  • git log 해서 보면 .gitignore, .gitattribures 파일을 추가하고 프로젝트 파일을 추가한 것을 확인할 수 있습니다.

2016년 5월 24일 화요일

git pull 시 문제해결

아래는 git pull 을 하면 가끔 발생하는 에러들입니다.
자주 까먹어서 헤메는 내용이라 정리해둡니다.

error: Your local changes to the following files would be overwritten by merge:
        [파일들...]
Please, commit your changes or stash them before you can merge.
error: The following untracked working tree files would be overwritten by merge:
        [파일들...]
Please move or remove them before you can merge.

첫번째 에러는 "Please, commit your changes or stash them before you can merge."
commit 하거나 stash 하라고 하는데요.
저의 경우 git pull 은 보통 원격저장소의 내용으로 덮어 씌우는 경우가 많아서

git stash
git pull

하여 해결합니다.

두번째 에러는 "Please move or remove them before you can merge."
문제가 되는 파일들을 이동하거나 지우라고 하는데요.
git stash 하여도 해결되지 않습니다.
추적하고 있지 않은(untracked) 파일이라 그런것 같습니다.

몇가지 방법이 있습니다.

1. untracked 파일들이라서 그런것 같으니 add 후 stash 합니다.
git add -A
git stash

2. untracked 까지 stash 해주는 옵션을 사용합니다.
이 경우 git stash pop 하면 untracked 였던 파일은 untracked 로 복원됩니다.
git stash --all

3. 워킹 디렉토리 안의 추적하고 있지 않은 모든 파일을 지웁니다.
복원이 안되므로 위 두가지 방법중 하나를 사용하는것이 좋을것 같습니다.
git clean [옵션]


저는 덮어 씌우는 목적이고 외우기 쉬워서

git add -A
git stash
git pull

을 하여 해결합니다.

참고 링크
https://git-scm.com/book/ko/v2/Git-%EB%8F%84%EA%B5%AC-Stashing%EA%B3%BC-Cleaning