Example #1
0
 def _transform_request_file(self):
     try:
         filename = get_import_file_path(self.request.GET.get("n"))
         if not os.path.isfile(filename):
             raise ValueError(_("%s is not a file") % self.request.GET.get("n"))
     except:
         raise Problem(_("File missing."))
     try:
         mode = "xls"
         if filename.endswith("xlsx"):
             mode = "xlsx"
         if filename.endswith("csv"):
             mode = "csv"
         return transform_file(mode, filename)
     except (Exception, RuntimeError) as e:
         messages.error(self.request, e)
Example #2
0
    def post(self, request, *args, **kwargs):
        file = self.request.FILES["file"]
        basename, ext = os.path.splitext(file.name)

        import_name = "%s%s" % (hashlib.sha256(("%s" % datetime.now()).encode("utf-8")).hexdigest(), ext)
        full_path = get_import_file_path(import_name)
        if not os.path.isdir(os.path.dirname(full_path)):
            os.makedirs(os.path.dirname(full_path))

        with open(full_path, 'wb+') as destination:
            for chunk in file.chunks():
                destination.write(chunk)

        next_url = request.POST.get("next")
        importer = request.POST.get("importer")
        lang = request.POST.get("language")
        return redirect("%s?n=%s&importer=%s&lang=%s" % (next_url, import_name, importer, lang))