Add error when required field is missing in .toml file
This commit is contained in:
parent
c259c5722e
commit
564a7e8254
@ -4,19 +4,24 @@ from bootstrap_modal_forms.mixins import PopRequestMixin, CreateUpdateAjaxMixin
|
||||
from django import forms
|
||||
from .models import Version
|
||||
|
||||
|
||||
class CustomUserCreationForm(PopRequestMixin, CreateUpdateAjaxMixin,
|
||||
UserCreationForm):
|
||||
class Meta:
|
||||
model = User
|
||||
fields = ['username', 'email', 'password1', 'password2']
|
||||
|
||||
|
||||
class CustomAuthenticationForm(AuthenticationForm):
|
||||
class Meta:
|
||||
model = User
|
||||
fields = ['username', 'password']
|
||||
|
||||
|
||||
class FileFieldForm(forms.Form):
|
||||
file = forms.FileField(widget=forms.ClearableFileInput(attrs={'multiple': True}))
|
||||
file = forms.FileField(
|
||||
widget=forms.ClearableFileInput(attrs={'multiple': True}))
|
||||
|
||||
|
||||
class FileFieldForm(forms.ModelForm):
|
||||
class Meta:
|
||||
@ -24,7 +29,7 @@ class FileFieldForm(forms.ModelForm):
|
||||
fields = ['file']
|
||||
widgets = {
|
||||
'file': forms.ClearableFileInput(
|
||||
attrs = {
|
||||
attrs={
|
||||
'multiple': True
|
||||
}
|
||||
)
|
||||
|
@ -16,7 +16,7 @@ def upload_path(instance, filename):
|
||||
|
||||
|
||||
class Module(models.Model):
|
||||
name = models.CharField(max_length=255)
|
||||
name = models.CharField(max_length=255, unique=True)
|
||||
desc = models.TextField(max_length=2048, null=True)
|
||||
creator = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
|
||||
|
||||
@ -34,6 +34,7 @@ class Version(models.Model):
|
||||
def __str__(self):
|
||||
return self.ver
|
||||
|
||||
|
||||
class Dependency(models.Model):
|
||||
version = models.ForeignKey(Version, on_delete=models.CASCADE)
|
||||
dep_module = models.CharField(max_length=255)
|
||||
|
@ -37,7 +37,7 @@
|
||||
{% endblock body %}
|
||||
|
||||
{% block extra_js %}
|
||||
<script type="text/javascript">
|
||||
<script type="text/javascript">
|
||||
$(function() {
|
||||
$("#registerLink").modalForm({
|
||||
formURL: "{% url 'signup_modal' %}"
|
||||
@ -46,5 +46,5 @@
|
||||
formURL: "{% url 'login_modal' %}"
|
||||
});
|
||||
});
|
||||
</script>
|
||||
</script>
|
||||
{% endblock extra_js %}
|
||||
|
@ -8,8 +8,7 @@
|
||||
<!-- Search form -->
|
||||
<form class="form-inline d-flex justify-content-center md-form form-sm mt-0">
|
||||
<i class="fa fa-search" aria-hidden="true"></i>
|
||||
<input class="form-control ml-3 w-75" type="text" placeholder="Search"
|
||||
aria-label="Search">
|
||||
<input class="form-control ml-3 w-75" type="text" placeholder="Search" aria-label="Search">
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
|
@ -8,8 +8,8 @@
|
||||
{% block body %}
|
||||
<main>
|
||||
<div class="container pt-5">
|
||||
<div class="alert alert-danger alert-dismissible fade show" id="errorZone" style="display:none;">
|
||||
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
|
||||
<div class="alert alert-danger alert-dismissible" id="errorZone" style="display:none;">
|
||||
<a href="#" class="close" onclick="$('.alert').hide();">×</a>
|
||||
</div>
|
||||
<form class="dropzone" enctype="multipart/form-data" method="post" id="multiFileUpload">
|
||||
<div class="fallback">
|
||||
@ -67,6 +67,7 @@ $('#multiFileUpload').dropzone({
|
||||
});
|
||||
this.on("error", function(file, message) {
|
||||
console.log(message);
|
||||
$('.alert').html('');
|
||||
$('#errorZone').append(message.error);
|
||||
$('#errorZone').show();
|
||||
});
|
||||
|
@ -31,6 +31,8 @@ class CustomLoginView(BSModalLoginView):
|
||||
extra_content = dict(success_url=reverse_lazy('index'))
|
||||
|
||||
|
||||
REQUIRED_FIELDS = ['name', 'description', 'version', 'bot_version']
|
||||
|
||||
class UploadView(LoginRequiredMixin, FormView):
|
||||
form_class = FileFieldForm
|
||||
template_name = 'store/upload.html'
|
||||
@ -46,6 +48,7 @@ class UploadView(LoginRequiredMixin, FormView):
|
||||
with open(zip_path, "wb+") as f: # Writing archive
|
||||
for chunk in file.chunks():
|
||||
f.write(chunk)
|
||||
# Check if uploaded file is a zip archive
|
||||
if magic.from_file(zip_path, mime=True) != 'application/zip':
|
||||
print(magic.from_file(zip_path))
|
||||
os.remove(zip_path)
|
||||
@ -56,6 +59,10 @@ class UploadView(LoginRequiredMixin, FormView):
|
||||
with open(os.path.join(extract_path, 'infos.toml'), 'r') as f:
|
||||
# Reading and parsing toml file
|
||||
module_info = toml.loads(f.read())
|
||||
for required_field in REQUIRED_FIELDS:
|
||||
if not required_field in module_info.keys():
|
||||
return JsonResponse({'error': f'Field <strong>{required_field}\
|
||||
</strong> is missing.'}, status=500)
|
||||
shutil.rmtree(extract_path)
|
||||
os.remove(zip_path)
|
||||
if Module.objects.filter(name=module_info['name'].lower(),
|
||||
|
Loading…
Reference in New Issue
Block a user