示例#1
0
def extract_children(item):
    children = None
    child_items = item["Children"]
    if child_items <> None:
        children = []
        for child in child_items:
            childPath = child["Path"]
            childOriginalAlbumPath = child["OriginalAlbumPath"]
            if childOriginalAlbumPath is None:
                childOriginalAlbumPath = ''
            childHash = utils.generate_url_hash(childPath)
            childTitle = child["Title"]
            childType = child["Type"]
            childDescription = child["Description"]

            foundThumbnailSize = None
            childImageSizes = child["ImageSizes"]
            if childImageSizes <> None:
                childImageWidth = 0
                childImageHeight = 0

                for childImageSize in childImageSizes:
                    childImageSizeWidth = childImageSize["Width"]
                    childImageSizeHeight = childImageSize["Height"]

                    if ( childImageWidth == 0 or childImageSizeWidth < childImageWidth) and (
                            childImageHeight == 0 or childImageSizeHeight < childImageHeight):
                        childImageWidth = childImageSizeWidth
                        childImageHeight = childImageSizeHeight

                if childImageWidth <> 0 and childImageHeight <> 0:
                    foundThumbnailSize = models.ResizedImage(
                        width=childImageWidth,
                        height=childImageHeight)

            children.append(models.ChildItem(
                id=childHash,
                path=childPath,
                originalAlbumPath=childOriginalAlbumPath,
                title=childTitle,
                type=childType,
                description=childDescription,
                thumbnail=foundThumbnailSize
            ))

    return children
示例#2
0
def extract_breadcrumbs(item):
    breadcrumbs = None
    breadcrumbItems = item["Breadcrumbs"]
    if breadcrumbItems <> None:
        breadcrumbs = []
        for crumb in breadcrumbItems:
            crumbPath = crumb["Path"]
            crumbHash = utils.generate_url_hash(crumbPath)
            crumbTitle = crumb["Title"]
            crumbDescription = crumb["Description"]

            breadcrumbs.append(models.BreadcrumbItem(
                id=crumbHash,
                path=crumbPath,
                title=crumbTitle
            ))

    return breadcrumbs
示例#3
0
def extract_sibling(item, which):
    firstSibling = None
    fsibling = item[which]
    if fsibling <> None:
        childPath = fsibling["Path"]
        childHash = utils.generate_url_hash(childPath)
        childTitle = fsibling["Title"]
        childType = fsibling["Type"]
        childDescription = fsibling["Description"]

        foundThumbnailSize = None
        childImageSizes = fsibling["ImageSizes"]
        if childImageSizes <> None:
            childImageWidth = 0
            childImageHeight = 0

            for childImageSize in childImageSizes:
                childImageSizeWidth = childImageSize["Width"]
                childImageSizeHeight = childImageSize["Height"]

                if ( childImageWidth == 0 or childImageSizeWidth < childImageWidth) and (
                        childImageHeight == 0 or childImageSizeHeight < childImageHeight):
                    childImageWidth = childImageSizeWidth
                    childImageHeight = childImageSizeHeight

            if childImageWidth <> 0 and childImageHeight <> 0:
                foundThumbnailSize = models.ResizedImage(
                    width=childImageWidth,
                    height=childImageHeight)

        firstSibling = models.ChildItem(
            id=childHash,
            path=childPath,
            title=childTitle,
            type=childType,
            description=childDescription,
            thumbnail=foundThumbnailSize
        )
    return firstSibling
示例#4
0
    def get(self):

        host = self.request.host_url

        user_agent = self.request.headers.get('User-Agent', None)

        if utils.is_development() == False:
            if self.request.scheme == 'http' and utils.device_supports_ssl_tni(user_agent):
                self.response.headers['Cache-Control'] = 'public,max-age=%d' % 86400
                self.response.headers['Pragma'] = 'public'
                self.redirect(utils.redirect_url(self.request.path, self.request.query_string), permanent=True)

            if host != 'http://www.markridgwell.co.uk' and host != 'https://www.markridgwell.co.uk':
                self.response.headers['Cache-Control'] = 'public,max-age=%d' % 86400
                self.response.headers['Pragma'] = 'public'
                self.redirect(utils.redirect_url(self.request.path, self.request.query_string), permanent=True)
                return



        windows_share = utils.enable_windows_share_metadata(user_agent)
        search_path = self.request.path.lower()

        hash = utils.generate_url_hash(search_path)

        q = models.GalleryItem.query(models.GalleryItem.id == hash)

        track = utils.should_track(self.request.headers)
        if track:
            logging.info('Tracking: Enabled')
        else:
            logging.info('Tracking: Disabled')

        item = q.get()
        if item is None:
            new_search_path = utils.convert_old_url(search_path)

            should_report_error = True
            if new_search_path <> search_path:
                hash = utils.generate_url_hash(new_search_path)
                search_path = new_search_path
                q = models.GalleryItem.query(models.GalleryItem.id == hash)
                item = q.get()

                if item is not None:
                    should_report_error = False
                    utils.add_response_headers(self.request, self.response.headers)
                    self.response.headers['Cache-Control'] = 'public,max-age=%d' % 86400
                    self.response.headers['Pragma'] = 'public'
                    self.redirect(utils.redirect_url(new_search_path, self.request.query_string), permanent=True)

            if should_report_error:
                template_vals = {'host': self.request.host_url, 'path': search_path, 'track': track, 'hash': hash,
                                 'users': users, 'showShare': False, 'windowsShare': windows_share}
                utils.add_response_headers(self.request, self.response.headers)
                self.response.out.write(utils.render_template("notfound.html", template_vals))
                self.response.set_status(404)
        else:
            children = None
            if item.children:
                children = []
                for child in item.children:
                    thumbnail_url = None
                    if child.thumbnail:
                        base_child_image_path = child.path
                        if child.originalAlbumPath:
                            base_child_image_path = child.originalAlbumPath

                        thumbnail_url = utils.image_url(
                            base_child_image_path, child.thumbnail)
                    tag_id = utils.path_to_tagId(child.path)
                    childItem = {'id': child.id, 'path': child.path, 'title': child.title, 'type': child.type,
                                 'description': child.description, 'thumbnail': child.thumbnail,
                                 'thumbnailUrl': thumbnail_url, "tagId": tag_id}
                    children.append(childItem)

            parent_item_url = None
            breadcrumbs = None
            if item.breadcrumbs:
                breadcrumbs = []
                last_crumb_tag_id = utils.path_to_tagId(item.path)
                for crumb in reversed(item.breadcrumbs):
                    if parent_item_url is None:
                        parent_item_url = host + crumb.path
                    tag_id = utils.path_to_tagId(crumb.path)
                    crumb_item = {'id': crumb.id, 'path': crumb.path, 'title': crumb.title, 'description': crumb.description, "tagId": last_crumb_tag_id}
                    breadcrumbs.insert(0, crumb_item)
                    last_crumb_tag_id = tag_id
            if parent_item_url is None:
                parent_item_url = host

            resize_css = None
            thumbnail_image_url = None
            full_image_url = None
            image_width = None
            image_height = None

            base_image_path = item.path
            if item.originalAlbumPath:
                base_image_path = item.originalAlbumPath

            if item.resizes:

                ordered_resizes = sorted(item.resizes, key=lambda r: r.width)

                css = utils.build_image_css(item, ordered_resizes)
                if css is None:
                    first = None
                    last = None
                    resize_css = None
                else:
                    first = css['first']
                    last = css['last']
                    resize_css = '<style>' + css['css'] + '</style>'

                if first is None:
                    resize_css = ''
                else:
                    thumbnail_image_url = utils.image_url(base_image_path, first)

                    full_image_url = utils.image_url(base_image_path, last)
                    image_width = last.width
                    image_height = last.height

            first_sibling = None
            previous_sibling = None
            next_sibling = None
            last_sibling = None

            if item.firstSibling is not None:
                first_sibling = {'title': item.firstSibling.title, 'url': item.firstSibling.path}

            if item.previousSibling is not None:
                previous_sibling = {'title': item.previousSibling.title, 'url': item.previousSibling.path}

            if item.nextSibling is not None:
                next_sibling = {'title': item.nextSibling.title, 'url': item.nextSibling.path}

            if item.lastSibling is not None:
                last_sibling = {'title': item.lastSibling.title, 'url': item.lastSibling.path}

            keywords_text = None
            if item.keywords:
                keywords_text = ",".join(item.keywords)

            show_share = utils.should_share(user_agent)

            views = 0
            if item.resizes:

                if tracking.is_sharing_callback(user_agent):
                    views = tracking.record_share(item.id, item.path)
                else:
                    if tracking.is_trackable(user_agent):
                        views = tracking.record_view(item.id, item.path)

            original_album_path = ''
            title = item.title
            if children is None:
                title = itemnaming.photo_title(item, 10000)

                if item.originalAlbumPath:
                    original_album_path = item.originalAlbumPath

            description = ''
            if item.description:
                md = markdown.Markdown()
                raw_description = item.description
                description = md.convert(raw_description)

            keywords = []
            if item.keywords:
                item_to_select = ''
                if item.path.startswith('/albums/'):
                    if item.breadcrumbs:
                        item_to_select = utils.path_to_tagId(item.breadcrumbs[-1].path) + '-' + utils.path_to_tagId(item.path)
                else:
                    item_to_select = utils.path_to_tagId(item.path)

                for keyword in item.keywords:
                    keyword_url = utils.generate_keyword_url(host, keyword, item_to_select)

                    keywords.append(
                        {'name': keyword, 'url': keyword_url}
                    )

            template_vals = {'host': host, 'path': search_path, 'track': track, 'hash': hash, 'users': users,
                             'title': title, 'item': item, 'children': children, 'breadcrumbs': breadcrumbs,
                             'resizecss': resize_css, 'staticurl': self.request.relative_url('/static'),
                             'thumbnail_url': thumbnail_image_url, 'fullImageUrl': full_image_url,
                             'fullImageWidth': image_width, 'fullImageHeight': image_height,
                             'firstSibling': first_sibling, 'previousSibling': previous_sibling,
                             'nextSibling': next_sibling, 'lastSibling': last_sibling,
                             'keywords': keywords, 'showShare': show_share, 'windowsShare': windows_share,
                             'parentItemUrl': parent_item_url, 'description': description,
                             'original_album_path': original_album_path, 'keywords_text': keywords_text}
            if children is None:
                self.response.out.write(utils.render_template("photo.html", template_vals))
            else:
                if search_path == '/':
                    self.response.out.write(utils.render_template("home.html", template_vals))
                else:
                    self.response.out.write(utils.render_template("index.html", template_vals))

            utils.add_response_headers(self.request, self.response.headers)
            self.response.headers['Cache-Control'] = 'public,max-age=%d' % 86400
            self.response.headers['Pragma'] = 'public'
            self.response.headers['X-PageViews'] = str(views)
示例#5
0
def synchronize_common(contents):
    decoded = json.loads(contents)
    version = decoded["version"]

    items_written = 0

    logging.info("Decoded Version: " + str(version))

    for item in decoded["items"]:

        path = item["Path"]
        logging.info("Path: " + path)
        original_album_path = item["OriginalAlbumPath"]
        if original_album_path is None:
            original_album_path = ''
        title = item["Title"]
        type = item["Type"]
        description = item["Description"]
        rating = None  # item["Rating"]

        hash = utils.generate_url_hash(path)
        indexSection = hash[:1]

        location = extract_location(item)
        children = extract_children(item)
        breadcrumbs = extract_breadcrumbs(item)
        metadata = extract_metadata(item)
        keywords = extract_keywords(item)
        foundImageSizes = extract_image_sizes(item)
        firstSibling = extract_sibling(item, "First")
        previousSibling = extract_sibling(item, "Previous")
        nextSibling = extract_sibling(item, "Next")
        lastSibling = extract_sibling(item, "Last")

        q = models.GalleryItem.query(models.GalleryItem.id == hash)

        dbItem = q.get()
        if dbItem is None:
            dbItem = models.GalleryItem(
                id=hash,
                path=path,
                originalAlbumPath=original_album_path,
                indexSection=indexSection,
                title=title,
                type=type,
                description=description,
                rating=rating,
                location=location,
                children=children,
                breadcrumbs=breadcrumbs,
                resizes=foundImageSizes,
                metadata=metadata,
                keywords=keywords,
                firstSibling=firstSibling,
                previousSibling=previousSibling,
                nextSibling=nextSibling,
                lastSibling=lastSibling
            )
            dbItem.put()

            items_written = items_written + 1
            logging.info('Created: ' + path)

            if type == 'photo' and utils.is_public_publishable_path(path) and utils.is_publishable(dbItem):
                publishItem = models.PublishableItem(id=dbItem.id)
                publishItem.put()

        else:

            if path <> dbItem.path or original_album_path <> dbItem.originalAlbumPath or indexSection <> dbItem.indexSection or dbItem.title <> title or dbItem.type <> type or dbItem.description <> description or dbItem.location <> location or children_changed(
                    dbItem.children, children) or breadcrumbs_changed(dbItem.breadcrumbs, breadcrumbs) or resizes_changed(dbItem.resizes,
                                                                                                      foundImageSizes) or metadata_changed(
                    dbItem.metadata, metadata) or keywords_changed(dbItem.keywords, keywords) or sibling_changed(
                    dbItem.firstSibling, firstSibling) or sibling_changed(dbItem.previousSibling,
                                                                          previousSibling) or sibling_changed(
                    dbItem.nextSibling, nextSibling) or sibling_changed(dbItem.lastSibling, lastSibling):
                dbItem.path = path
                dbItem.originalAlbumPath = original_album_path
                dbItem.indexSection = indexSection
                dbItem.title = title
                dbItem.type = type
                dbItem.description = description
                dbItem.rating = rating
                dbItem.location = location
                dbItem.children = children
                dbItem.breadcrumbs = breadcrumbs
                dbItem.resizes = foundImageSizes
                dbItem.metadata = metadata
                dbItem.keywords = keywords
                dbItem.firstSibling = firstSibling
                dbItem.previousSibling = previousSibling
                dbItem.nextSibling = nextSibling
                dbItem.lastSibling = lastSibling
                dbItem.updated = datetime.datetime.now()

                dbItem.put()

                items_written = items_written + 1
                logging.info('updated: ' + path)
            else:
                logging.info('Unchanged: ' + path)

    for deletedItem in decoded["deletedItems"]:
        logging.info('Deleting: ' + deletedItem)
        hash = utils.generate_url_hash(deletedItem)

        if delete_item(hash):
            items_written = items_written + 1

        delete_published_item(hash)

    if items_written > 0:
        invalidateOutputCaches()
        pubsubhubub.queue_update()

    return items_written