addons-server/apps/webapps/views.py

116 строки
3.6 KiB
Python
Исходник Обычный вид История

2011-11-20 22:12:54 +04:00
from django.shortcuts import get_object_or_404, redirect
2011-07-30 04:50:33 +04:00
import jingo
2011-10-29 03:20:22 +04:00
from mobility.decorators import mobile_template
from tower import ugettext_lazy as _lazy
import amo
from amo.decorators import json_view, login_required, post_required
2011-11-20 22:12:54 +04:00
from amo.helpers import loc
from amo.utils import paginate
from addons.decorators import addon_view
2011-07-30 04:50:33 +04:00
import addons.views
from webapps.models import Installed
import search.views
2011-07-30 04:50:33 +04:00
from addons.models import Category
from browse.views import category_landing, CategoryLandingFilter
from sharing.views import share as share_redirect
2011-07-30 04:50:33 +04:00
from .models import Webapp
TYPE = amo.ADDON_WEBAPP
def es_app_list(request):
ctx = search.views.app_search_query(request)
return jingo.render(request, 'webapps/listing.html', ctx)
2011-11-20 22:12:54 +04:00
# TODO(cvan): Make a mobile apps homepage when we know what we want.
#@mobile_template('webapps/{mobile/}home.html')
def app_home(request):
2011-11-20 22:12:54 +04:00
if request.MOBILE:
return redirect('apps.list')
src = 'cb-btn-home'
dl_src = 'cb-dl-home'
ctx = {
'section': amo.ADDON_SLUGS[TYPE],
'addon_type': TYPE,
'free': Webapp.objects.top_free()[:18],
'paid': Webapp.objects.top_paid()[:18],
2011-11-20 22:12:54 +04:00
'src': src,
'dl_src': dl_src,
}
return jingo.render(request, 'webapps/home.html', ctx)
class AppCategoryLandingFilter(CategoryLandingFilter):
opts = (('downloads', _lazy(u'Most Popular')),
('rating', _lazy(u'Top Rated')),
('created', _lazy(u'Recently Added')),
('featured', _lazy(u'Featured')))
class AppFilter(addons.views.BaseFilter):
opts = (('downloads', _lazy(u'Weekly Downloads')),
2011-11-20 22:12:54 +04:00
('free', loc(u'Top Free')),
('paid', loc(u'Top Paid')),
('rating', _lazy(u'Top Rated')))
extras = (('created', _lazy(u'Newest')),
('name', _lazy(u'Name')),
('price', loc(u'Price')),
('updated', _lazy(u'Recently Updated')))
def app_listing(request):
qs = Webapp.objects.listed()
filter = AppFilter(request, qs, 'sort', default='downloads', model=Webapp)
return filter.qs, filter
2011-10-29 03:20:22 +04:00
@mobile_template('browse/{mobile/}extensions.html')
def app_list(request, category=None, template=None):
if category is not None:
q = Category.objects.filter(type=TYPE)
category = get_object_or_404(q, slug=category)
sort = request.GET.get('sort')
if not sort and not request.MOBILE and category and category.count > 4:
return category_landing(request, category, TYPE,
AppCategoryLandingFilter)
addons, filter = app_listing(request)
sorting = filter.field
src = 'cb-btn-%s' % sorting
dl_src = 'cb-dl-%s' % sorting
if category:
addons = addons.filter(categories__id=category.id)
addons = paginate(request, addons, count=addons.count())
ctx = {'section': amo.ADDON_SLUGS[TYPE], 'addon_type': TYPE,
'category': category, 'addons': addons, 'filter': filter,
'sorting': sorting, 'sort_opts': filter.opts, 'src': src,
2011-11-20 22:12:54 +04:00
'dl_src': dl_src}
2011-10-29 03:20:22 +04:00
return jingo.render(request, template, ctx)
2011-07-30 04:50:33 +04:00
def share(request, app_slug):
webapp = get_object_or_404(Webapp, app_slug=app_slug)
return share_redirect(request, webapp, webapp.name, webapp.summary)
@json_view
@addon_view
@login_required
@post_required
def record(request, addon):
if addon.is_webapp():
installed, c = Installed.objects.safer_get_or_create(addon=addon,
user=request.amo_user)
return {'addon': addon.pk,
'receipt': installed.receipt if installed else ''}