Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
[LIB-852] re-work hosting; add HostingFile models; add update_file me…
…thod;
  • Loading branch information
opalczynski committed Aug 29, 2016
commit 962253e4db463a5522568d57b04ae236187c900f
80 changes: 71 additions & 9 deletions syncano/models/hosting.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# -*- coding: utf-8 -*-

from syncano.exceptions import SyncanoRequestError

from . import fields
from .base import Instance, Model, logger

Expand Down Expand Up @@ -31,24 +34,51 @@ class Meta:
}

def upload_file(self, path, file):
"""
Upload a new file to the hosting.
:param path: the file path;
:param file: the file to be uploaded;
:return: the response from the API;
"""
files_path = self.links.files
data = {'path': path}
connection = self._get_connection()
params = connection.build_params(params={})
headers = params['headers']
headers.pop('content-type')
response = connection.session.post(connection.host + files_path, headers=headers,
headers = self._prepare_header(connection)
response = connection.session.post('{}{}'.format(connection.host, files_path), headers=headers,
data=data, files=[('file', file)])
if response.status_code != 201:
logger.error(response.text)
return
return response
return HostingFile(**response.json())

def update_file(self, path, file):
"""
Updates an existing file.
:param path: the file path;
:param file: the file to be uploaded;
:return: the response from the API;
"""
hosting_files = self._get_files()
is_found = False
for hosting_file in hosting_files:
if hosting_file.path == path:
is_found = True
break

if not is_found:
raise SyncanoRequestError('File with path {} not found.'.format(path))

def list_files(self):
files_path = self.links.files
connection = self._get_connection()
response = connection.request('GET', files_path)
return [f['path'] for f in response['objects']]
headers = self._prepare_header(connection)
response = connection.session.patch('{}{}'.format(connection.host, hosting_file.links.self), headers=headers,
files=[('file', file)])
if response.status_code != 200:
logger.error(response.text)
return
return HostingFile(**response.json())

def list_files(self):
return self._get_files()

def set_default(self):
default_path = self.links.set_default
Expand All @@ -57,3 +87,35 @@ def set_default(self):
response = connection.make_request('POST', default_path)
self.to_python(response)
return self

def _prepare_header(self, connection):
params = connection.build_params(params={})
headers = params['headers']
headers.pop('content-type')
return headers

def _get_files(self):
return [hfile for hfile in HostingFile.please.list(hosting_id=self.id)]


class HostingFile(Model):
"""
OO wrapper around hosting file.
"""

path = fields.StringField(max_length=300)
file = fields.FileField()
links = fields.LinksField()

class Meta:
parent = Hosting
endpoints = {
'detail': {
'methods': ['delete', 'get', 'put', 'patch'],
'path': '/files/{id}/',
},
'list': {
'methods': ['post', 'get'],
'path': '/files/',
}
}
21 changes: 16 additions & 5 deletions tests/integration_tests_hosting.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,23 @@ def test_create_file(self):
a_hosting_file.write('h1 {color: #541231;}')
a_hosting_file.seek(0)

self.hosting.upload_file(path='styles/main.css', file=a_hosting_file)

files_list = self.hosting.list_files()

self.assertIn('styles/main.css', files_list)
hosting_file = self.hosting.upload_file(path='styles/main.css', file=a_hosting_file)
self.assertEqual(hosting_file.path, 'styles/main.css')

def test_set_default(self):
hosting = self.hosting.set_default()
self.assertIn('default', hosting.domains)

def test_update_file(self):
a_hosting_file = StringIO()
a_hosting_file.write('h1 {color: #541231;}')
a_hosting_file.seek(0)

self.hosting.upload_file(path='styles/main.css', file=a_hosting_file)

a_hosting_file = StringIO()
a_hosting_file.write('h2 {color: #541231;}')
a_hosting_file.seek(0)

hosting_file = self.hosting.update_file(path='styles/main.css', file=a_hosting_file)
self.assertEqual(hosting_file.path, 'styles/main.css')