Skip to content

Commit

Permalink
input: JSON Text field input
Browse files Browse the repository at this point in the history
  • Loading branch information
jarofgreen committed Nov 7, 2022
1 parent b2946fd commit bfd3172
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 26 deletions.
4 changes: 4 additions & 0 deletions libcoveweb2/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ class NewJSONUploadForm(forms.Form):
)


class NewJSONTextForm(forms.Form):
paste = forms.CharField(label="Paste (JSON only)", widget=forms.Textarea)


class NewSpreadsheetUploadForm(forms.Form):
file_upload = forms.FileField(
widget=forms.FileInput(
Expand Down
23 changes: 23 additions & 0 deletions libcoveweb2/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,29 @@ def save_file(self, f, meta={}):
for chunk in f.chunks():
destination.write(chunk)

def save_file_contents(
self,
filename: str,
contents: str,
content_type: str,
charset: str = None,
meta: dict = {},
):

os.makedirs(self.upload_dir(), exist_ok=True)

supplied_data_file = SuppliedDataFile()
supplied_data_file.supplied_data = self
supplied_data_file.filename = filename
supplied_data_file.size = len(contents)
supplied_data_file.content_type = content_type
supplied_data_file.charset = charset
supplied_data_file.meta = meta
supplied_data_file.save()

with open(supplied_data_file.upload_dir_and_filename(), "w") as destination:
destination.write(contents)


class SuppliedDataFile(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
Expand Down
12 changes: 12 additions & 0 deletions libcoveweb2/templates/libcoveweb2/new_json.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,16 @@
</form>
</div>

<div class="panel panel-default">
<form method="POST" action="{% url 'new_json' %}" enctype="multipart/form-data">
{% csrf_token %}
{% bootstrap_form forms.text_form %}
{% buttons %}
<button type="submit" class="btn btn-primary">
{% trans 'Submit' %}
</button>
{% endbuttons %}
</form>
</div>

{% endblock %}
80 changes: 54 additions & 26 deletions libcoveweb2/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,43 +6,71 @@

from libcoveweb2.forms import (
NewCSVsUploadForm,
NewJSONTextForm,
NewJSONUploadForm,
NewSpreadsheetUploadForm,
)
from libcoveweb2.models import SuppliedData, SuppliedDataFile

JSON_FORM_CLASSES = {
"upload_form": NewJSONUploadForm,
"text_form": NewJSONTextForm,
}


def new_json(request):

forms = {
"upload_form": NewJSONUploadForm(request.POST, request.FILES)
if request.POST
else NewJSONUploadForm()
form_name: form_class() for form_name, form_class in JSON_FORM_CLASSES.items()
}
form = forms["upload_form"]
if form.is_valid():
# Extra Validation
if (
not request.FILES["file_upload"].content_type
in settings.ALLOWED_JSON_CONTENT_TYPES
):
form.add_error("file_upload", "This does not appear to be a JSON file")
if not [
e
for e in settings.ALLOWED_JSON_EXTENSIONS
if str(request.FILES["file_upload"].name).lower().endswith(e)
]:
form.add_error("file_upload", "This does not appear to be a JSON file")

# Process
request_data = None
if request.POST:
request_data = request.POST
if request_data:
if "paste" in request_data:
form_name = "text_form"
else:
form_name = "upload_form"
forms[form_name] = JSON_FORM_CLASSES[form_name](request_data, request.FILES)
form = forms[form_name]
if form.is_valid():
supplied_data = SuppliedData()
supplied_data.format = "json"
supplied_data.save()

supplied_data.save_file(request.FILES["file_upload"])

return HttpResponseRedirect(supplied_data.get_absolute_url())
# Extra Validation
if form_name == "upload_form":
if (
not request.FILES["file_upload"].content_type
in settings.ALLOWED_JSON_CONTENT_TYPES
):
form.add_error(
"file_upload", "This does not appear to be a JSON file"
)
if not [
e
for e in settings.ALLOWED_JSON_EXTENSIONS
if str(request.FILES["file_upload"].name).lower().endswith(e)
]:
form.add_error(
"file_upload", "This does not appear to be a JSON file"
)
elif form_name == "text_form":
pass # TODO

# Process
if form.is_valid():
supplied_data = SuppliedData()
supplied_data.format = "json"
supplied_data.save()

if form_name == "upload_form":
supplied_data.save_file(request.FILES["file_upload"])
elif form_name == "text_form":
supplied_data.save_file_contents(
"input.json",
form.cleaned_data["paste"],
"application/json",
None,
)

return HttpResponseRedirect(supplied_data.get_absolute_url())

return render(request, "libcoveweb2/new_json.html", {"forms": forms})

Expand Down

0 comments on commit bfd3172

Please sign in to comment.