Пример #1
0
def search(request):
    documents = []
    q = None
    is_searching = False

    if 'q' in request.GET:
        form = SearchForm(request.GET)
        if form.is_valid():
            q = form.cleaned_data['q']

            is_searching = True
            documents = Document.search(q, results_per_page=20, prefetch_tags=True)
    else:
        form = SearchForm()

    if request.is_ajax():
        return render(request, "wagtaildocs/documents/results.html", {
            'documents': documents,
            'is_searching': is_searching,
            'search_query': q
        })
    else:
        return render(request, "wagtaildocs/documents/index.html", {
            'form': form,
            'documents': documents,
            'is_searching': True,
            'search_query': q,
            'popular_tags': Document.popular_tags(),
        })
Пример #2
0
def index(request):
    # Get documents
    documents = Document.objects.all()

    # Ordering
    if 'ordering' in request.GET and request.GET['ordering'] in ['title', '-created_at']:
        ordering = request.GET['ordering']
    else:
        ordering = '-created_at'
    documents = documents.order_by(ordering)

    # Permissions
    if not request.user.has_perm('wagtaildocs.change_document'):
        # restrict to the user's own documents
        documents = documents.filter(uploaded_by_user=request.user)

    # Search
    query_string = None
    if 'q' in request.GET:
        form = SearchForm(request.GET, placeholder=_("Search documents"))
        if form.is_valid():
            query_string = form.cleaned_data['q']
            if not request.user.has_perm('wagtaildocs.change_document'):
                # restrict to the user's own documents
                documents = Document.search(query_string, filters={'uploaded_by_user_id': request.user.id})
            else:
                documents = Document.search(query_string)
    else:
        form = SearchForm(placeholder=_("Search documents"))

    # Pagination
    p = request.GET.get('p', 1)
    paginator = Paginator(documents, 20)

    try:
        documents = paginator.page(p)
    except PageNotAnInteger:
        documents = paginator.page(1)
    except EmptyPage:
        documents = paginator.page(paginator.num_pages)

    # Create response
    if request.is_ajax():
        return render(request, 'wagtaildocs/documents/results.html', {
            'ordering': ordering,
            'documents': documents,
            'query_string': query_string,
            'is_searching': bool(query_string),
        })
    else:
        return render(request, 'wagtaildocs/documents/index.html', {
            'ordering': ordering,
            'documents': documents,
            'query_string': query_string,
            'is_searching': bool(query_string),

            'search_form': form,
            'popular_tags': Document.popular_tags(),
        })
Пример #3
0
def index(request):
    # Get documents
    documents = Document.objects.all()

    # Ordering
    if 'ordering' in request.GET and request.GET['ordering'] in ['title', '-created_at']:
        ordering = request.GET['ordering']
    else:
        ordering = '-created_at'
    documents = documents.order_by(ordering)

    # Permissions
    if not request.user.has_perm('wagtaildocs.change_document'):
        # restrict to the user's own documents
        documents = documents.filter(uploaded_by_user=request.user)

    # Search
    query_string = None
    if 'q' in request.GET:
        form = SearchForm(request.GET, placeholder_suffix='documents')
        if form.is_valid():
            query_string = form.cleaned_data['q']
            if not request.user.has_perm('wagtaildocs.change_document'):
                # restrict to the user's own documents
                documents = Document.search(query_string, filters={'uploaded_by_user_id': request.user.id})
            else:
                documents = Document.search(query_string)
    else:
        form = SearchForm(placeholder_suffix='documents')

    # Pagination
    p = request.GET.get('p', 1)
    paginator = Paginator(documents, 20)

    try:
        documents = paginator.page(p)
    except PageNotAnInteger:
        documents = paginator.page(1)
    except EmptyPage:
        documents = paginator.page(paginator.num_pages)

    # Create response
    if request.is_ajax():
        return render(request, 'wagtaildocs/documents/results.html', {
            'ordering': ordering,
            'documents': documents,
            'query_string': query_string,
            'is_searching': bool(query_string),
        })
    else:
        return render(request, 'wagtaildocs/documents/index.html', {
            'ordering': ordering,
            'documents': documents,
            'query_string': query_string,
            'is_searching': bool(query_string),

            'search_form': form,
            'popular_tags': Document.popular_tags(),
        })
Пример #4
0
 def _import_documents (self, pdf_dir):
     doc_dir = os.path.join(pdf_dir, 'documents')
     for data in document_data.values():
         title = data['title']
         filename = data['file']
         self.logger.debug('Creating document {}'.format(
             title.encode('utf-8')))
         doc = Document(title=title)
         with open(os.path.join(doc_dir, filename), 'rb') as fh:
             document_file = File(fh)
             doc.file.save(filename, document_file)
         doc.save()
Пример #5
0
 def _import_documents(self, pdf_dir):
     doc_dir = os.path.join(pdf_dir, 'documents')
     for data in document_data.values():
         title = data['title']
         filename = data['file']
         self.logger.debug('Creating document {}'.format(
             title.encode('utf-8')))
         doc = Document(title=title)
         with open(os.path.join(doc_dir, filename), 'rb') as fh:
             document_file = File(fh)
             doc.file.save(filename, document_file)
         doc.save()
Пример #6
0
    def setUp(self):
        self.document = Document(title="Test document")
        self.document_without_file = Document(title="Document without file")
        self.document.file.save(
            'example.txt',
            ContentFile("A boring example document")
        )
        self.image = CFGOVImage.objects.create(
            title='test',
            file=get_test_image_file()
        )
        self.rendition = self.image.get_rendition('original')

        CACHE_PURGED_URLS[:] = []
Пример #7
0
def chooser_upload(request):
    if request.POST:
        document = Document(uploaded_by_user=request.user)
        form = DocumentForm(request.POST, request.FILES, instance=document)

        if form.is_valid():
            form.save()

            # Reindex the document to make sure all tags are indexed
            for backend in get_search_backends():
                backend.add(document)

            document_json = json.dumps({
                'id': document.id,
                'title': document.title
            })
            return render_modal_workflow(
                request, None, 'wagtaildocs/chooser/document_chosen.js',
                {'document_json': document_json})
    else:
        form = DocumentForm()

    documents = Document.objects.order_by('title')

    return render_modal_workflow(request, 'wagtaildocs/chooser/chooser.html',
                                 'wagtaildocs/chooser/chooser.js', {
                                     'documents': documents,
                                     'uploadform': form
                                 })
Пример #8
0
def add(request):
    if request.POST:
        doc = Document(uploaded_by_user=request.user)
        form = DocumentForm(request.POST, request.FILES, instance=doc)
        if form.is_valid():
            form.save()

            # Reindex the document to make sure all tags are indexed
            for backend in get_search_backends():
                backend.add(doc)

            messages.success(request,
                             _("Document '{0}' added.").format(doc.title),
                             buttons=[
                                 messages.button(
                                     reverse('wagtaildocs:edit',
                                             args=(doc.id, )), _('Edit'))
                             ])
            return redirect('wagtaildocs:index')
        else:
            messages.error(request,
                           _("The document could not be saved due to errors."))
    else:
        form = DocumentForm()

    return render(request, "wagtaildocs/documents/add.html", {
        'form': form,
    })
Пример #9
0
    def setUp(self):
        self.document = Document(title="Test document")
        self.document.file.save('example.txt',
                                ContentFile("A boring example document"))
        self.image = CFGOVImage.objects.create(title='test',
                                               file=get_test_image_file())

        CACHE_PURGED_URLS[:] = []
Пример #10
0
def dummy_wagtail_doc(request):
    if not Collection.objects.exists():  # pragma: no cover
        Collection.add_root()

    doc = Document(title='hello')
    doc.file.save('foo.txt', ContentFile('foo', 'foo.txt'))
    doc.save()
    doc = Document.objects.get(pk=doc.pk)  # Reload to ensure the upload took

    def nuke():
        try:  # Try cleaning up so `/var/media` isn't full of foo
            doc.file.delete()
            doc.delete()
        except:  # pragma: no cover
            pass

    request.addfinalizer(nuke)
    return doc
Пример #11
0
    def import_work(self, works_page, publishers_page):
        """Imports a Work: gets the work name and heading; gets the order of
        impressions; walks through the work directory and creates an
        impression object for each PDF file."""
        work = Work()
        work.code = self._work_code

        if work.code.find('Opus') >= 0:
            work.has_opus = True

            try:
                opus_n = int(work.code.split()[1].strip())
            except ValueError:
                opus_n = 66

            work.is_posthumous = (opus_n >=
                                  settings.POSTHUMOUS_WORKS_WITH_OPUS)
            work.sort_order = opus_n
        else:
            work.has_opus = False
            work.is_posthumous = (work.code
                                  in settings.POSTHUMOUS_WORKS_WITHOUT_OPUS)
            work.sort_order = settings.ALL_WORKS_WITHOUT_OPUS.index(
                work.code) + 74
        self.logger.debug('Work sort order: {}'.format(work.sort_order))
        # Heading filename.
        try:
            heading_filename = glob.glob(
                os.path.join(self._work_path, '*.heading*.pdf'))[0]
        except IndexError:
            self.logger.error(
                'No heading file found; skipping work {0}'.format(work.code))
            return
        work.heading = self._import_heading(heading_filename)
        work.title = work.heading.split('  ')[0].strip()
        work.slug = safe_slugify(work.title, Work)

        # Create a Work PDF Document.
        document = Document(title=work.title)
        with open(heading_filename, 'rb') as fh:
            pdf_file = File(fh)
            document.file.save(os.path.basename(heading_filename), pdf_file)
        document.tags.add('work')
        work.pdf = document

        # gets the order of impressions
        self._order_of_impressions = self._import_order_of_impressions()
        self.logger.debug(self._order_of_impressions)
        works_page.add_child(instance=work)
        self._import_impressions(work, publishers_page)
Пример #12
0
def add(request):
    if request.POST:
        doc = Document(uploaded_by_user=request.user)
        form = DocumentForm(request.POST, request.FILES, instance=doc)
        if form.is_valid():
            form.save()
            messages.success(request, "Document '%s' added." % doc.title)
            return redirect('wagtaildocs_index')
        else:
            messages.error(request, "The document could not be saved due to errors.")
    else:
        form = DocumentForm()

    return render(request, "wagtaildocs/documents/add.html", {
        'form': form,
    })
Пример #13
0
def index(request):
    # Get documents
    documents = Document.objects.all()

    # Ordering
    if 'ordering' in request.GET and request.GET['ordering'] in ['title', '-created_at']:
        ordering = request.GET['ordering']
    else:
        ordering = '-created_at'
    documents = documents.order_by(ordering)

    # Permissions
    if not request.user.has_perm('wagtaildocs.change_document'):
        # restrict to the user's own documents
        documents = documents.filter(uploaded_by_user=request.user)

    # Search
    query_string = None
    if 'q' in request.GET:
        form = SearchForm(request.GET, placeholder=_("Search documents"))
        if form.is_valid():
            query_string = form.cleaned_data['q']
            documents = documents.search(query_string)
    else:
        form = SearchForm(placeholder=_("Search documents"))

    # Pagination
    paginator, documents = paginate(request, documents)

    # Create response
    if request.is_ajax():
        return render(request, 'wagtaildocs/documents/results.html', {
            'ordering': ordering,
            'documents': documents,
            'query_string': query_string,
            'is_searching': bool(query_string),
        })
    else:
        return render(request, 'wagtaildocs/documents/index.html', {
            'ordering': ordering,
            'documents': documents,
            'query_string': query_string,
            'is_searching': bool(query_string),

            'search_form': form,
            'popular_tags': Document.popular_tags(),
        })
def import_stps(stps_dir):
    logger.debug('Importing STPs')
    for root, dirs, files in os.walk(stps_dir):
        publisher_name = _clean_publisher_name(root, ' STP')
        logger.debug('publisher_name: {}'.format(
            publisher_name.encode('utf-8')))
        for filename in files:
            if filename.endswith('.pdf'):
                rubric = _clean_rubric(filename)
                document = Document(
                    title=u'{}; {}'.format(publisher_name, rubric))
                with open(os.path.join(root, filename), 'rb') as fh:
                    pdf_file = File(fh)
                    document.file.save(filename, pdf_file)
                document.tags.add('STP')
                stp = STP()
                stp.publisher_name = publisher_name
                stp.rubric = rubric
                stp.pdf = document
                stp.save()
                logger.debug('rubric: {}'.format(rubric.encode('utf-8')))
def import_adverts(advert_dir):
    logging.debug('Importing adverts')
    for root, dirs, files in os.walk(advert_dir):
        publisher_name = _clean_publisher_name(root, ' advt')
        logger.debug('publisher_name: {}'.format(
            publisher_name.encode('utf-8')))
        for filename in files:
            if filename.endswith('.pdf'):
                rubric = _clean_rubric(filename)
                document = Document(
                    title=u'{}; {}'.format(publisher_name, rubric))
                with open(os.path.join(root, filename), 'rb') as fh:
                    pdf_file = File(fh)
                    document.file.save(filename, pdf_file)
                document.tags.add('advert')
                advert = Advert()
                advert.publisher_name = publisher_name
                advert.rubric = rubric
                advert.pdf = document
                advert.save()
                logger.debug('rubric: {}'.format(rubric.encode('utf-8')))
Пример #16
0
    def _import_impression(self, work, publishers_page, f_path):
        # creates a new PDFParser to get the impression
        self.logger.debug('Parsing {}'.format(f_path))
        parser = PDFParser(f_path)
        code = parser.get_impression_code()
        if code:
            self.logger.debug('Impression: ' + code)

            # Create an Impression PDF Document.
            document = Document(title=code)
            with open(f_path, 'rb') as fh:
                pdf_file = File(fh)
                document.file.save(os.path.basename(f_path), pdf_file)
            document.tags.add('impression')

            # creates a new impression
            impression = Impression()
            impression.title = code
            impression.impression_title = parser.get_title()
            impression.content = parser.get_text_content()
            impression.pdf = document
            try:
                sort_order = self._order_of_impressions.index(code.lower())
            except Exception:
                self.logger.error(
                    u'{0} missing from order of impressions, which consists of: {1}'
                    .format(code, ', '.join(self._order_of_impressions)))
                sort_order = 999
            impression.sort_order = sort_order
            impression.slug = safe_slugify(impression.title, Impression)
            impression.comments = parser.get_comments()
            self._import_copies(impression, parser, code)
            publisher_code = impression.title.split('-')[-1]
            publisher = Publisher.objects.filter(title=publisher_code).first()
            if not publisher:
                publisher = Publisher(title=publisher_code)
                publisher.slug = slugify(publisher_code)
                publishers_page.add_child(instance=publisher)
            impression.publisher = publisher
            work.add_child(instance=impression)
Пример #17
0
def chooser(request):
    if request.user.has_perm('wagtaildocs.add_document'):
        uploadform = DocumentForm()
    else:
        uploadform = None

    documents = []

    q = None
    is_searching = False
    if 'q' in request.GET or 'p' in request.GET:
        searchform = SearchForm(request.GET)
        if searchform.is_valid():
            q = searchform.cleaned_data['q']

            # page number
            p = request.GET.get("p", 1)

            documents = Document.search(q, results_per_page=10, prefetch_tags=True)

            is_searching = True

        else:
            documents = Document.objects.order_by('-created_at')

            p = request.GET.get("p", 1)
            paginator = Paginator(documents, 10)

            try:
                documents = paginator.page(p)
            except PageNotAnInteger:
                documents = paginator.page(1)
            except EmptyPage:
                documents = paginator.page(paginator.num_pages)

            is_searching = False

        return render(request, "wagtaildocs/chooser/results.html", {
            'documents': documents,
            'query_string': q,
            'is_searching': is_searching,
        })
    else:
        searchform = SearchForm()

        documents = Document.objects.order_by('-created_at')
        p = request.GET.get("p", 1)
        paginator = Paginator(documents, 10)

        try:
            documents = paginator.page(p)
        except PageNotAnInteger:
            documents = paginator.page(1)
        except EmptyPage:
            documents = paginator.page(paginator.num_pages)

    return render_modal_workflow(request, 'wagtaildocs/chooser/chooser.html', 'wagtaildocs/chooser/chooser.js', {
        'documents': documents,
        'uploadform': uploadform,
        'searchform': searchform,
        'is_searching': False,
    })
Пример #18
0
def chooser(request):
    if request.user.has_perm("wagtaildocs.add_document"):
        uploadform = DocumentForm()
    else:
        uploadform = None

    documents = []

    q = None
    is_searching = False
    if "q" in request.GET or "p" in request.GET:
        searchform = SearchForm(request.GET)
        if searchform.is_valid():
            q = searchform.cleaned_data["q"]

            # page number
            p = request.GET.get("p", 1)

            documents = Document.search(q, results_per_page=10, prefetch_tags=True)

            is_searching = True

        else:
            documents = Document.objects.order_by("-created_at")

            p = request.GET.get("p", 1)
            paginator = Paginator(documents, 10)

            try:
                documents = paginator.page(p)
            except PageNotAnInteger:
                documents = paginator.page(1)
            except EmptyPage:
                documents = paginator.page(paginator.num_pages)

            is_searching = False

        return render(
            request,
            "wagtaildocs/chooser/results.html",
            {"documents": documents, "query_string": q, "is_searching": is_searching},
        )
    else:
        searchform = SearchForm()

        documents = Document.objects.order_by("-created_at")
        p = request.GET.get("p", 1)
        paginator = Paginator(documents, 10)

        try:
            documents = paginator.page(p)
        except PageNotAnInteger:
            documents = paginator.page(1)
        except EmptyPage:
            documents = paginator.page(paginator.num_pages)

    return render_modal_workflow(
        request,
        "wagtaildocs/chooser/chooser.html",
        "wagtaildocs/chooser/chooser.js",
        {"documents": documents, "uploadform": uploadform, "searchform": searchform, "is_searching": False},
    )
def import_library(file_path, index_page):
    logger.debug('Importing {}'.format(file_path))
    parser = PDFParser(file_path)
    content = parser.get_text_content()
    if not content:
        logger.debug('Found no content in the PDF')
        return

    # gets the library heading
    heading = content.split('\n')[0]
    heading_parts = heading.split('   ')

    # gets the library code, the first value before the spaces
    if len(heading_parts) < 2:
        code = content.split('\n')[1]
    else:
        code = heading_parts[0].strip()

    # the information after the spaces
    metadata = heading_parts[-1]
    metadata_parts = metadata.split(',')

    # the country name is the first element in the metadata
    country_name = metadata_parts[0].strip()
    # and the city the second
    city_name = metadata_parts[1].strip()

    # if the county is usa
    if country_name == 'United States of America':
        # the library name is after the state
        name = ','.join(metadata_parts[3:]).strip()
    else:
        # otherwise the library name comes after the city
        name = ','.join(metadata_parts[2:]).strip()

    logger.debug(u'{0} {1} {2} {3}'.format(code, country_name, city_name,
                                           name))

    # gets the country
    country = Country.objects.filter(name=country_name).first()

    # if the country is not in the db yet
    if not country:
        # creates a new country object
        country = Country(name=country_name)
        country.save()

    # gets the city
    city = City.objects.filter(name=city_name, country=country).first()

    # if the city is not in the db yet
    if not city:
        # creates a new city object
        city = City(country=country, name=city_name)
        city.save()

    # gets the library
    slug = slugify(code)[:50]
    # Use the slug for lookups, because there are case differences in
    # some references that are meant to be the same.
    library = Library.objects.filter(slug=slug).first()

    # if the library is not in the db
    if not library:
        # creates a new library
        library = Library(title=code, city=city, name=name)
        library.slug = slug
        index_page.add_child(instance=library)
    else:
        logger.warning('Duplicate library')
        # otherwise update the library
        library.city = city
        library.name = name

    # Create a Library PDF Document.
    document = Document(title=code)
    with open(file_path, 'rb') as fh:
        pdf_file = File(fh)
        document.file.save(os.path.basename(file_path), pdf_file)
    document.tags.add('library')
    library.pdf = document
    library.save()
Пример #20
0
def chooser(request):
    if request.user.has_perm('wagtaildocs.add_document'):
        uploadform = DocumentForm()
    else:
        uploadform = None

    documents = []

    q = None
    is_searching = False
    if 'q' in request.GET or 'p' in request.GET:
        searchform = SearchForm(request.GET)
        if searchform.is_valid():
            q = searchform.cleaned_data['q']

            # page number
            p = request.GET.get("p", 1)

            documents = Document.search(q,
                                        results_per_page=10,
                                        prefetch_tags=True)

            is_searching = True

        else:
            documents = Document.objects.order_by('-created_at')

            p = request.GET.get("p", 1)
            paginator = Paginator(documents, 10)

            try:
                documents = paginator.page(p)
            except PageNotAnInteger:
                documents = paginator.page(1)
            except EmptyPage:
                documents = paginator.page(paginator.num_pages)

            is_searching = False

        return render(
            request, "wagtaildocs/chooser/results.html", {
                'documents': documents,
                'search_query': q,
                'is_searching': is_searching,
            })
    else:
        searchform = SearchForm()

        documents = Document.objects.order_by('-created_at')
        p = request.GET.get("p", 1)
        paginator = Paginator(documents, 10)

        try:
            documents = paginator.page(p)
        except PageNotAnInteger:
            documents = paginator.page(1)
        except EmptyPage:
            documents = paginator.page(paginator.num_pages)

    return render_modal_workflow(
        request, 'wagtaildocs/chooser/chooser.html',
        'wagtaildocs/chooser/chooser.js', {
            'documents': documents,
            'uploadform': uploadform,
            'searchform': searchform,
            'is_searching': False,
        })
Пример #21
0
def index(request):
    # Get documents
    documents = Document.objects.all()

    # Ordering
    if "ordering" in request.GET and request.GET["ordering"] in ["title", "-created_at"]:
        ordering = request.GET["ordering"]
    else:
        ordering = "-created_at"
    documents = documents.order_by(ordering)

    # Permissions
    if not request.user.has_perm("wagtaildocs.change_document"):
        # restrict to the user's own documents
        documents = documents.filter(uploaded_by_user=request.user)

    # Search
    query_string = None
    if "q" in request.GET:
        form = SearchForm(request.GET, placeholder=_("Search documents"))
        if form.is_valid():
            query_string = form.cleaned_data["q"]
            if not request.user.has_perm("wagtaildocs.change_document"):
                # restrict to the user's own documents
                documents = Document.search(query_string, filters={"uploaded_by_user_id": request.user.id})
            else:
                documents = Document.search(query_string)
    else:
        form = SearchForm(placeholder=_("Search documents"))

    # Pagination
    p = request.GET.get("p", 1)
    paginator = Paginator(documents, 20)

    try:
        documents = paginator.page(p)
    except PageNotAnInteger:
        documents = paginator.page(1)
    except EmptyPage:
        documents = paginator.page(paginator.num_pages)

    # Create response
    if request.is_ajax():
        return render(
            request,
            "wagtaildocs/documents/results.html",
            {
                "ordering": ordering,
                "documents": documents,
                "query_string": query_string,
                "is_searching": bool(query_string),
            },
        )
    else:
        return render(
            request,
            "wagtaildocs/documents/index.html",
            {
                "ordering": ordering,
                "documents": documents,
                "query_string": query_string,
                "is_searching": bool(query_string),
                "search_form": form,
                "popular_tags": Document.popular_tags(),
            },
        )
Пример #22
0
def savefiles(msg, simulate):
    """
    Extract parts from  msg (which is an email.message_from_string(str) instance)
    and send them to the database.
    NOTES:
    - uses only the first found email address to assume recipient

    TODO stuff
    - reject if From: is empty
    """
    part_counter = 1
    subject = get_subject(msg)
    tos = get_recipient(msg)
    msg_id = msg.get('message-id', '')
    froms = msg.get_all('from', [])
    print(subject, tos, froms)
    p = re.compile('([\w\.\-]+@[\w\.\-]+)')
    try:  # May raise in some cases IndexError: list index out of range
        matches = p.findall(froms[0])
        print("MATS", matches[0])
        sender_nick = matches[0].split(".")[0].title()  # Use all before first '.'
    except:
        print("ERROR: No From header %s" % (msg_id))
        return False
    if len(tos) == 0:
        print("ERROR: No Tos found %s" % (msg_id))
        return False
    p = re.compile('([\w]+)\.([\w]+)@')  # e.g. [email protected]
    matches = p.findall(tos[0])
    if len(matches) > 0:
        username = matches[0][0].title()
        key = matches[0][1].lower()
    else:
        print("ERROR: No user.authkey found from %s %s" % (tos[0], msg_id))
        # return False
    # try:
    #     user = User.objects.get(username=username.lower())
    # except User.DoesNotExist:
    #     print("User.DoesNotExist !", username)
    #     log.warning("User.DoesNotExist: '%s'" % username)
    #     return False

    parts_not_to_save = ["multipart/mixed",
                         "multipart/alternative",
                         "multipart/related",
                         "text/plain",
                         ]
    if simulate:  # Print lots of debug stuff
        print('=========\nMetadata:\n=========')
        print('''Subject: %s\nUsername: \nFrom: %s\nTo: %s\nM-id: %s\n''' % (
                subject, ','.join(froms), ','.join(tos), msg_id))
        print('=========\nParts:\n=========')
    saved_parts = 0
    log.info("Walking through message parts")
    bodies = []
    index_page = NewssheetIndexPage.objects.live()[0]
    # print(index_page)
    page = NewssheetPage(
        title=subject,
        date=datetime.datetime.today(),
        live=True,
    )
    page.slug = slugify(page.title)
    # print(dir(page))
    # page.save()
    page.unpublish(commit=False)
    newssheet = index_page.add_child(instance=page)
    newssheet.save_revision(submitted_for_moderation=True)
    # newssheet.unpublish()
    print(newssheet)
    for part in msg.walk():
        part_content_type = part.get_content_type()
        filename, filedata = handle_part(part)
        if part_content_type == "text/plain":
            pl = part.get_payload(decode=True)
            pl = pl.decode()  #(encoding=word[1])
            bodies.append(pl)
            # if part['Content-Transfer-Encoding'] == 'base64':
            #     bodies.append(pl)
            # elif part['Content-Transfer-Encoding'] == 'quoted-printable':
            #     bodies.append(pl)
            # elif part['Content-Transfer-Encoding'] == 'jotain muuta':
            #     bodies.append(pl)
        print(bodies)
        if part_content_type in parts_not_to_save or filename is None:
            # print "NOT SAVING", part_content_type
            log_msg = "Not saving '%s', filename '%s'." % (part_content_type, filename)
            log.info(log_msg)
            if simulate: print(log_msg)  # Print lots of debug stuff
            continue
            #print filedata, type(filedata), len(filedata)
        if filedata is None or len(filedata) == 0:
            log_msg = "Not saving '%s', filename '%s', file has no data" % (part_content_type, filename)
            log.warning(log_msg)
            if simulate:
                print(log_msg)  # Print lots of debug stuff
            continue
        log_msg = u'Saving: %s (%s)' % (filename, part_content_type)
        log.info(log_msg)
        if simulate:
            print(log_msg)  # Print lots of debug stuff
        # Saving attachemnts
        from wagtail.wagtaildocs.models import Document
        attachment = Document(title=filename)
        attachment.file.save(filename, ContentFile(filedata))
        attachment.save()
        na = NewssheetPageAttachments(attachment=attachment, text=filename,
                                      page=newssheet)
        na.save()
        # newssheet.attachments.add(na)
    newssheet.body = '\n\n'.join(bodies)
    newssheet.save_revision()
    return saved_parts
Пример #23
0
def index(request):

    q = None
    p = request.GET.get("p", 1)
    is_searching = False

    if 'q' in request.GET:
        form = SearchForm(request.GET, placeholder_suffix="documents")
        if form.is_valid():
            q = form.cleaned_data['q']

            is_searching = True
            if not request.user.has_perm('wagtaildocs.change_document'):
                # restrict to the user's own documents
                documents = Document.search(q, results_per_page=20, page=p, filters={'uploaded_by_user_id': request.user.id})
            else:
                documents = Document.search(q, results_per_page=20, page=p)
        else:
            documents = Document.objects.order_by('-created_at')
            if not request.user.has_perm('wagtaildocs.change_document'):
                # restrict to the user's own documents
                documents = documents.filter(uploaded_by_user=request.user)
    else:
        documents = Document.objects.order_by('-created_at')
        if not request.user.has_perm('wagtaildocs.change_document'):
            # restrict to the user's own documents
            documents = documents.filter(uploaded_by_user=request.user)
        form = SearchForm(placeholder_suffix="documents")

    if 'ordering' in request.GET:
        ordering = request.GET['ordering']

        if ordering in ['title', '-created_at']:
            if ordering != '-created_at':
                documents = documents.order_by(ordering)
    else:
        ordering = '-created_at'

    if not is_searching:
        paginator = Paginator(documents, 20)

        try:
            documents = paginator.page(p)
        except PageNotAnInteger:
            documents = paginator.page(1)
        except EmptyPage:
            documents = paginator.page(paginator.num_pages)

    if request.is_ajax():
        return render(request, "wagtaildocs/documents/results.html", {
            'ordering': ordering,
            'documents': documents,
            'is_searching': is_searching,
            'search_query': q,
        })
    else:
        return render(request, "wagtaildocs/documents/index.html", {
            'ordering': ordering,
            'search_form': form,
            'documents': documents,
            'popular_tags': Document.popular_tags(),
            'is_searching': is_searching,
            'search_query': q,
        })