Exemple #1
0
def recent_snippet(request, page=1, limit=FETCH_LIMIT):
    """
        Return the list of recently posted snippets.

        Processes GET and POST requests.
    """
    limit = int(limit)
    if limit > FETCH_LIMIT:
        limit = FETCH_LIMIT

    query = Snippet.all()
    query.order("-date")
    snippets = query.fetch(limit)

    try:
        p = Paginator(snippets, FETCH_PER_PAGE)
        elems, pages = p[int(page)]
    except (ValueError, AssertionError):
        if snippets:
            return webapp2.abort(404, "Invalid page number: {0}".format(page))
        else:
            elems = []
            pages = []

    return render_to_response("list.html", snippets=elems, pages=pages, url=u"/recent/")
Exemple #2
0
def sitemap(request):
    """
        Generate sitemap.xml.

        Processes GET and POST requests.
    """
    keys = Snippet.all(keys_only=True).order("-date")
    jinja = jinja2.get_jinja2(app=webapp2.get_app())
    return create_response({"Content-Type": "text/xml; charset=utf-8"}, jinja.render_template("sitemap.xml", keys=keys))
Exemple #3
0
 def post(self):
     user = user_from_email(self.request.get('email'))
     d = date_for_retrieval()
     all_snippets = Snippet.all().filter("date =", d).fetch(500)
     all_users = User.all().fetch(500)
     following = compute_following(user, all_users)
     logging.info(all_snippets)
     body = '\n\n\n'.join([self.__snippet_to_text(s) for s in all_snippets if s.user.email in following])
     if body:
         self.__send_mail(user.email, 'https://snippetless.appspot.com\n\n' + body)
     else:
         logging.info(user.email + ' not following anybody.')
Exemple #4
0
def search_snippet(request, querystr="", page=1, limit=FETCH_LIMIT):
    """
        Return the list of snippets that meet the given requirements (author, language, etc)

        Processes GET and POST requests.

        Requirements are specified in search request, i.e. a query:
            author:James Black,tags:coolstuff,language:C++
        will return a list of all code snippets written by James Black
        in C++ and tagged as 'coolstuff' (all conditions should be fulfiled)

        NOTE: the delimeter is a ',' character

        List of snippet properties consits of:
            language
            author
            tags
            title
    """
    limit = int(limit)
    if limit > FETCH_LIMIT:
        limit = FETCH_LIMIT

    if request.get("search"):
        querystr = urllib.unquote(request.get("search"))
    else:
        querystr = urllib.unquote(querystr).decode("utf-8")

    pattern = ur"(author|language|tags|title):([^,]+),?"
    conditions = re.findall(pattern, querystr)

    query = Snippet.all()
    for key, value in conditions:
        query.filter("{0} =".format(key), value)
    query.order("-date")
    snippets = query.fetch(limit)

    try:
        p = Paginator(snippets, FETCH_PER_PAGE)
        elems, pages = p[int(page)]
    except (ValueError, AssertionError):
        if snippets:
            return webapp2.abort(404, "Invalid page number: {0}".format(page))
        else:
            elems = []
            pages = []

    return render_to_response("list.html", snippets=elems, pages=pages, url=u"/search/{0}/".format(querystr))
Exemple #5
0
    def post(self):
        user = user_from_email(self.request.get('email'))
        d = date_for_retrieval()
        all_snippets = Snippet.all().filter("digest_date =", d).filter("replaced =", False).fetch(500)
        logging.info(all_snippets)

        def snippet_to_text(snippet):
            divider = '-' * 30
            return '%s\n%s\n%s' % (snippet.user.pretty_name(), divider, snippet.text)

        body = '\n\n\n'.join(snippet_to_text(s) for s in all_snippets)

        if body:
            self.__send_mail(user.email, body)
        else:
            logging.info(user.email + 'not following anybody.')
Exemple #6
0
def list_snippet(request, key, value, page=1, limit=FETCH_LIMIT):

    limit = int(limit)
    if limit > FETCH_LIMIT:
        limit = FETCH_LIMIT

    value = urllib.unquote(value).decode("utf-8")

    query = Snippet.all()
    query.filter("{0} =".format(key), value)
    query.order("-date")
    snippets = query.fetch(int(limit))

    try:
        p = Paginator(snippets, FETCH_PER_PAGE)
        elems, pages = p[int(page)]
    except (ValueError, AssertionError):
        if snippets:
            return webapp2.abort(404, "Invalid page number: {0}".format(page))
        else:
            elems = []
            pages = []

    return render_to_response("list.html", snippets=elems, pages=pages, url=u"/{0}/{1}/".format(key, value))