Esempio n. 1
0
def post_queue_add_page(request, addpagerequest_id, html, url):
    """Called by the server-side renderer to finish adding a page."""

    addPageRequest = models.AddPageRequest.objects.get(id=addpagerequest_id)

    # If this page was already completed, don't add it again -- there's a possibility that multiple
    # threads are working on the same page at once.
    if addPageRequest.status_code == models.StatusCode.DONE:
        log.error("Multiple threads processed APR %s" % addPageRequest.id)
        return

    flowgram = addPageRequest.flowgram
    user = flowgram.owner
    title = helpers.get_title(request)

    # Modify the HTML to add base tag etc.
    (html, url) = fix.process_page(html, url)
        
    # Create and save the page:
    page = addPageRequest.page
    page.title = title
    page.save()
    html = helpers.cache_css_files(page, html)
    page = controller.create_page_to_flowgram(flowgram, page, html, do_make_thumbnail=False,
                                                  set_position=False)
    
    # Updating the status code and page
    addPageRequest.status_code = models.StatusCode.DONE
    
    controller.set_page_hl_contents(page, html)
    
    addPageRequest.save()
        
    log.action('post_queue_add_page %s to %s for %s' % (page.id, flowgram.id, user.username))
Esempio n. 2
0
def queue_add_url(request, enc, url, flowgram):
    if not url.startswith('http://') and not url.startswith('https://'):
        url = 'http://' + url

    match = GALLERY_ME_COM_REGEX.match(url)
    if match:
        (username, gallery, file) = match.groups(0)

        title = 'MobileMe Gallery - %s' % file
        link = 'http://gallery.me.com/%s/%s/%s/web.jpg' % (username, gallery, file)
        
        html = '<html><head><title>%s</title><style>@import url("/media/css/photo_importers.css");</style></head><body><img src="%s" /></body></html>' % \
            (title, link)
    
        page = models.Page.objects.create(title=title, source_url=link)
        page = controller.create_page_to_flowgram(flowgram, page, html)
        
        aprId = '-1'
    else:
        page = models.Page.objects.create(flowgram=flowgram,
                                          owner=request.user,
                                          source_url=url,
                                          position=controller.get_next_position(flowgram))
        
        aprId = models.AddPageRequest.objects.create(flowgram=flowgram, url=url, page=page).id
        
    return data_response.create(enc, 'ok', {'ready': 0,
                                            'error': 0,
                                            'results': encode.page.to_dict(page),
                                            'request_id': aprId})
Esempio n. 3
0
def save_custom_page(request, enc, flowgram, content):
    page_id = request.POST.get('page_id', '')
    page = models.Page.objects.get(id=page_id) if page_id else None

    user = flowgram.owner

    if page:
        # If editing an existing page.

        # Checking to make sure the page belongs to the specified Flowgram.
        if not page.flowgram == flowgram:
            return error_response.create(
                enc,
                'The specified page does not belong to the specified Flowgram.')

        html = render_to_string('blankpages/view.html', {'html': content,
                                                         'user': user})
        controller.set_page_contents(page, html, sethl=True)

        # This try..except block is used for legacy support (previous versions did not save a
        # separate CustomPage object).
        try:
            custom_page = models.CustomPage.objects.get(page=page)
            custom_page.content = content
            custom_page.save()
        except models.CustomPage.DoesNotExist:
            models.CustomPage.objects.create(page=page, content=content)

        controller.make_thumbnail(page)

        return data_response.create(enc, 'ok', encode.page.to_dict(page))
    else:
        # If creating a new page.

        html = render_to_string('blankpages/view.html', {'html': content,
                                                         'user': user})
        
        title = "%s's custom Flowgram page" % user
        page = models.Page.objects.create(title=title, source_url='')
        page = controller.create_page_to_flowgram(flowgram, page, html)

        page.source_url = '%sapi/getpage/%s/' % (localsettings.my_URL_BASE, page.id)
        page.is_custom = True
        page.save()

        models.CustomPage.objects.create(page=page, content=content)

        return data_response.create(enc, 'ok', encode.page.to_dict(page))
Esempio n. 4
0
def add_page(user, url, html, title):
    # Modifying the HTML to add a base tag, etc.
    (html, url) = fix.process_page(html, url)
    
    # Creating and saving the page.
    page = models.Page.objects.create(title=title, source_url=url)
    html = helpers.cache_css_files(page, html)

    (flowgram, is_new) = controller.get_working_flowgram(user)
    if is_new:
        log.action('Created new working Flowgram with ID %s as a result of calling get_working_flowgram in add_page.' % \
                       flowgram.id)
    page = controller.create_page_to_flowgram(flowgram, page, html)
        
    # Removing the user's 'just_published' attribute.
    profile = user.get_profile()
    profile.just_published = False
    profile.save()

    return (flowgram, page)
Esempio n. 5
0
def import_rss(flowgram, rss_url, type, options):
    if rss_url.startswith("feed://"):
        rss_url = "http://" + rss_url[7:]

    parsed = feedparser.parse(rss_url)
    if not parsed.has_key("status"):
        raise Http404
    else:
        if parsed["status"] == "404":
            raise Http404

    is_atom = not not parsed["feed"]

    if is_atom:
        # If ATOM
        channelTitle = parsed["feed"].get("title", "")
        channelDescription = ""
    else:
        # If RSS
        channelTitle = parsed.channel.title
        channelDescription = parsed.channel.description

    flowgram_changed = False
    if not flowgram.title or flowgram.title.lower() == "untitled":
        flowgram.title = channelTitle
        flowgram_changed = True
    if not flowgram.description:
        flowgram.description = channelDescription
        flowgram_changed = True

    if flowgram_changed:
        flowgram.save()

    pages = []

    items = parsed["items"]
    if options["max_results"]:
        max_results = options["max_results"]
        items = items[:max_results]

    if type == "linkedarticles" or type == "articlesummaries":
        required_keys = ["title", "link", "description"]
        for item in items:
            for rkey in required_keys:
                if not item.has_key("title"):
                    title = ""
                elif item.has_key("title"):
                    title = item.title
                if not item.has_key("link"):
                    link = ""
                elif item.has_key("link"):
                    link = item.link
                if not item.has_key("description"):
                    description = ""
                elif item.has_key("description"):
                    description = item.description

            # need to check whether the 'title,link,description' attribute exists

            if type == "linkedarticles":
                page = Page.objects.create(
                    flowgram=flowgram,
                    owner=flowgram.owner,
                    title=title,
                    source_url=link,
                    position=controller.get_next_position(flowgram),
                )

                controller.add_default_time(page)

                AddPageRequest.objects.create(flowgram=flowgram, url=link, page=page)

                pages.append(encode.page.to_dict(page))
            elif type == "articlesummaries":
                # Create and save the page:
                page = Page.objects.create(title=title, source_url=link)
                page.save()

                context = Context({"title": title, "url": link, "description": remove_script_tags(description)})
                template = loader.get_template("importers/rss.html")

                html = "%s" % template.render(context)
                page = controller.create_page_to_flowgram(flowgram, page, html)

                pages.append(encode.page.to_dict(page))
    elif type == "singlesummarypage":
        # Create and save the page:
        page = Page.objects.create(title=channelTitle, source_url=rss_url)
        page.save()

        context = Context({"title": channelTitle, "items": items})
        template = loader.get_template("importers/rss_singlesummarypage.html")

        html = "%s" % template.render(context)
        page = controller.create_page_to_flowgram(flowgram, page, html)

        pages.append(encode.page.to_dict(page))

    if len(pages) == 0:
        raise Http404
    return data_response.create(options.get("enc", "json"), "ok", pages)
Esempio n. 6
0
def handle_file_upload_image(enc, content, content_type, filetype, filename, flowgram):
    temp_image = tempfile.mkstemp(".image")
    os.close(temp_image[0])
    temp_image_path = temp_image[1]

    try:
        file = open(temp_image_path, "wb")
        file.write(content)
        file.close()
        
        try:
            # Opens the file to make sure it works as an image.
            image = Image.open(temp_image_path)
            image.load()
        except:
            return error_response.create(
                enc,
                'Invalid image: Currently supported image types include: JPEG, PNG, and GIF')

        (mime, extension) = IMAGE_FORMAT_TO_MIME_AND_EXTENSION[image.format]

        # Resizing the image if too large.
        if image.size[0] > settings.DEFAULT_PHOTO_WIDTH or \
               image.size[1] > settings.DEFAULT_PHOTO_HEIGHT:
            image.thumbnail((settings.DEFAULT_PHOTO_WIDTH, settings.DEFAULT_PHOTO_HEIGHT),
                            Image.BICUBIC)

        page = models.Page.objects.create(title=filename)

        uploaded_file = models.UploadedFile.objects.create(page=page, mimetype=mime, media_type=0)
        page.source_url = uploaded_file.get_absolute_url()

        html = ''.join(['<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html><head><title></title>',
                        '<link rel="stylesheet" type="text/css" href="',
                        localsettings.MEDIA_URL,
                        'css/photo_importers.css">',
                        '<link rel="stylesheet" type="text/css" href="',
                        localsettings.MEDIA_URL,
                        'css/page.css"></head><body><div><img src="',
                        uploaded_file.get_absolute_url(),
                        '" /></div></body></html>'])

        # Saving the thumbnailed image and reading the data.
        try:
            temp_image_final = tempfile.mkstemp(extension)
            os.close(temp_image_final[0])
            temp_image_final_path = temp_image_final[1]
            image.save(temp_image_final_path)

            file = open(temp_image_final_path, "rb")
            data = file.read()
            file.close()
        finally:
            os.remove(temp_image_final_path)
        
        uploaded_file.set_file(data)

        page = controller.create_page_to_flowgram(flowgram, page, html)
        return data_response.create(enc, 'ok', encode.page.to_dict(page))
    finally:
        os.remove(temp_image_path)
Esempio n. 7
0
def add_ppt_slide(request, flowgram, ppt_req_id, title, url, html):
    page = models.Page.objects.create(title=title,
                                      source_url=url,
                                      creation_source="ppt:" + ppt_req_id)
    page = controller.create_page_to_flowgram(flowgram, page, html)
Esempio n. 8
0
@require_auth
@login_required
def create_fg_from_aid(request):
    facebook_token = request.session['facebook_token']
    
    try:
        photos = minifb.call("facebook.photos.get", localsettings.FACEBOOK_API_KEY, FACEBOOK_API_SECRET_MINIFB, session_key=facebook_token, aid=request.GET["aid"])
    except minifb.FacebookError, e:
        if e.error_code == 102:
            return HttpResponse("<script>top.location = \"%s\";</script>" % login_url())
        raise
    
    fg, new = controller.get_working_flowgram(request.user)
    
    for photo in photos:
        title = photo["caption"]
        link = photo["src_big"]
        
        if title == "":
            title = "photo"
        
        html = "<html><head><title>%s</title>" \
                    "<style>@import url(\"/media/css/photo_importers.css\");</style>" \
                "</head><body><img src=\"%s\" /></body></html>" % (title, link)

        page = Page(title=title, source_url=link)
        page = controller.create_page_to_flowgram(fg, page, html)

    return HttpResponse("")