Ejemplo n.º 1
0
 def post(self, request, *args, **kwargs):
     files = request.FILES.getlist("files[]")
     for file in files:
         save_uploaded_file_to(file, path.join(settings.UPLOAD_DIR, str(self.problem.pk)),
                               filename=path.splitext(file.name)[0] + '.' + random_string(16),
                               keep_extension=True)
     return HttpResponse()
Ejemplo n.º 2
0
 def post(self, request, *args, **kwargs):
     uploadtype = request.POST.get('uploadtype')
     comment = request.POST.get('comment')[:20]
     noprint = request.POST.get('noprint') == 'on'
     manager = get_object_or_404(PrintManager, user=self.request.user)
     if uploadtype == "code":
         code = request.POST.get('code')
         if len(code) < 6 or len(code) > 65536:
             messages.error(
                 request, "Length of code is either too short or too long.")
             return redirect(request.path)
         p = manager.printcode_set.create(code=code,
                                          user=self.request.user,
                                          comment=comment)
     else:
         # print(request.POST)
         file = request.FILES['file']
         secret_key = random_string()
         save_uploaded_file_to(file, settings.GENERATE_DIR,
                               secret_key + ".pdf")
         p = manager.printcode_set.create(code='#',
                                          user=self.request.user,
                                          comment=comment,
                                          generated_pdf=secret_key)
     if noprint:
         p.status = 2
         p.pages = 0
         p.save()
     else:
         process_code(p)
     return redirect(request.path)
Ejemplo n.º 3
0
 def post(self, request, *args, **kwargs):
     if request.POST['type'] == 'manual':
         input = request.POST['input']
         output = request.POST['output']
         well_form = request.POST.get("wellForm") == "on"
         if well_form:
             input, output = well_form_text(input), well_form_text(output)
         if not input:
             raise ValueError('Input file cannot be empty')
         save_case(self.session, input.encode(), output.encode(), well_form=well_form)
     elif request.POST['type'] == 'upload':
         file = request.FILES['file']
         file_directory = '/tmp'
         file_path = save_uploaded_file_to(file, file_directory, filename=random_string(), keep_extension=True)
         process_uploaded_case(self.session, file_path)
         remove(file_path)
     elif request.POST['type'] == 'generate':
         generator = request.POST['generator']
         raw_param = request.POST['param']
         generate_input('Generate cases', self.session, generator, raw_param)
     elif request.POST['type'] == 'stress':
         generator = request.POST['generator']
         raw_param = request.POST['param']
         submission = request.POST['submission']
         time = int(request.POST['time']) * 60
         if time < 60 or time > 300:
             raise ValueError('Time not in range')
         stress('Stress test', self.session, generator, submission, raw_param, time)
     return HttpResponse()
Ejemplo n.º 4
0
def file_manager(request):
    def slugify(text):
        import re
        return re.sub(r'[ /"#!:]+', '_', text)

    if not is_admin_or_root(request.user):
        raise PermissionDenied
    if request.method == 'POST':
        try:
            file = request.FILES['file']
            save_uploaded_file_to(file,
                                  settings.UPLOAD_DIR,
                                  filename=slugify(file.name))
        except Exception as e:
            raise PermissionDenied(repr(e))
    return render(
        request,
        'filemanager.jinja2',
        context={
            'file_list':
            list(
                map(
                    lambda x: {
                        'name':
                        x,
                        'modified_time':
                        datetime.fromtimestamp(
                            path.getmtime(path.join(settings.UPLOAD_DIR, x))).
                        strftime(settings.DATETIME_FORMAT_TEMPLATE),
                        'size':
                        str(
                            path.getsize(path.join(settings.UPLOAD_DIR, x)) //
                            1024) + "K"
                    },
                    filter(
                        lambda x: path.isfile(path.join(
                            settings.UPLOAD_DIR, x)),
                        listdir(settings.UPLOAD_DIR))))
        })
Ejemplo n.º 5
0
 def handle_upload(self, request):
     file = request.FILES['file']
     save_uploaded_file_to(file,
                           self.root,
                           filename=self.slugify(file.name))