예제 #1
0
def process_decipher_in_post(post, post_content):
    # load html doc in to readable python objects
    soup = load_html_doc(post_content)

    # will only process div element with class 'decipher'
    deciphers = get_tags(soup, 'span.decipher')

    # decipher ids from content
    content_decipher_ids = []

    # create or update
    if deciphers:
        for decipher in deciphers:
            # To avoid IntegrityError (null value)
            if decipher.string:
                try:
                    # if key 'id' doesn't exist, maybe add to database
                    decipher_id = int(decipher['id'][11:])

                    instance = Decipher.objects.get(id=decipher_id)
                    # update decipher
                    instance.hidden_text = decipher.string
                    instance.save()

                    # if id was successfully parsed to int and is
                    # existing append to content_decipher_ids list
                    # so they would not be deleted later
                    content_decipher_ids.append(decipher_id)
                except (KeyError, ValueError, Decipher.DoesNotExist):
                    # save decipher to db
                    instance = Decipher.objects \
                                            .create(post=post, hidden_text=decipher.string)

                    decipher_name = 'decipherme-' + str(instance.id)
                    assign_attr_to_tag(tag=decipher,
                                       target_attr='id',
                                       attr_val=decipher_name)

                    # update decipher name once saved
                    instance.name = decipher_name
                    instance.save()

                    # also append newly saved decipher's id so
                    # it would not be deleted later
                    content_decipher_ids.append(instance.id)
            else:
                decipher.decompose()

    # get existing id of deciphers from given post
    post_decipher_ids = Decipher.objects.filter(post=post).values_list(
        'id', flat=True)

    # delete all deciphers from db that are no longer found
    # on content (from content_decipher_ids)
    for post_decipher_id in post_decipher_ids:
        if post_decipher_id not in content_decipher_ids:
            instance = Decipher.objects.get(id=post_decipher_id)
            instance.delete()

    return soup.prettify(formatter="html5")
예제 #2
0
    def short_content_for_list(self):
        # Slice content to 500 characters
        soup = load_html_doc(self.content)
        deciphers = soup.select('span.decipher')
        for decipher in deciphers:
            decipher.decompose()

        return soup.get_text()[:200]
예제 #3
0
    def sanitized_content(self):
        soup = load_html_doc(self.content)

        # get all span.decipher elements
        # prepended with underscore as a marker that is is
        # an HTML element, not from database
        _deciphers = soup.select('span.decipher')
        for _decipher in _deciphers:
            decipher_id = _decipher.get('id', '')
            decipher_classes = _decipher.get('class', [])

            if decipher_id:
                # get decipher from db
                decipher = self.deciphers.get(name=decipher_id)

                # create div element tag for tooltip
                div_tooltip_container = soup.new_tag('div')
                div_tooltip_container['id'] = "tooltip-" + decipher.name
                div_tooltip_container['class'] = decipher_classes + [
                    'tooltip-container'
                ]
                div_tooltip_container['data-target'] = '#' + decipher.name

                # create form
                decipher_form = soup.new_tag('form')
                decipher_form['class'] = ['notify-form', 'decipher-form']
                decipher_form['data-checkcode-url'] = reverse(
                    'api:decipher-check-code', args=[str(decipher.id)])

                # create div element tag for notify / pop up message
                div_notify_container = soup.new_tag('div')
                div_notify_container['class'] = ['notify-container']

                # create icon element for notify close
                close_notify_container = soup.new_tag('i')
                close_notify_container['class'] = [
                    'fas', 'fa-times', 'decipher-close'
                ]

                div_notify_container.append(close_notify_container)

                # create div row for clue
                decipher_form_row = soup.new_tag('div')
                decipher_form_row['class'] = ['notify-form-row']

                decipher_clue = soup.new_tag('p')
                decipher_clue_label = soup.new_tag('span')
                decipher_clue_label.string = "clue:"
                decipher_clue.append(decipher_clue_label)
                decipher_clue.append(decipher.clue)

                # append row clue to form
                decipher_form_row.append(decipher_clue)
                decipher_form.append(decipher_form_row)

                if decipher.clue_photo_url:
                    # create new row for clue_photo
                    decipher_form_row = soup.new_tag('div')
                    decipher_form_row['class'] = ['notify-form-row']

                    # create img element tag
                    decipher_clue_photo = soup.new_tag('img')
                    decipher_clue_photo['class'] = ['decipher-clue-photo']
                    decipher_clue_photo['src'] = decipher.clue_photo_url

                    # append to new div form row
                    decipher_form_row.append(decipher_clue_photo)
                    # append div form row to form
                    decipher_form.append(decipher_form_row)

                decipher_form_row = soup.new_tag('div')
                decipher_form_row['class'] = ['notify-form-row']

                decipher_code = soup.new_tag('input')
                decipher_code['type'] = "text"
                decipher_code['name'] = "code"

                decipher_form_row.append(decipher_code)
                decipher_form.append(decipher_form_row)

                decipher_form_submit = soup.new_tag('button')
                decipher_form_submit['type'] = "submit"
                decipher_form_submit.string = "Enter"

                # input[type="text"] and button on same row
                decipher_form_row.append(decipher_form_submit)

                # append form.notify-form to div.notify-container
                div_notify_container.append(decipher_form)

                # append the div.notify-container to div.tooltip-container
                div_tooltip_container.append(div_notify_container)

                # change span.decipher with div.tooltip-container.decipher
                _decipher.replace_with(div_tooltip_container)

        #print("SOUPPPP",soup)
        return soup.prettify(formatter="html5")