Example #1
0
def AJAX_Add_New_Link(request):
    from enterlink.media_functions import getYouTubeIdIfPresent
    # Get the POST variables
    pageSlug = request.POST['pageSlug']
    option = request.POST['group1']
    citeHTML = request.POST['citeHTML']
    URLComment = request.POST['nl_linkcomment']

    # Make sure an empty description was not provided
    placeholderPresent = any(placeholder in URLComment
                             for placeholder in PLACEHOLDER_LIST)
    if (placeholderPresent or URLComment.strip() == "" or URLComment is None
            or URLComment == "<br data-mce-bogus=\"1\">"):
        return HttpResponse("ERROR_NO_DESCRIPTION")
    else:
        pass

    URLComment = badLinkSanitizer(URLComment)

    # Get and format the UTC timestamp
    timestamp = datetime.datetime.utcnow()
    timestamp = pytz.utc.localize(timestamp).strftime(
        '%m/%d/%Y %I:%M:%S %p %Z')

    # Get the article object from the URL slug provided
    cleanedParamList = getTheArticleObject(pageSlug)
    pageSlug = cleanedParamList[0]
    articleObject = cleanedParamList[1]

    # Parse all the current citations for the article.
    # This will be used later for things such as finding duplicates and getting the citation number
    theSoup = BeautifulSoup(citeHTML, "html5lib")
    resultDictionary = {}
    parseTinyMCE_Citations(theSoup, resultDictionary)

    # Set up a blank dictionary for the new link
    newLinkDict = {
        "url": None,
        "thumb": None,
        "description": URLComment,
        "category": "NONE",
        "integer": None,
        "isSocial": False,
        "attr": None,
        "timestamp": timestamp,
        "mime": None,
        "attribution_url": None
    }

    # Create a list of all current citations
    citationList, urlList = [], []
    try:
        for citation in resultDictionary["CITATION_OBJECTS"]:
            citationList.append(int(citation["integer"]))
            urlList.append(citation["url"])
    except:
        pass

    # Calculate and set the citation number for the new link
    if len(citationList) == 0:
        citationInteger = 1
    else:
        try:
            citationInteger = max(citationList) + 1
        except:
            citationInteger = None
    newLinkDict["integer"] = citationInteger

    # Check if the new link is a file
    if option == 'id_file':
        # Get and set some variables
        theFile = request.FILES['file']

        # Add the file to the Amazon S3 bucket and get some information about it
        resultPack = addMediaImage(request=request,
                                   pageID=pageSlug,
                                   theFile=theFile,
                                   fileComment=URLComment,
                                   PLACEHOLDER_LIST=PLACEHOLDER_LIST,
                                   inputMime="EMPTY")

        # Update the new link dictionary with information about the file
        newLinkDict["url"] = resultPack["url"]
        newLinkDict["thumb"] = resultPack["thumb"]
        newLinkDict["mime"] = resultPack["mime"]
        newLinkDict["category"] = linkCategorizer(resultPack["url"],
                                                  resultPack["mime"])

        # Check for duplicate links
        if dupeLinkDetector(newLinkDict["url"], urlList):
            return HttpResponse("ERROR_ALREADY_EXISTS")

        # Decide where to put the new link. If it is an image, video, or YouTube link, put it in the media gallery.
        # Otherwise, put it in the normal citation list.
        if newLinkDict["category"] == "NONE":
            newLinkHTMLBlock = render_to_string(
                'enterlink/ajax_link_singlet.html', {'theLink': newLinkDict})
            return JsonResponse({
                "type": "NORMAL",
                "htmlblock": newLinkHTMLBlock
            })
        else:
            newLinkHTMLBlock = render_to_string(
                'enterlink/ajax_media_gallery_singlet.html',
                {'theLink': newLinkDict})
            return JsonResponse({
                "type": "MEDIA",
                "htmlblock": newLinkHTMLBlock
            })

    # Check if the new link is a URL
    elif option == 'id_linkurl':
        # Get the URL and check if it is a Wikipedia link.
        if request.POST['linkurl']:
            theURL = request.POST['linkurl']
            if ('wikipedia.org' in theURL):
                return HttpResponse("ERROR_WIKIPEDIA_LINK")
            else:
                pass
        else:
            return HttpResponse("ERROR_NO_URL")

        # Check for a duplicate link
        if dupeLinkDetector(theURL, urlList):
            return HttpResponse("ERROR_ALREADY_EXISTS")

        # Update the new link JSON data
        newLinkDict["url"] = theURL
        newLinkDict["isSocial"] = profileLinkTester(theURL)["isProfileLink"]

        # Check if the URL is a link to a media object
        isMedia = False
        try:
            # Ping the URL to see get the data and response headers
            mediaTest = requests.get(theURL,
                                     headers=REQUEST_HEADER,
                                     timeout=6,
                                     verify=False,
                                     stream=True)

            # See if the response header indicates it is a media item. If so, set isMedia to True
            listOfMedias = ["video", "image", "audio"]
            for mediaItem in listOfMedias:
                mediaName = u"%s/" % mediaItem
                if mediaName in mediaTest.headers['Content-Type']:
                    isMedia = True
                    break

            # Check for YouTube
            youtubeID = getYouTubeIdIfPresent(theURL)
            if youtubeID:
                isMedia = True

            # Analyze and process the URL if it is a media file
            if isMedia:
                # Process depending on media type
                if youtubeID:
                    newLinkDict[
                        "thumb"] = "https://i.ytimg.com/vi/%s/hqdefault.jpg" % youtubeID
                    newLinkDict["mime"] = "youtube"
                    newLinkDict["category"] = "YOUTUBE"
                else:
                    # Create an empty string buffer and fill it with the media file data
                    streamObject = StringIO.StringIO()
                    streamObject.write(
                        mediaTest.raw.read(decode_content=False))

                    # Create a file in memory from the media file
                    theFile = InMemoryUploadedFile(
                        streamObject, None,
                        theURL.split("?")[0].split("/")[-1],
                        mediaTest.headers['Content-Type'], streamObject.len,
                        None)

                    # Add the media file to the Amazon S3 bucket and analyze the media file
                    resultPack = addMediaImage(
                        request=request,
                        pageID=pageSlug,
                        theFile=theFile,
                        fileComment=URLComment,
                        PLACEHOLDER_LIST=PLACEHOLDER_LIST,
                        inputMime=mediaTest.headers['Content-Type'])

                    # Update the new link dictionary with info about the file
                    newLinkDict["thumb"] = resultPack["thumb"]
                    newLinkDict["mime"] = resultPack["mime"]
                    newLinkDict["category"] = linkCategorizer(
                        resultPack["url"], resultPack["mime"])

                # Render the new media object as an HTML block
                newLinkHTMLBlock = render_to_string(
                    'enterlink/ajax_media_gallery_singlet.html',
                    {'theLink': newLinkDict})
                return JsonResponse({
                    "type": "MEDIA",
                    "htmlblock": newLinkHTMLBlock
                })

        except Exception as e:
            print(str(e))
            print(
                "Is not media, or timeout. Considering link as normal url...")

        # Find the thumbnail for the new link web page, if it has one from the og:image, schema, etc.
        theThumbURL = fetchMetaThumbnail(request, theURL, articleObject.slug,
                                         articleObject.ipfs_hash_current[:10])
        newLinkDict["thumb"] = theThumbURL

        # Render the new link as an HTML block
        newLinkHTMLBlock = render_to_string('enterlink/ajax_link_singlet.html',
                                            {'theLink': newLinkDict})
        return JsonResponse({"type": "NORMAL", "htmlblock": newLinkHTMLBlock})
Example #2
0
def AJAX_Search(request, url_param):
    FETCH_LIMIT = 20
    resultDictionary = {}

    # See if the search request is for a citation
    # If so, create a list of citations in the article and construct the citation HTML superscript block (e.g. [24])
    # that is to be injected into the article
    if url_param == 'tinymce-cite-source':
        try:
            cited_by = request.GET.get('cited_by')
            citer_rank = request.GET.get('citer_rank')
            citer_is_verified = request.GET.get('citer_is_verified')
        except:
            cited_by = ""
            citer_rank = ""
            citer_is_verified = ""

        # Get the POST variables
        slug = request.POST['slug']
        htmlBlock = request.POST['htmlblock']

        # Create the BeautifulSoup object
        theSoup = BeautifulSoup(htmlBlock, "html5lib")

        # Parse the citations from the HTML and add them to the context dictionary
        parseTinyMCE_Citations(theSoup, resultDictionary)

        # Create the citation HTML (e.g. [26])
        resultArray = []
        for linkNugget in resultDictionary["CITATION_OBJECTS"]:
            stringToRespond = u"<span class='tooltip-wrap'><a class='tooltippableCarat' rel='nofollow'"
            stringToRespond = stringToRespond + u" href='/wiki/" + unicode(slug) + u"/"
            stringToRespond = stringToRespond + u"' data-username='******' data-cited_by='" + unicode(cited_by) + \
                              u"' data-cited_rank='" + unicode(citer_rank) + \
                              u"' data-citer_is_verified='" + unicode(citer_is_verified) + \
                              u"' ><sup>"
            stringToRespond = stringToRespond + u"[" + unicode(linkNugget["integer"]) + u"]"
            stringToRespond = stringToRespond + u"</sup></a></span>&#8203;"
            linkNugget["tooltipHTML"] = stringToRespond
            resultArray.append(linkNugget)

        # Return the list of citations on the page
        return render(request, 'enterlink/ajaxresults.html',
              {"result": resultArray, "renderType": url_param, "cited_by": cited_by,
               "citer_rank": citer_rank, "citer_is_verified": citer_is_verified})

    # Generate a list of the photos
    if url_param == 'tinymce-cite-picture':
        try:
            cited_by = request.GET.get('cited_by')
            citer_rank = request.GET.get('citer_rank')
            citer_is_verified = request.GET.get('citer_is_verified')
        except:
            cited_by = ""
            citer_rank = ""
            citer_is_verified = ""

        # Get the POST variables
        slug = request.POST['slug']
        htmlBlock = request.POST['htmlblock']

        # Create the BeautifulSoup object
        theSoup = BeautifulSoup(htmlBlock, "html5lib")

        # Parse the media gallery objects from the HTML and add them to the context dictionary
        parseTinyMCE_Media(theSoup, resultDictionary)

        # Create the in-article image container.
        mediaNugget, resultArray = [], []
        for linkNugget in resultDictionary["MEDIA_OBJECTS"]:
            # Get the URL
            theLinkURL = linkNugget["url"]

            # Parse the name and extension
            name, extension = os.path.splitext(theLinkURL)
            extension = extension.lower()

            # Get some relevant data from the media object
            theMIME = linkNugget["mime"]
            theLinkComment = unicode(linkNugget["caption"].decode("UTF-8"))
            theLinkID = ""
            theLinkTimestamp = linkNugget["timestamp"]

            # Structure the media container according to what type of MIME it is
            if theMIME == "" or theMIME is None:
                theMIME = "NONE"
            if theMIME == 'image/gif':
                thumbURL = linkNugget["thumb"]
                mediaNugget = [theLinkURL, theLinkComment, "GIF", theLinkID, slug, thumbURL, theLinkTimestamp]
                picString = u"<td>"
                picString += u"<a class='imagelink' href='/wiki/%s/%s/'>" % (slug, theLinkID)
                picString += u"<img class='caption-img lazyloadable' alt='%s' height='AAA111BBB222CCC333DDD444EEE' width='200' data-mimetype='%s' src='%s' data-src='%s' ></a></td>" % (u'Image', theMIME, thumbURL, theLinkURL )
            elif 'image' in theMIME:
                thumbURL = linkNugget["thumb"]
                mediaNugget = [theLinkURL, theLinkComment, "PICTURE", theLinkID, slug, thumbURL, theLinkTimestamp, theMIME]
                picString = u"<td>"
                picString += u"<a class='imagelink' href='/wiki/%s/%s/'>" % (slug, theLinkID)
                picString += u"<img class='caption-img lazyloadable' alt='%s' height='AAA111BBB222CCC333DDD444EEE' width='200' data-mimetype='%s' src='%s' data-src='%s' ></a></td>" % (u'Image', theMIME, thumbURL, theLinkURL)
            elif getYouTubeIdIfPresent(theLinkURL):
                yt_id = getYouTubeIdIfPresent(theLinkURL)
                LOAD_YOUTUBE_JS = True
                mediaNugget = [theLinkURL, theLinkComment, "YOUTUBE", yt_id, theLinkID, slug, theLinkTimestamp]
                picString = u"<td class='inline-video-wrapper'>"
                picString += u"<a class='imagelink inline-video-overlay' href='/wiki/%s/%s/'>" % (slug, theLinkID)
                picString += u"<img class='caption-img caption-video lazyloadable' alt='%s' height='AAA111BBB222CCC333DDD444EEE' width='200' data-mimetype='%s' src='https://i.ytimg.com/vi/%s/default.jpg' data-src='https://i.ytimg.com/vi/%s/hqdefault.jpg' ></a></td>" % (u'Youtube Video', theMIME, yt_id, yt_id )
            elif extension in VALID_VIDEO_EXTENSIONS:
                mediaNugget = [theLinkURL, theLinkComment, "NORMAL_VIDEO", theLinkID, theMIME, theLinkTimestamp]
                picString = u"<td class='inline-video-wrapper'>"
                picString += u"<a class='imagelink inline-video-overlay' href='/wiki/%s/%s/'>" % (slug, theLinkID)
                picString += u"<video class='caption-img caption-video' alt='%s' height='AAA111BBB222CCC333DDD444EEE' width='200' data-mimetype='%s' src='%s' preload='metadata'><source src='%s#t=0.1' type='%s'></video></a></td>" % (u'Video', theMIME, theLinkURL, theLinkURL, theMIME  )
            elif extension in VALID_AUDIO_EXTENSIONS:
                mediaNugget = [theLinkURL, theLinkComment, "AUDIO", theLinkID, theMIME, theLinkTimestamp]
                picString = u"<td class='inline-audio-wrapper'>"
                picString += u"<a class='imagelink inline-audio-overlay' href='/wiki/%s/%s/'>" % (slug, theLinkID)
                picString += u"<img class='caption-img caption-audio' alt='%s' height='AAA111BBB222CCC333DDD444EEE' width='200' data-mimetype='%s' src='%s' ></a></td>" % (u'Audio', theMIME, theLinkURL )
            else:
                continue
            stringToRespond = u"<table class='mce-item-table blurb-inline-image-container'  data-nlid='%s' data-mce-selected='1'>" \
              u"<tbody><tr>%s</tr><tr><caption class='blurbimage-caption'>%s</caption></tr></tbody></table>" % (theLinkID, picString, theLinkComment)
            resultArray.append([mediaNugget, stringToRespond])

        return render(request, 'enterlink/ajaxresults.html',
              {"result": resultArray, "renderType": url_param, "cited_by": cited_by,
               "citer_rank": citer_rank, "citer_is_verified": citer_is_verified})

    # Get the search term
    searchterm = request.GET['searchterm']

    # Get the conditions of the search
    if request.GET.get('search_type'):
        search_type = request.GET.get('search_type')
    else:
        search_type = None

    # The TinyMCE bookmark is a pointer to where the cursor / mouse was in the TinyMCE window. This is useful for inserting
    # objects there.
    if request.GET.get('tinymceBookmark'):
        tinymceBookmark = request.GET.get('tinymceBookmark')
    else:
        tinymceBookmark = None

    # Get the ID of a relevant element, if applicable
    if request.GET.get('elementID'):
        elementID = request.GET.get('elementID')
    else:
        elementID = None

    # tinymce-link-page
    BLURB_CHAR_LIMIT = 250

    # Perform the search
    result = mainSearch(url_param, search_type, searchterm, FETCH_LIMIT, BLURB_CHAR_LIMIT)

    blurbResults = []
    # Blank result
    if ((result == 0) and (blurbResults == 0)):
        return render(request, 'enterlink/ajaxresults.html',
               {"result": result, "renderType": url_param, "blurbResults": blurbResults, "searchterm": searchterm,
                "search_type": search_type, "tinymceBookmark": tinymceBookmark, "elementID": elementID})

    # Return the results
    return render(request, 'enterlink/ajaxresults.html',
                  {"result": result, "renderType": url_param, "blurbResults": blurbResults, "searchterm": searchterm,
                   "search_type": search_type, "tinymceBookmark": tinymceBookmark, "elementID": elementID})