Example #1
0
def home(request):

    try:
        cache = get_cache('default')

        if not cache.get('flickr_main'):
            # from flickr photos get one tagged "main"(should have only one)
            main = flickr.api.walk(user_id=flickr.user_id, tags="main", tag_mode='all', sort="date-posted-desc")
            main_list = list(iter(main))

            cache.set('flickr_main', json.dumps([dict(elt.items()) for elt in main_list]), timeout=3600)

        cached_main = cache.get('flickr_main')
        main = json.loads(cached_main)

        if not cache.get('flickr_favorites'):
            # from flickr get all photo elements tagged "favorite"
            favorites = flickr.api.walk(user_id=flickr.user_id, tags="favorite, -main", tag_mode='all', sort="date-posted-desc")
            favorites_list = list(iter(favorites))
            cache.set('flickr_favorites',json.dumps([dict(elt.items()) for elt in favorites_list]) , timeout=3600)

        cached_favorites = cache.get('flickr_favorites')
        favorites = json.loads(cached_favorites)

        images = []
        sizes = ['496x374', '296x224', '296x146', '194x146', '194x224']

        main_photo = main[0]
        images.append((flickr.get_url(main_photo, 'b'), sizes[0], main_photo.get('title')))

        sizes = sizes[1:]

        j = 0
        # create an image file from every favorite
        for i,favorite in enumerate(favorites):

            if main_photo.get('id') != favorite.get('id'):
                images.append((flickr.get_url(favorite, 'b'), sizes[j % len(sizes)], favorite.get('title')))
                j += 1
    except Exception as e:
        import traceback
        traceback.print_exc(e)
        images = []

    # get recent blog posts
    recent = Post.objects.filter(is_active=True, post_type=Post.TYPE_BLOG).order_by('-created_on')[:5]

    # get upcoming events
    upcoming = Event.objects.filter(is_active=True, date__gte=datetime.today())[:5]

    videos = Video.objects.filter(is_active=True)[:3]

    context = dict(images = images, recent=recent, upcoming=upcoming, videos=videos)
    return render(request, 'public/home.html', context)
Example #2
0
    def photo(self):
        cache = get_cache('default')
        key = self.get_cache_key()
        
        if not cache.get(key):
            blog_photos = flickr.api.walk(user_id=flickr.user_id, tags="blog")
            cache.set(key, list(iter(blog_photos)), 3600)
            
        blog_photos = cache.get(key)

        for photo in blog_photos:
            if photo.get('id') == self.image_id:
                return flickr.get_url(photo,"b")
Example #3
0
    def photo(self):
        cache = get_cache('default')
        key = self.get_cache_key()

        if not cache.get(key):
            blog_photos = flickr.api.walk(user_id=flickr.user_id, tags="blog")
            cache.set(key, list(iter(blog_photos)), 3600)
            
        blog_photos = cache.get(key)

        for photo in blog_photos:
            if photo.get('id') == self.image_id:
                return flickr.get_url(photo,"b")
Example #4
0
    def photo(self):
        key = self.get_cache_key()

        try:
            if not cache.get(key):
                blog_photos = flickr.api.walk(user_id=flickr.user_id, tags="blog")
                blog_photos_list = list(iter(blog_photos))
                cache.set(key, json.dumps([dict(elt.items()) for elt in blog_photos_list]), timeout=None)

            cached_blog_photos = cache.get(key)
            blog_photos = json.loads(cached_blog_photos)

            for photo in blog_photos:
                if photo.get('id') == self.image_id:
                    return flickr.get_url(photo, "b")

        except:
            pass
Example #5
0
def home(request):

    try:
        cache = get_cache('default')

        if not cache.get('flickr_main'):
            # from flickr photos get one tagged "main"(should have only one)
            main = flickr.api.walk(user_id=flickr.user_id,
                                   tags="main",
                                   tag_mode='all',
                                   sort="date-posted-desc")

            cache.set('flickr_main', list(iter(main)), 3600)

        main = cache.get('flickr_main')

        if not cache.get('flickr_favorites'):
            # from flickr get all photo elements tagged "favorite"
            favorites = flickr.api.walk(user_id=flickr.user_id,
                                        tags="favorite, -main",
                                        tag_mode='all',
                                        sort="date-posted-desc")
            cache.set('flickr_favorites', list(iter(favorites)), 3600)

        favorites = cache.get('flickr_favorites')

        images = []
        sizes = ['496x374', '296x224', '296x146', '194x146', '194x224']

        main_photo = main[0]
        images.append((flickr.get_url(main_photo,
                                      'b'), sizes[0], main_photo.get('title')))

        sizes = sizes[1:]

        j = 0
        # create an image file from every favorite
        for i, favorite in enumerate(favorites):

            if main_photo.get('id') != favorite.get('id'):
                images.append((flickr.get_url(favorite,
                                              'b'), sizes[j % len(sizes)],
                               favorite.get('title')))
                j += 1
    except Exception as e:
        import traceback
        traceback.print_exc(e)
        images = []

    # get recent blog posts
    recent = Post.objects.filter(is_active=True).order_by('-created_on')[:5]

    # get upcoming events
    upcoming = Event.objects.filter(is_active=True,
                                    date__gte=datetime.today())[:5]

    videos = Video.objects.filter(is_active=True)[:3]

    context = dict(images=images,
                   recent=recent,
                   upcoming=upcoming,
                   videos=videos)
    return render(request, 'public/home.html', context)
Example #6
0
def home(request):

    try:
        if not cache.get('flickr_main'):
            # from flickr photos get one tagged "main"(should have only one)
            main = flickr.api.walk(user_id=flickr.user_id,
                                   tags="main",
                                   tag_mode='all',
                                   sort="date-posted-desc")
            main_list = list(iter(main))

            cache.set('flickr_main',
                      json.dumps([dict(elt.items()) for elt in main_list]),
                      timeout=3600)

        cached_main = cache.get('flickr_main')
        main = json.loads(cached_main)

        if not cache.get('flickr_favorites'):
            # from flickr get all photo elements tagged "favorite"
            favorites = flickr.api.walk(user_id=flickr.user_id,
                                        tags="favorite, -main",
                                        tag_mode='all',
                                        sort="date-posted-desc")
            favorites_list = list(iter(favorites))
            cache.set('flickr_favorites',
                      json.dumps([dict(elt.items())
                                  for elt in favorites_list]),
                      timeout=3600)

        cached_favorites = cache.get('flickr_favorites')
        favorites = json.loads(cached_favorites)

        images = []
        sizes = ['496x374', '296x224', '296x146', '194x146', '194x224']

        main_photo = main[random.choice(range(len(main)))]
        images.append((flickr.get_url(main_photo,
                                      'b'), sizes[0], main_photo.get('title')))

        sizes = sizes[1:]

        j = 0
        random.shuffle(favorites)
        # create an image file from every favorite
        for i, favorite in enumerate(favorites):

            if main_photo.get('id') != favorite.get('id'):
                images.append((flickr.get_url(favorite,
                                              'b'), sizes[j % len(sizes)],
                               favorite.get('title')))
                j += 1
    except Exception as e:
        import traceback
        traceback.print_exc(e)
        images = []

    # get recent blog posts
    recent = Post.objects.filter(
        is_active=True, post_type=Post.TYPE_BLOG).order_by('-created_on')[:5]

    # get upcoming events
    upcoming = Event.objects.filter(is_active=True,
                                    date__gte=datetime.today())[:5]

    videos = Video.objects.filter(is_active=True)[:3]

    context = dict(images=images,
                   recent=recent,
                   upcoming=upcoming,
                   videos=videos)
    return render(request, 'public/home.html', context)