# Path: apps/pages/views.py
# class PageView(TemplateView):
# 
#     def get_template_names(self):
#         return settings.MYSMILE_THEME + '/page.html'
# 
#     def get_context_data(self, **kwargs):
#         context = super(PageView, self).get_context_data(**kwargs)
#         c = Page.objects.get_content(kwargs['lang'], kwargs['slug'])
#         c.update(Preferences.objects.get_all())
#         c['inav'] = self.get_additional_dynamic_menu(self.request, kwargs['slug'], c['menu'], c['page__ptype'], int(c['MAX_INNERLINK_HISTORY']))
# 
#         c['MYSMILE_THEME'] = settings.MYSMILE_THEME
#         context.update(c)
#         return context
# 
#     def get_additional_dynamic_menu(self, request, slug, menu, ptype, max_innerlink_history):
#         inner_nav = request.session.get('inner_nav', [])
#         if ptype == Page.PTYPE_INNER:
#             if not [slug, menu] in inner_nav:  # work with sessions
#                 inner_nav.append([slug, menu]) # add to dynamic menu
#                 request.session['inner_nav'] = inner_nav  # save data to the session
#                 while len(inner_nav) > max_innerlink_history:
#                     inner_nav.pop(0)
#         return inner_nav
# 
# class PageRedirectView(RedirectView):
# 
#     permanent = False
#     query_string = True
#     pattern_name = 'page'
# 
#     def get_redirect_url(self, *args, **kwargs):
#         # get first slug
#         slug = Page.objects.filter(status=Page.STATUS_PUBLISHED, ptype__in=[Page.PTYPE_MENU, Page.PTYPE_MENU_API]).values_list('slug', flat=True).order_by('sortorder').first()
#         try:
#             lang = kwargs['lang']
#         except KeyError:  # adaptive language selection
#             lang = settings.LANGUAGES[0][0]
#             if 'HTTP_ACCEPT_LANGUAGE' in self.request.META:
#                 for k in settings.LANGUAGES:
#                     if k[0] in self.request.META['HTTP_ACCEPT_LANGUAGE']:
#                         lang = k[0]
#                         k = 0  # break cycle "for"
#         return super(PageRedirectView, self).get_redirect_url(lang=lang, slug=slug)


from django.conf.urls import patterns, url
from apps.pages.views import PageView, PageRedirectView



urlpatterns = patterns('',
    url(r'^$', PageRedirectView.as_view(), name='home'),
    url(r'^(?P<lang>[a-z]{2})/$', PageRedirectView.as_view(), name='page'),
    url(r'^(?P<lang>[a-z]{2})/(?P<slug>[a-zA-Z0-9-]+)\.html$', PageView.as_view(), name='page'),

# Path: apps/pages/views.py
# class PageView(TemplateView):
# 
#     def get_template_names(self):
#         return settings.MYSMILE_THEME + '/page.html'
# 
#     def get_context_data(self, **kwargs):
#         context = super(PageView, self).get_context_data(**kwargs)
#         c = Page.objects.get_content(kwargs['lang'], kwargs['slug'])
#         c.update(Preferences.objects.get_all())
#         c['inav'] = self.get_additional_dynamic_menu(self.request, kwargs['slug'], c['menu'], c['page__ptype'], int(c['MAX_INNERLINK_HISTORY']))
# 
#         c['MYSMILE_THEME'] = settings.MYSMILE_THEME
#         context.update(c)
#         return context
# 
#     def get_additional_dynamic_menu(self, request, slug, menu, ptype, max_innerlink_history):
#         inner_nav = request.session.get('inner_nav', [])
#         if ptype == Page.PTYPE_INNER:
#             if not [slug, menu] in inner_nav:  # work with sessions
#                 inner_nav.append([slug, menu]) # add to dynamic menu
#                 request.session['inner_nav'] = inner_nav  # save data to the session
#                 while len(inner_nav) > max_innerlink_history:
#                     inner_nav.pop(0)
#         return inner_nav
# 
# class PageRedirectView(RedirectView):
# 
#     permanent = False
#     query_string = True
#     pattern_name = 'page'
# 
#     def get_redirect_url(self, *args, **kwargs):
#         # get first slug
#         slug = Page.objects.filter(status=Page.STATUS_PUBLISHED, ptype__in=[Page.PTYPE_MENU, Page.PTYPE_MENU_API]).values_list('slug', flat=True).order_by('sortorder').first()
#         try:
#             lang = kwargs['lang']
#         except KeyError:  # adaptive language selection
#             lang = settings.LANGUAGES[0][0]
#             if 'HTTP_ACCEPT_LANGUAGE' in self.request.META:
#                 for k in settings.LANGUAGES:
#                     if k[0] in self.request.META['HTTP_ACCEPT_LANGUAGE']:
#                         lang = k[0]
#                         k = 0  # break cycle "for"
#         return super(PageRedirectView, self).get_redirect_url(lang=lang, slug=slug)


# Path: apps/pages/urls.py
from django.conf.urls import patterns, url
from apps.pages.views import PageView, PageRedirectView




urlpatterns = patterns('',
    url(r'^$', PageRedirectView.as_view(), name='home'),
    url(r'^(?P<lang>[a-z]{2})/$', PageRedirectView.as_view(), name='page'),
    url(r'^(?P<lang>[a-z]{2})/(?P<slug>[a-zA-Z0-9-]+)\.html$', PageView.as_view(), name='page'),

    )