Add Uploadview and get_progress

This commit is contained in:
PCoder 2021-07-28 17:17:11 +05:30
parent 10de2b5df6
commit 3663e41cd3

View file

@ -153,6 +153,66 @@ class ConfigurationPageView(TemplateView):
return HttpResponsePermanentRedirect(reverse('admin:index')) return HttpResponsePermanentRedirect(reverse('admin:index'))
class UploadView(TemplateView):
template_name = "app/admin/config.html"
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
return context
def get(self, request):
messages.add_message(request, messages.ERROR, 'Please select a valid file')
return HttpResponsePermanentRedirect(reverse('app:config'))
def post(self, request, **kwargs):
if 'datafile' in request.FILES:
fs = request.FILES['datafile']
fs_name = secure_filename(fs.name)
fs_path = default_storage.save(fs_name, ContentFile(fs.read()))
print('Uploading: %s' % fs_path)
# Validation
fmt = None
if fs_name.endswith('.csv'):
with open(fs_path, 'rt', encoding='utf-8', errors='ignore') as csvfile:
datareader = csv.DictReader(csvfile)
datalist = list(datareader)
fmt = detect_dataformat(datalist[0])
elif fs_name.endswith('.geojson'):
with open(fs_path, 'rt', encoding='utf-8', errors='ignore') as jsonfile:
jsondata = json.load(jsonfile)
fmt = detect_dataformat(jsondata['features'][0]['properties'])
# Loading
if fmt is not None:
fs_target = get_datafile(fmt)
move(fs_path, fs_target)
messages.add_message(request, message="Uploaded new data file %s" % fmt['filename'],
level=messages.SUCCESS)
else:
messages.add_message(request, message="Could not validate data format!", level=messages.ERROR)
else:
messages.add_message(request, message="Please select a valid file", level=messages.ERROR)
return HttpResponsePermanentRedirect(reverse('config'))
def get_progress(request):
global c_progress
global c_filename
def generate():
while 1:
p = str(100*c_progress)
yield 'data: { "p":'+p+',"f":"'+c_filename+'"}\n\n'
time.sleep(1.0)
if c_filename == "":
return HttpResponse("{}", content_type='text/event-stream')
response = HttpResponse(generate(), content_type='text/event-stream')
response['X-Accel-Buffering'] = 'no'
return response
def send_from_file(request, filename): def send_from_file(request, filename):
request_for = '' request_for = ''
if request.path.startswith('/geodata'): if request.path.startswith('/geodata'):