Exemplo n.º 1
0
def _update_adapter(adptr):
    """
    Update implementation.

    If `adptr` returns no update_command(), it is being ignored.
    """
    os.chdir(adptr.local_repository_location())

    cmd = adptr.update_command()
    if not cmd:
        return True

    errorcode, output = _run_cmd(cmd)
    if errorcode:
        _log("\nERROR:\n---%s\n" % output.decode("utf-8") +
             "\n---\nCould not update %s" % adptr)
        return False

    # Getting current repository state
    # This state will be saved after the update procedure is finished
    # (all cache entries invalidated)
    cmd = adptr.current_state_command()
    state = None
    if cmd:
        errorcode, state = _run_cmd(cmd)
        if errorcode:
            _log("\nERROR:\n---\n" + state +
                 "\n---\nCould not get repository state: %s" % adptr)
            return False
        state = state.strip()

    # Getting list of files that were changed
    # that will be later converted to the list of the pages to be invalidated
    cmd = adptr.get_updates_list_command()
    updates = []
    if cmd:
        errorcode, output = _run_cmd(cmd)
        output = output.decode("utf-8")
        if errorcode:
            _log("\nERROR:\n---\n" + output +
                 "\n---\nCould not get list of pages to be updated: %s" %
                 adptr)
            return False
        updates = output.splitlines()

    entries = adptr.get_updates_list(updates)
    if entries:
        _log("%s Entries to be updated: %s", adptr, len(entries))

    name = adptr.name()
    for entry in entries:
        cache_name = name + ":" + entry
        _log("+ invalidating %s", cache_name)
        cache.delete(cache_name)

    if entries:
        _log("Done")

    adptr.save_state(state)
    return True
Exemplo n.º 2
0
def delete_sysprop(key, modifier_id):
    if strutil.is_empty(key):
        raise CoreError("key can't be empty.")
    conn = __get_conn()
    conn.delete(SYSPROP_TABLENAME, where="key='%s'" % key)
    cache.delete(CACHESPACE_SYSPROP, key)
    return True
Exemplo n.º 3
0
def tag_added(title, user_id, artwork):
    if not title:
        return None

    title = title.strip()
    url_name = tag_url_name(title)
    if len(title) <= 1 or len(title) > 64:
        return None

    if hide_bad_language(title) != title:
        return None

    global_tag = db.Tag.all().filter('url_name =', url_name).get()
    if global_tag:
        if getattr(global_tag, 'count') is not None:
            global_tag.count = global_tag.count + 1
            global_tag.last_date = datetime.now()
            if not global_tag.cover:
                global_tag.cover = artwork
            global_tag.put()
    else:
        tag = db.Tag.all().filter('url_name =', url_name).get()
        if not tag:
            tag = db.Tag()
            tag.title = title
            tag.title_lower = title.lower()
            tag.url_name = url_name
            tag.date = datetime.now()
            tag.last_date = datetime.now()
            tag.cover = artwork
            tag.count = 1
            tag.put()

    user_tag = db.UserTag.all().filter('user_id =',
                                       user_id).filter('url_name',
                                                       url_name).get()
    if user_tag:
        user_tag.count = user_tag.count + 1
        user_tag.last_date = datetime.now()
        if not user_tag.cover:
            user_tag.cover = artwork
        user_tag.put()
    else:
        user_tag = db.UserTag()
        user_tag.user_id = user_id
        user_tag.url_name = url_name
        user_tag.cover = artwork
        user_tag.title = title
        user_tag.title_lower = title.lower()
        user_tag.date = datetime.now()
        user_tag.last_date = datetime.now()
        user_tag.count = 1
        user_tag.put()

    cache.delete(cache.MC_TAG + url_name)
    return url_name
Exemplo n.º 4
0
def add_user_profile(profile):
    profile_exist = get_user_profile(profile.email)
    if profile_exist:
        return profile_exist

    result = profile.put()
    cache.add(cache.MC_USER_PROFILE + profile.email, profile)
    cache.delete(cache.MC_MAIN_PAGE_RECENT_IMAGES_KEY)
    cache.delete(cache.MC_MAIN_PAGE_RECENT_COMMENTS)
    return result
Exemplo n.º 5
0
def favorite_artwork(artwork, user_email):
    cache.delete(cache.MC_FAVORITE_BY_USER + str(artwork.key().id()) + '_' +
                 user_email)
    cache.delete(cache.MC_FAVORITE_COUNT + str(artwork.key().id()))

    # Save favorite star
    fav = db.Favorite()
    fav.artwork = artwork
    fav.user_email = user_email
    fav.save()

    # increase artworks favorites counter
    fav_count = db.FavoriteCounter.all().filter('artwork =', artwork).get()
    if fav_count:
        fav_count.count = fav_count.count + 1
    else:
        fav_count = db.FavoriteCounter()
        fav_count.artwork = artwork
        fav_count.count = 1

    fav_count.save()

    # increase author favorites counter
    user_profile = db.UserProfile.all().filter('email =',
                                               artwork.author_email).get()
    if hasattr(user_profile, 'favorite_count'):
        if user_profile.favorite_count is None:
            user_profile.favorite_count = 1
        else:
            user_profile.favorite_count = user_profile.favorite_count + 1
        user_profile.put()
    else:
        user_profile.favorite_count = 1
        user_profile.put()

    # increase today artwork counter
    today_fav_count = db.TodayFavoriteCounter.all().filter(
        'artwork =', artwork).get()
    if today_fav_count:
        today_fav_count.count = today_fav_count.count + 1
    else:
        today_fav_count = db.TodayFavoriteCounter()
        today_fav_count.artwork = artwork
        today_fav_count.count = 1

    today_fav_count.put()

    return fav_count.count
Exemplo n.º 6
0
def _saveToCache(newItems, fitTrackedData, fitFileHash):
    toAdd = dict(newItems)
    toRemove = set()

    for l in listdir(savesDir):
        savesFile = joinpath(savesDir, l)
        oldSaveItems = readFitFile(savesFile)
        for i,f in oldSaveItems.iteritems():
            if fitTrackedData.get(i) == f:
                toAdd[i] = f
            else:
                toRemove.add(f[0])
        remove(savesFile)

    writeFitFile(toAdd, joinpath(savesDir,fitFileHash))
    cache.delete(toRemove - {toAdd[i][0] for i in toAdd})
    cache.insert({h:(s,f) for f,(h,s) in newItems.iteritems()}, progressMsg='Caching new and modified items')
Exemplo n.º 7
0
def beautify(text, lang, options):
    """
    Process input `text` according to the specified `mode`.
    Adds comments if needed, according to the `lang` rules.
    Caches the results.
    The whole work (except caching) is done by _beautify().
    """

    options = options or {}
    beauty_options = dict((k, v) for k, v in options.items()
                          if k in ['add_comments', 'remove_text'])

    mode = ''
    if beauty_options.get('add_comments'):
        mode += 'c'
    if beauty_options.get('remove_text'):
        mode += 'q'

    if beauty_options == {}:
        # if mode is unknown, just don't transform the text at all
        return text

    if isinstance(text, str):
        text = text.encode('utf-8')
    digest = "t:%s:%s:%s" % (hashlib.md5(text).hexdigest(), lang, mode)

    # temporary added line that removes invalid cache entries
    # that used wrong commenting methods
    if lang in ["git", "django", "flask", "cmake"]:
        cache.delete(digest)

    answer = cache.get(digest)
    if answer:
        return answer
    answer = _beautify(text, lang, **beauty_options)
    cache.put(digest, answer)

    return answer
Exemplo n.º 8
0
def unfollow(leader_email, follower_email):
    memcache_key = cache.MC_FAVORITE_BY_USER + leader_email + '_' + follower_email
    cache.delete(memcache_key)
    cache.delete(cache.MC_USER_PROFILE + leader_email)
    cache.delete(cache.MC_USER_PROFILE + follower_email)

    follow = db.Follow.all().filter('leader_email =', leader_email).filter(
        'follower_email =', follower_email).get()
    if follow:
        follow.delete()
        return True
    else:
        return False
Exemplo n.º 9
0
def unfavorite_artwork(artwork, user_email):
    cache.delete(cache.MC_FAVORITE_BY_USER + str(artwork.key().id()) + '_' +
                 user_email)
    cache.delete(cache.MC_FAVORITE_COUNT + str(artwork.key().id()))

    # delete favorite star
    fav = db.Favorite.all().filter('artwork =',
                                   artwork).filter('user_email =',
                                                   user_email).get()
    if fav:
        fav.delete()

    cache.delete(cache.MC_FAVORITE_BY_USER + str(artwork.key().id()) + '_' +
                 user_email)

    # descrease artwork stars count
    fav_count = db.FavoriteCounter.all().filter('artwork =', artwork).get()
    result = 0
    if fav_count:
        if fav_count.count > 1:
            fav_count.count = fav_count.count - 1
            fav_count.save()
            result = fav_count.count
        else:
            fav_count.delete()

    # descrease author stars count
    user_profile = db.UserProfile.all().filter('email =',
                                               artwork.author_email).get()
    if hasattr(user_profile, 'favorite_count'):
        user_profile.favorite_count = user_profile.favorite_count - 1
        user_profile.put()

    # descrease today stars count
    today_fav_count = db.TodayFavoriteCounter.all().filter(
        'artwork =', artwork).get()
    if today_fav_count:
        if today_fav_count.count > 1:
            today_fav_count.count = today_fav_count.count - 1
            today_fav_count.put()
        else:
            today_fav_count.delete()

    return result
Exemplo n.º 10
0
 def uncache(self):
     """Remove the model from the cache so that a subsequent get will re-retrieve the data from the database"""
     
     cacheKey = "%s.%s" % (self.__class__, self.key())
     cache.delete(cacheKey)
Exemplo n.º 11
0
def set_user_profile(profile):
    profile.put()
    for email in getattr(profile, 'alternative_email', []):
        cache.delete(cache.MC_USER_PROFILE + email)

    cache.delete(cache.MC_USER_PROFILE + profile.email)
    cache.delete(cache.MC_MAIN_PAGE_RECENT_IMAGES_KEY)
    cache.delete(cache.MC_MAIN_PAGE_RECENT_COMMENTS)
    cache.delete(cache.MC_MAIN_PAGE_PRODUCTIVE_ARTISTS)
    cache.delete(cache.MC_MAIN_PAGE_TOP_RATED_ARTISTS)
Exemplo n.º 12
0
def add_notification(notification):
    cache_key = cache.MC_USER_NOTIFICATION_PREFIX + notification.recipient_email
    cache.delete(cache_key)
    notification.put()
Exemplo n.º 13
0
def delete_from_cache(url, hostname):
    return cache.delete(url, hostname)
#!/usr/bin/env python

from cache import delete

delete('expedia_df')
Exemplo n.º 15
0
def render_template_to_response(request, templateName, responseType=HttpResponse, allowDefaultTemplates=False, allowRetry=True, args=None):
    """Renders a specified template to a string, using the specified response type
    (which gets overriden for errors and automatic permanent redirects).
    
    If allowDefaultTemplates is True, allows the default templates built into Kiwi to be found.
    
    Returns a 404 response if the template requested does not exist or the name is illegally formed.
    """
    
    if args == None:
        args = request.args

    filepath = cache.get(templateName, namespace="kiwi.t")
    fromCache = False
    
    if filepath == "":
        # We cached the fact that the file didn't exist
        filepath = None
    elif filepath:
        fromCache = True
    else:
        templateDir = kiwioptions.webDirectoryPath(request)
        filepath = find_template(templateDir, templateName)
        
        if not filepath and allowDefaultTemplates:
            filepath = find_template(kiwioptions.KIWI_TEMPLATES_PATH, templateName)
            
        if filepath:
            cache.set(templateName, filepath, 300, namespace="kiwi.t")
        else:
            cache.set(templateName, "", 60, namespace="kiwi.t")
           
    if not filepath:
        return error404(request)

    # If we get a HEAD request for a page that we'll be rendering automatically, accept it and render it as empty
    if request.method == "HEAD":
        return responseType()

    try:
        # BOGUS consider caching the contents of the file
        f = open(filepath)
        contents = f.read()
        f.close()
        
        # We differentiate Kiwi markup files by a suffix (normally .kiwi)
        if filepath.endswith(kiwioptions.WEB_MARKUP_SUFFIX):
            args["IS_KIWI_MARKUP"] = True
            s = kiwimarkup.render(request, contents, args)         # BOGUS: Cache result
            return responseType(s)
        
        # A Django template file must begin with "{". If no directive is needed, a {# Django #} comment can be used.
        if contents == "" or contents[0] != "{":
            return responseType(contents)
        
        if "{~" in contents:
            # Handle {~ auth ~} and {~ admin ~}
            contents = contents.replace("{~ admin ~}", "")

        args["IS_DJANGO"] = True

        t = Template(contents)                  # BOGUS: Cache this instead of just the pathname
        c = RequestContext(request, args)
        s = t.render(c)
        response = responseType(s)
        return response
        
    except Exception, ex:
        if fromCache:
            # Cache error can be caused by filename changing when app is updated
            # Call ourselves recursively after deleting the cache entry
            cache.delete(templateName, namespace="kiwi.t")
            return render_template_to_response(request, templateName, responseType=responseType,
                                               allowDefaultTemplates=allowDefaultTemplates, allowRetry=False, args=args)
        else:
            return error500(request)
Exemplo n.º 16
0
 def save(self, *args, **kwargs):
     template = super(EmailTemplate, self).save(*args, **kwargs)
     cache.delete(self.name)
     return template