Пример #1
0
def api_opensearch(request):
    if request.GET.has_key("keyword"):
        words = request.GET["keyword"].split("/")
        if len(words) > 1 and words[1] and words[0]:
            snippets = Snippet.all().filter("language", words[1]).search(words[0]).order("-published_at")
        elif len(words) > 1 and words[1]:
            snippets = Snippet.all().filter("language", words[1]).order("-published_at")
        elif words[0]:
            snippets = Snippet.all().search(words[0]).order("-published_at").fetch(50)
        else:
            snippets = Snippet.all().order("-published_at").fetch(50)

    data = []
    for s in snippets:
        tags = [t.name for t in s.get_tags()]
        data.append(
            {
                "title": s.title,
                "language": s.language,
                "code": s.code,
                "comment": s.comment,
                "url": s.url,
                "author": s.author.email,
                "pubDate": s.published_at,
                "tags": tags,
            }
        )
    return json_response(data)
Пример #2
0
def snippet_edit(request, snippet_id, profile_callback=None):
    snippet = Snippet.get_by_id(int(snippet_id))
    if not snippet:
        raise Http404

    if request.method == "GET":
        tags = ""
        if snippet.tags:
            tags = ",".join([t.name for t in snippet.get_tags() if t])
        form = SnippetForm(
            {
                "title": snippet.title,
                "language": snippet.language,
                "code": snippet.code,
                "url": snippet.url,
                "comment": snippet.comment,
                "private": snippet.private,
                "tag": tags,
            }
        )
    if request.method == "POST":
        form = SnippetForm(request.POST)
        logging.getLogger().debug(form)
        if form.is_valid():
            updated_snippet = form.edit(int(snippet_id), profile_callback=profile_callback)
            memcache.flush_all()
            ping()
            return HttpResponseRedirect(updated_snippet.get_absolute_url())

    return render_to_response(
        "snippet/snippet_edit.html", {"form": form, "snippet": snippet}, context_instance=RequestContext(request)
    )
Пример #3
0
def del_fav_snippet(request, snippet_id):
    userPref = UserPref.get_or_insert_by_user(users.get_current_user())
    if not userPref: return JsonResponse(-1)
    snippet = Snippet.get_by_id(int(snippet_id))
    if not snippet: return JsonResponse(-2)
    UserFavSnippet.objects.del_fav(userPref, snippet)
    return JsonResponse(1)
Пример #4
0
def user_favorites(request, user_id):
    user = UserPref.get_by_id(int(user_id))
    if not user: raise Http404
    favorites = UserFavSnippet.all().filter('user', user.key()).order('-inserted_at')
    favorites_count = favorites.count()
    snippets_count = Snippet.all().filter('author', user.key()).count()
    rev_tags, rev_languages = get_relevent_lang_tags(user)        
    return object_list(request, queryset=favorites, allow_empty=True,
                template_name='account/user_favorites.html', extra_context={'CURRENT_USER': user, 'CURRENT_PAGE': 'user_snippets', 'RELEVENT_TAGS': rev_tags, 'RELEVENT_LANGUAGES':rev_languages, 'CURRENT_USER_SNIPPERS_COUNT':snippets_count, 'CURRENT_USER_FAVORITES_COUNT':favorites_count},
                paginate_by=settings.SNIPPET_LIST_PAGE_SIZE)
Пример #5
0
def user_tag_snippets(request, tag_name, tag_id, user_id):
    user = UserPref.get_by_id(int(user_id))
    if not user: raise Http404
    tag = Tag.get_by_id(int(tag_id))
    if not tag: raise Http404
    snippets = Tag.objects.snippets(tag)
    snippets = snippets.filter('author', user.key()).order('-published_at')
    snippets_count = Snippet.all().filter('author', user.key()).count()
    rev_tags, rev_languages = get_relevent_lang_tags(user)
    return object_list(request, queryset=snippets, allow_empty=True,
                template_name='account/user_detail.html', extra_context={'CURRENT_USER': user, 'CURRENT_PAGE': 'user_snippets', 'CURRENT_TAG': tag, 'RELEVENT_TAGS': rev_tags, 'RELEVENT_LANGUAGES':rev_languages, 'CURRENT_USER_SNIPPERS_COUNT': snippets_count},
                paginate_by=settings.SNIPPET_LIST_PAGE_SIZE)
Пример #6
0
def snippet_list(request):
    snippets = Snippet.all().order("-published_at")

    rev_tags = Tag.all().order("-number").order("-inserted_at").fetch(settings.RELEVENT_TAG_LIMIT)

    return object_list(
        request,
        queryset=snippets,
        allow_empty=True,
        template_name="snippet/snippet_list.html",
        extra_context={"is_admin": is_admin(), "CURRENT_PAGE": "snippet_list", "RELEVENT_TAGS": rev_tags},
        paginate_by=settings.SNIPPET_LIST_PAGE_SIZE,
    )
Пример #7
0
def snippet_search(request):
    if request.GET.has_key("keyword"):
        keyword = request.GET["keyword"]
        logging.getLogger().info(keyword)
        if request.GET.has_key("search_language") and request.GET["search_language"]:
            snippets = (
                Snippet.all().filter("language", request.GET["search_language"]).search(keyword).order("-published_at")
            )
        else:
            snippets = Snippet.all().search(keyword).order("-published_at")
    else:
        if request.GET.has_key("search_language") and request.GET["search_language"]:
            snippets = Snippet.all().filter("language", request.GET["search_language"]).order("-published_at")
        else:
            snippets = Snippet.all().order("-published_at")
    rev_tags = Tag.all().order("-number").order("-inserted_at").fetch(settings.RELEVENT_TAG_LIMIT)
    return object_list(
        request,
        queryset=snippets,
        allow_empty=True,
        template_name="snippet/snippet_list.html",
        extra_context={"is_admin": is_admin(), "CURRENT_PAGE": "snippet_list", "RELEVENT_TAGS": rev_tags},
        paginate_by=settings.SNIPPET_LIST_PAGE_SIZE,
    )
Пример #8
0
def user_language_snippets(request, language, user_id):
    user = UserPref.get_by_id(int(user_id))
    if not user: raise Http404
    snippets = Snippet.all().filter('author', user.key()).order('-published_at')
    snippets_count = snippets.count()
    snippets = snippets.filter('language', language).order('-published_at')
    rev_tags, rev_languages = get_relevent_lang_tags(user, language)
    for l, v in settings.SUPPORT_LANGUAGES.items():
        if l == language:
            current_language = {'name':v, 'key':l}
            break;
            
    return object_list(request, queryset=snippets, allow_empty=True,
                template_name='account/user_detail.html', extra_context={'CURRENT_USER': user, 'CURRENT_PAGE': 'user_snippets', 'CURRENT_LANGUAGE': current_language, 'RELEVENT_TAGS': rev_tags, 'RELEVENT_LANGUAGES':rev_languages, 'CURRENT_USER_SNIPPERS_COUNT': snippets_count},
                paginate_by=settings.SNIPPET_LIST_PAGE_SIZE)
Пример #9
0
def snippet_detail(request, snippet_id, snippet_language, snippet_title):
    snippet = Snippet.get_by_id(int(snippet_id))
    if not snippet:
        raise Http404
    svlm = SnippetVisitLogManager()
    svlm.log(snippet)
    if request.method == "POST":
        snippet.add_comment(request.POST["commentContent"])
    memcache.flush_all()
    subversions = snippet.get_subversions()
    comments = snippet.get_comments()
    return render_to_response(
        "snippet/snippet_detail.html",
        {"snippet": snippet, "subversions": subversions, "comments": comments},
        context_instance=RequestContext(request),
    )
Пример #10
0
def language_snippets(request, language):
    snippets = Snippet.all().filter("language", language).order("-published_at")
    for l, v in settings.SUPPORT_LANGUAGES.items():
        if l == language:
            current_language = {"name": v, "key": l}
            break
    rev_tags = (
        Tag.all()
        .filter("languages", language)
        .order("-number")
        .order("-inserted_at")
        .fetch(settings.RELEVENT_TAG_LIMIT)
    )

    return object_list(
        request,
        queryset=snippets,
        allow_empty=True,
        template_name="snippet/snippet_list.html",
        extra_context={"CURRENT_LANGUAGE": current_language, "CURRENT_PAGE": "snippet_list", "RELEVENT_TAGS": rev_tags},
        paginate_by=settings.SNIPPET_LIST_PAGE_SIZE,
    )
Пример #11
0
def sitemap(request):
    str = (
        """<?xml version="1.0" encoding="UTF-8"?>
        <urlset xmlns="http://www.google.com/schemas/sitemap/0.84" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.google.com/schemas/sitemap/0.84 http://www.google.com/schemas/sitemap/0.84/sitemap.xsd">
            <url>
                <loc>%s/</loc>
                <changefreq>daily</changefreq>
                <priority>0.9</priority>
            </url>
        """
        % settings.SITE_DOMAIN
    )

    for alias, lang in settings.SUPPORT_LANGUAGES.items():
        str += """<url>
                    <loc>%s%s</loc>
                      <changefreq>daily</changefreq>
                      <priority>0.8</priority>
                   </url>
                """ % (
            settings.SITE_DOMAIN,
            reverse("language_snippets", kwargs={"language": alias}),
        )
    for tag in Tag.all():
        str += """<url>
                    <loc>%s%s</loc>
                      <changefreq>daily</changefreq>
                      <priority>0.8</priority>
                   </url>
                """ % (
            settings.SITE_DOMAIN,
            reverse("tag_snippets", kwargs={"tag_name": tag.get_escape_name(), "tag_id": tag.key().id()}),
        )

    for alias, lang in settings.SUPPORT_LANGUAGES.items():
        tags = Tag.all().filter("languages", alias)
        if not tags:
            continue
        for tag in tags:
            str += """<url>
                        <loc>%s%s</loc>
                          <changefreq>daily</changefreq>
                          <priority>0.8</priority>
                       </url>
                    """ % (
                settings.SITE_DOMAIN,
                reverse(
                    "language_tag_snippets",
                    kwargs={"language": alias, "tag_name": tag.get_escape_name(), "tag_id": tag.key().id()},
                ),
            )

    for snippet in Snippet.all():
        str += """<url>
                    <loc>%s%s</loc>
                    <changefreq>monthly</changefreq>
                    <priority>0.5</priority>
                </url>
                """ % (
            settings.SITE_DOMAIN,
            snippet.get_absolute_url(),
        )

    str += "</urlset>"
    return HttpResponse(str, content_type="text/xml")
Пример #12
0
 def items(self, obj):
     return Snippet.all().filter('author', obj.key()).order('-published_at')[:50]
Пример #13
0
 def items(self):
     return Snippet.all().order('-published_at')[:50]
Пример #14
0
 def items(self, obj):
     return Snippet.all().filter('language', obj).order('-published_at')[:50]
Пример #15
0
 def count_both(self, ta, tb):
     return Snippet.all().filter('tags', ta.key()).filter('tags', tb.key()).count()
Пример #16
0
 def count_tag(self, tag):
     return Snippet.all().filter('tags', tag.key()).count()