Exemple #1
0
 def post(self, request, pk=None):
     #pk = self.request.session.get('archive',None)
     ar_id = request.POST.get('archive_id', None)
     obj = get_object_or_404(Archive, pk=ar_id)
     form = ArchiveForm(request.POST, request.FILES, instance=obj)
     #files = request.FILES.getlist('files')
     if form.is_valid():
         obj = form.save(commit=False)
         obj.username = self.request.user.username
         obj.add_time = timezone.now()
         obj.save()
         return redirect(reverse('archive:detail', args=[ar_id]))
     else:
         return render(request, 'archive/archive_edit.html', locals())
Exemple #2
0
    def post(self, request):
        if request.session.get('has_commented', False):
            return HttpResponse("You've already commented.")

        form = ArchiveForm(request.POST, request.FILES)
        if form.is_valid():
            new = form.save(commit=False)
            new.username = self.request.user.username
            new.save()

            request.session['has_commented'] = True

            return redirect(reverse('archive:edit',
                                    args=[str(new.archive_id)]))

        return render(request, 'archivearchive_add.html', locals())
Exemple #3
0
 def assemble_archive(cls, username, archive_id):
     """
     :param username:
     :param archive_id:
     :return: read in the parts in cache directory and write all bytes in a single swoop to the archive directory
     """
     cache_dir = os.path.join(MEDIA_ROOT, 'cache', username, archive_id)
     archive_dir = os.path.join(MEDIA_ROOT, 'archives', username,
                                archive_id)
     #   Delete archive_dir and remake it
     shutil.rmtree(path=archive_dir)
     os.makedirs(archive_dir)
     #   Sort the file parts numerically by the index
     file_part_names = os.listdir(cache_dir)
     file_part_names.sort(key=lambda x: int(x))
     #   Find the original file name
     archive: Archive = Archive.objects.get(pk=archive_id)
     archive_file_name = os.path.basename(archive.archive_file.name)
     archive_file_path = os.path.join(archive_dir, archive_file_name)
     #   Start writing in batches:
     with open(archive_file_path, 'ab') as f:
         for file_part_name in file_part_names:
             file_part_path = os.path.join(cache_dir, file_part_name)
             with open(file_part_path, 'rb') as p:
                 f.write(p.read())
     #   Confirm the checksum
     written_checksum = ArchiveForm.get_file_checksum(
         file_path=archive_file_path)
     if written_checksum == archive.archive_file_checksum:
         print(f"Successfully assembled archive at {archive_file_path}")
         archive.cached = True
         archive.save()
         shutil.rmtree(cache_dir)
     else:
         print(f"Oh oh something went wrong")
Exemple #4
0
    def check_cache_health(cls, archive_cache_dir: str,
                           archive_id: str) -> bool:
        """
        :param archive_cache_dir: an archive's cache directory
        :param archive_id:
        :return: True if and only if the all parts are present and are in good health
        """
        archive = Archive.objects.get(pk=archive_id)
        archive_parts_meta = ArchivePartMeta.objects.filter(archive=archive)
        ready_for_assembly = True
        for archive_part_meta in archive_parts_meta:
            cache_part_file_path = os.path.join(
                archive_cache_dir, str(archive_part_meta.part_index))
            if not (os.path.exists(cache_part_file_path)
                    and os.path.isfile(cache_part_file_path)):
                #   If the desired path doesn't point to an existing file, then the archive is not ready for assembly
                ready_for_assembly = False
            else:
                cache_part_file_checksum = ArchiveForm.get_file_checksum(
                    file_path=cache_part_file_path)
                if cache_part_file_checksum != archive_part_meta.part_checksum:
                    #   If the file part's checksum does not check out, then remove the file part
                    print(f"Corrupted file part at {cache_part_file_path}")
                    os.remove(cache_part_file_path)
                    archive_part_meta.cached = False
                else:
                    archive_part_meta.cached = True
                archive_part_meta.save()

        return ready_for_assembly
Exemple #5
0
def index(request):
    data = {}
    if request.method == 'GET':
        data['form'] = ArchiveForm()
        if request.GET.get('archive_from') and request.GET.get('archive_to'):
            data['from'] = request.GET['archive_from']
            data['to'] = request.GET['archive_to']
        return render(request, 'archive/index.html', data)
    return HttpResponse(status=405)
Exemple #6
0
    def get(self, request, pk=None):
        obj = get_object_or_404(Archive, pk=pk)

        # 对身份进行判断,只有发放人或工艺人员才可以修改
        can_upbom, can_upfile = can_edit_archive(obj, request.user)

        if can_upbom or can_upfile:
            form = ArchiveForm(instance=obj)
            return render(request, 'archive/archive_edit.html', locals())
        else:
            title = 'Forbidden'
            info = '无此权限进行编辑'
            return render(request, 'info.html', locals())
Exemple #7
0
 def get(self, request):
     form = ArchiveForm()
     request.session['has_commented'] = False
     return render(request, 'archivearchive_add.html', locals())