Skip to content

Commit

Permalink
Fixed updating document Content Type
Browse files Browse the repository at this point in the history
  • Loading branch information
kroman0 committed Jan 28, 2015
1 parent 54f480d commit 2c52cc1
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/openprocurement/api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ class Document(Model):
class Options:
serialize_when_none = False
roles = {
'edit': blacklist('id', 'format', 'url', 'datePublished'),
'edit': blacklist('id', 'url', 'datePublished', 'dateModified'),
'embedded': schematics_embedded_role,
'view': (blacklist('revisions') + schematics_default_role),
'revisions': whitelist('url', 'dateModified'),
Expand All @@ -195,7 +195,7 @@ class Options:
description = StringType() # A description of the document.
description_en = StringType()
description_ru = StringType()
format = StringType()
format = StringType(regex='^[-\w]+/[-\.\w\+]+$')
url = StringType() # Link to the document or attachment.
datePublished = IsoDateTimeType(default=get_now)
dateModified = IsoDateTimeType(default=get_now) # Date that the document was last dateModified
Expand Down
5 changes: 5 additions & 0 deletions src/openprocurement/api/tests/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,11 @@ def new_key(self, key_name=None):
self.acls[key_name] = MockAcl()
return mock_key

def get_key(self, key_name, headers=NOT_IMPL, version_id=NOT_IMPL):
# Emulate behavior of boto when get_key called with non-existent key.
if key_name not in self.keys:
return None
return self.keys[key_name]

class MockProvider(object):

Expand Down
12 changes: 12 additions & 0 deletions src/openprocurement/api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from time import sleep
from cornice.util import json_error
from json import dumps
from urlparse import urlparse, parse_qs

try:
from systemd.journal import JournalHandler
Expand Down Expand Up @@ -83,6 +84,17 @@ def upload_file(request):
return document


def update_file_content_type(request):
conn = getattr(request.registry, 's3_connection', None)
if conn:
document = request.validated['document']
key = parse_qs(urlparse(document.url).query).get('download').pop()
bucket = conn.get_bucket(request.registry.bucket_name)
filename = "{}/{}/{}".format(request.validated['tender_id'], document.id, key)
key = bucket.get_key(filename)
key.set_metadata('Content-Type', document.format)


def get_file(request):
tender_id = request.validated['tender_id']
document = request.validated['document']
Expand Down
2 changes: 2 additions & 0 deletions src/openprocurement/api/views/award_complaint_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
apply_patch,
error_handler,
update_journal_handler_params,
update_file_content_type,
)
from openprocurement.api.validation import (
validate_file_update,
Expand Down Expand Up @@ -96,5 +97,6 @@ def patch(self):
self.request.errors.status = 403
return
if apply_patch(self.request, src=self.request.context.serialize()):
update_file_content_type(self.request)
LOGGER.info('Updated tender award complaint document {}'.format(self.request.context.id), extra={'MESSAGE_ID': 'tender_award_complaint_document_patch'})
return {'data': self.request.context.serialize("view")}
2 changes: 2 additions & 0 deletions src/openprocurement/api/views/award_contract_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
apply_patch,
error_handler,
update_journal_handler_params,
update_file_content_type,
)
from openprocurement.api.validation import (
validate_file_update,
Expand Down Expand Up @@ -110,5 +111,6 @@ def patch(self):
self.request.errors.status = 403
return
if apply_patch(self.request, src=self.request.context.serialize()):
update_file_content_type(self.request)
LOGGER.info('Updated tender award contract document {}'.format(self.request.context.id), extra={'MESSAGE_ID': 'tender_award_contract_document_patch'})
return {'data': self.request.context.serialize("view")}
2 changes: 2 additions & 0 deletions src/openprocurement/api/views/award_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
apply_patch,
error_handler,
update_journal_handler_params,
update_file_content_type,
)
from openprocurement.api.validation import (
validate_file_update,
Expand Down Expand Up @@ -96,5 +97,6 @@ def patch(self):
self.request.errors.status = 403
return
if apply_patch(self.request, src=self.request.context.serialize()):
update_file_content_type(self.request)
LOGGER.info('Updated tender award document {}'.format(self.request.context.id), extra={'MESSAGE_ID': 'tender_award_document_patch'})
return {'data': self.request.context.serialize("view")}
2 changes: 2 additions & 0 deletions src/openprocurement/api/views/bid_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
apply_patch,
error_handler,
update_journal_handler_params,
update_file_content_type,
)
from openprocurement.api.validation import (
validate_file_update,
Expand Down Expand Up @@ -104,5 +105,6 @@ def patch(self):
self.request.errors.status = 403
return
if apply_patch(self.request, src=self.request.context.serialize()):
update_file_content_type(self.request)
LOGGER.info('Updated tender bid document {}'.format(self.request.context.id), extra={'MESSAGE_ID': 'tender_bid_document_patch'})
return {'data': self.request.context.serialize("view")}
2 changes: 2 additions & 0 deletions src/openprocurement/api/views/complaint_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
apply_patch,
error_handler,
update_journal_handler_params,
update_file_content_type,
)
from openprocurement.api.validation import (
validate_file_update,
Expand Down Expand Up @@ -96,5 +97,6 @@ def patch(self):
self.request.errors.status = 403
return
if apply_patch(self.request, src=self.request.context.serialize()):
update_file_content_type(self.request)
LOGGER.info('Updated tender complaint document {}'.format(self.request.context.id), extra={'MESSAGE_ID': 'tender_complaint_document_patch'})
return {'data': self.request.context.serialize("view")}
2 changes: 2 additions & 0 deletions src/openprocurement/api/views/tender_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
apply_patch,
error_handler,
update_journal_handler_params,
update_file_content_type,
)
from openprocurement.api.validation import (
validate_file_update,
Expand Down Expand Up @@ -95,5 +96,6 @@ def patch(self):
self.request.errors.status = 403
return
if apply_patch(self.request, src=self.request.context.serialize()):
update_file_content_type(self.request)
LOGGER.info('Updated tender document {}'.format(self.request.context.id), extra={'MESSAGE_ID': 'tender_document_patch'})
return {'data': self.request.context.serialize("view")}

0 comments on commit 2c52cc1

Please sign in to comment.