File upload fixes from reviews

parent 0a260044
......@@ -5,10 +5,9 @@ python:
addons:
ssh_known_hosts: localhost
services: postgresql
sudo: true
env:
- DJANGO_SETTINGS_MODULE="webapp.settings"
services: postgresql
before_install:
- export DJANGO_SETTINGS_MODULE=webapp.settings
......@@ -22,35 +21,32 @@ before_script:
- psql -c "ALTER USER lambda WITH SUPERUSER;" -U postgres
script:
- ssh-keygen -t rsa -C your_email@youremail.com -P '' -f ~/.ssh/id_rsa
- cat /home/travis/.ssh/id_rsa.pub >> /home/travis/.ssh/authorized_keys
# - echo -e "Host *\n\tStrictHostKeyChecking no" >> /home/travis/.ssh/config
- ln -s /home/travis/.ssh/authorized_keys /home/travis/.ssh/authorized_keys2
- echo "Host localhost" >> /home/travis/.ssh/config
- echo " StrictHostKeyChecking no" >> /home/travis/.ssh/config
- chmod g-rw,o-rw /home/travis/.ssh/*
- git fetch origin $TRAVIS_BRANCH:travis_pr_branch
- flake8-diff travis_pr_branch
- cd core
- python setup.py install
- pip install -r requirements.txt
# - tox -e $TOXENV_CORE
- tox -e $TOXENV_CORE
- cd ../webapp/ansible
- cd ../webapp
- pip install -r requirements.txt
- cd ansible
- echo -e "[service-vms]\nlocalhost ansible_ssh_user=travis" > hosts
- cat hosts
- ansible-playbook playbooks/setup.yml --syntax-check
- ansible-playbook -v playbooks/setup.yml --start-at-task="Upgrade packages." --connection=local --sudo -vvvv
- ansible-playbook -v playbooks/setup.yml -e "repository_url="$TRAVIS_BUILD_DIR -e "repository_branch=HEAD" --start-at-task="Upgrade packages." --connection=local -vvvv
- cd ../
- pwd
- ls
- pip install -r requirements.txt
- python manage.py syncdb --noinput
- cd /var/www/okeanos-LoD/webapp/
- python manage.py makemigrations
- python manage.py syncdb --noinput
- python manage.py migrate
- python manage.py test
- pip list
env:
- TOXENV_CORE=py27
# - DJANGO_SETTINGS_MODULE="webapp.settings" TOXENV_CORE=py27
- TOXENV_CORE=py27
......@@ -53,7 +53,7 @@ def check_auth_token(auth_token, auth_url=None):
if ex.message == 'UNAUTHORIZED':
return False, ex.details
else:
raise ex
raise
return True, user_info
......
......@@ -24,5 +24,5 @@ commands=
[flake8]
max-line-length = 100
ignore = E127, E241
ignore = E127,E241
[flake8]
max-line-length = 100
ignore = E127, E241
ignore = E127,E241
[service-vms]
snf-670397.vm.okeanos.grnet.gr
lod-service
......@@ -13,7 +13,7 @@
include: postgresql-install.yml
- name: Download Django code from Github.
git: repo=https://github.com/grnet/okeanos-LoD.git dest={{ repository_download_path }}/okeanos-LoD update=yes version=devel # version can be used to select a branch.
git: repo={{ repository_url }} dest={{ repository_download_path }}/okeanos-LoD update=yes version={{ repository_branch }}
- name: Change repository permissions.
file: path={{ repository_download_path }} owner=celery group=celery recurse=yes
......
......@@ -3,3 +3,5 @@ database_username: lambda
database_name: lambda_db
database_user_password: change_me
repository_download_path: /var/www
repository_url: https://github.com/grnet/okeanos-LoD.git
repository_branch: devel
from django.db import models
import uuid
"""
OBJECTS
......@@ -52,7 +53,8 @@ class Project(models.Model):
class ProjectFile(models.Model):
id = models.AutoField("File ID", primary_key=True, unique=True, help_text="Project file id.")
id = models.AutoField("id", primary_key=True, unique=True, help_text="Project file id.")
uuid = models.UUIDField("uuid", unique=True, default=uuid.uuid4, help_text="Project file uuid.")
name = models.CharField(max_length=100)
path = models.CharField(max_length=400)
description = models.CharField(max_length=400, blank=True, default='')
......
......@@ -5,7 +5,7 @@ from .models import ProjectFile, LambdaInstance, Server, PrivateNetwork
class ProjectFileSerializer(serializers.ModelSerializer):
class Meta:
model = ProjectFile
fields = ('id', 'name', 'description')
fields = ('uuid', 'name', 'description')
class ServerSerializer(serializers.ModelSerializer):
......
import json
from os import path, mkdir
import uuid
from os import path, mkdir, remove
from django.conf import settings
from django.http import JsonResponse
......@@ -61,33 +63,43 @@ class ProjectFileList(APIView):
return Response({"errors": [{"message": "No file uploaded", "code": 422}]}, status=422)
description = request.data.get('description', '')
new_file_path = path.join(settings.FILE_STORAGE, uploaded_file.name)
if not path.exists(settings.FILE_STORAGE):
mkdir(settings.FILE_STORAGE)
if ProjectFile.objects.filter(name=uploaded_file.name).count() > 0:
return Response({"errors": [{"message": "File name already exists"}]}, status=400)
with open(new_file_path, 'wb+') as f:
f.write(uploaded_file.read())
if path.isfile(new_file_path):
# TODO: Change this to an event call that updates the db
file_uuid = uuid.uuid4()
ProjectFile.objects.create(name=uploaded_file.name,
path=new_file_path,
description=description,
owner=request.user)
return Response({"result": "success"}, status=201)
owner=request.user,
uuid=file_uuid)
return Response({"result": "success"}, status=201)
def delete(self, request, format=None):
file_id = request.data.get('id')
if not file_id:
return Response({"errors:"[{"message": "missing id header", "code": 422}]},
file_uuid = request.data.get('uuid')
if not file_uuid:
return Response({"errors": [{"message": "missing id header"}]},
status=422)
try:
file_data = ProjectFile.objects.get(id=file_id)
file_data = ProjectFile.objects.get(uuid=file_uuid)
except ProjectFile.DoesNotExist:
return Response({"errors": [{"message": "file does not exist", "code": 400}]},
return Response({"errors": [{"message": "file does not exist"}]},
status=400)
if file_data.owner != request.user:
return Response({"errors:"[{"message": "file does not exist", "code": 400}]},
return Response({"errors": [{"message": "file does not exist"}]},
status=400)
# TODO: Change this to an event call that update the db
if path.isfile(file_data.path):
remove(file_data.path)
file_data.delete()
return Response({"result": "success"}, status=200)
......
[pytest]
DJANGO_SETTINGS_MODULE = backend.settings
\ No newline at end of file
......@@ -10,25 +10,28 @@ class TestFileUpload(APITestCase):
self.user = User.objects.create(uuid='209230923ur92r029u3r')
self.client.force_authenticate(user=self.user)
def test_file_upload(self):
file_to_upload = SimpleUploadedFile("test_upload_file",
def upload_test_file(self, name):
file_to_upload = SimpleUploadedFile(name,
"test12345@#%^!@(*^$@!*(""&^!@∂¨ˆ•¡™¶ª©∆˚ß"
"ƒå¬åµ˜∂˜ˆ¨˙£™®¨˜ß∂√©¨ˆ™˙£∫®¨∂婃™ø`ˆ¨")
response = self.client.put('/backend/user_files',
{'file': file_to_upload},
format='multipart')
return response
def test_file_upload(self):
response = self.upload_test_file("test_file_upload")
self.assertEqual(response.status_code, 201)
def test_list_files(self):
self.test_file_upload()
self.upload_test_file(name="test_file_list")
response = self.client.get('/backend/user_files')
self.assertEqual(response.data[0]['name'], u'test_upload_file')
self.assertEqual(response.data[0]['name'], u'test_file_list')
def test_delete_file(self):
self.test_file_upload()
self.upload_test_file(name="test_file_delete")
response = self.client.get('/backend/user_files')
id = response.data[0]['id']
self.client.delete('/backend/user_files', {'id': id})
uuid = response.data[0]['uuid']
self.client.delete('/backend/user_files', {'uuid': uuid})
response = self.client.get('/backend/user_files')
self.assertEqual(len(response.data), 0)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment