Skip to content

Commit

Permalink
foo
Browse files Browse the repository at this point in the history
  • Loading branch information
Szaki committed Oct 2, 2023
1 parent 68e47b3 commit 5b08e04
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions binder/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1830,21 +1830,25 @@ def _store_m2m_field(self, obj, field, value, request):
def _resolve_file_path(self, value, request):
match = re.match(r'/api/(\w+)/(\d+)/(\w+)/', value)
if not match:
print('no match')
return None

model, pk, field = match.groups()
try:
model = self.router.name_models[model]
except KeyError:
print('KeyError')
return None

view = self.get_model_view(model)
if field not in view.file_fields:
print('not in')
return None

try:
obj = view.get_queryset(request).get(pk=pk)
except model.DoesNotExist:
print('doesnt exist')
return None

value = getattr(obj, field)
Expand Down Expand Up @@ -2023,11 +2027,15 @@ def _store_field(self, obj, field, value, request, pk=None):
elif isinstance(f, models.FileField):
# If the value is a str that matches how we return file
# fields we convert it to the correct file

print(f'value: {value}')
if isinstance(value, str):
value_ = self._resolve_file_path(value, request)
print(f'value_: {value_}')
if value_ is not None:
value = value_

print(f'processed value: {value}')
if not isinstance(value, File) and value is not None:
raise BinderFieldTypeError(self.model.__name__, field)

Expand Down Expand Up @@ -2774,6 +2782,7 @@ def dispatch_file_field(self, request, pk=None, file_field=None):
try:
# Take an arbitrary uploaded file
file = next(request.FILES.values())
print(f'file: {file}')
except StopIteration:
raise BinderRequestError('File POST should use multipart/form-data (with an arbitrary key for the file data).')

Expand All @@ -2784,6 +2793,8 @@ def dispatch_file_field(self, request, pk=None, file_field=None):

field = self.model._meta.get_field(file_field_name)
path = self.router.model_route(self.model, obj.id, field)
print(f'field: {field}')
print(f'path: {path}')
# {duplicate-binder-file-field-hash-code}
if isinstance(field, BinderFileField):
file_field = getattr(obj, file_field_name)
Expand Down

0 comments on commit 5b08e04

Please sign in to comment.