def get_object(self, queryset=None): try: banner = self.model.objects.get() except Banner.DoesNotExist: banner = Banner() banner.save() return banner
def get_object(self, queryset=None): try: banner = self.model.objects.get() except Banner.DoesNotExist: banner = Banner() banner.save() banner.enabled = False banner.title_en = None banner.title_pl = None banner.description_en = None banner.description_pl = None return banner
def home(request): if request.method == 'POST': banner_form = BannerForm(request.POST) if banner_form.is_valid(): banner_model = Banner() banner_model.name = banner_form.cleaned_data['name'] banner_model.user = request.user banner_model.save() return HttpResponseRedirect('/') banner_list = Banner.objects.filter(user=request.user) return render(request, 'home.html', { 'banner_list': banner_list, 'form': banner_form }) else: banner_list = Banner.objects.filter(user=request.user) banner_form = BannerForm() return render(request, 'home.html', { 'banner_list': banner_list, 'form': banner_form })
def save_or_update(self, request): '''新增和修改banner ''' banner_id = request.POST.get('banner_id', '') name = request.POST.get('name', '') original = request.POST.get('original', '') thumbnail = request.POST.get('thumbnail', '') background = request.POST.get('background', '') background_image_width = request.POST.get('background_image_width', '') background_image_color = request.POST.get('background_image_color', '') location = request.POST.get('page_url', '') order = request.POST.get('order', '') update_user = '' if 'session_id' in request.COOKIES: session_id = request.COOKIES['session_id'] update_user = self.cache.get(session_id)['username'] if banner_id: ban = Banner.objects.filter(id=banner_id) ban = ban[0] else: ban = Banner() ban.order = 1 ban.is_active = 1 ban.is_preview = 1 ban.name = name ban.original = original ban.thumbnail = thumbnail ban.background = background ban.background_image_width = background_image_width ban.background_image_color = background_image_color ban.location = location ban.update_user = update_user ban.save() return {'statusCode': 1}
def init_banner_data(self): '''初始化数据,把banner数据复制一份到预览数据 ''' ban = Banner.objects.filter(is_preview=True) if ban: return {'statusCode':1, 'msg': 'preview data is already exist'} online_banner = Banner.objects.filter(is_preview=False) for dra in online_banner: ban = Banner() ban.name = dra.name ban.original = dra.original ban.thumbnail = dra.thumbnail ban.background = dra.background ban.background_image_width = dra.background_image_width ban.background_image_color = dra.background_image_color ban.location = dra.location ban.order = dra.order ban.is_active = dra.is_active ban.update_user = dra.update_user ban.is_preview = True ban.save() return {'statusCode':1, 'msg': 'success!'}
def publish_banner(self): '''发布banner图 ''' # 1.复制新的banner草稿到线上banner draft_banner = Banner.objects.filter(is_preview=True,is_active=1) res_json = {'statusCode': 0, 'msg': ''} if draft_banner: # 2.删除老的线上banner old_banner = Banner.objects.filter(is_preview=False) old_banner.delete() for dra in draft_banner: ban = Banner() ban.name = dra.name ban.original = dra.original ban.thumbnail = dra.thumbnail ban.background = dra.background ban.background_image_width = dra.background_image_width ban.background_image_color = dra.background_image_color ban.location = dra.location ban.order = dra.order ban.is_active = 1 ban.update_user = dra.update_user ban.is_preview = False ban.save() res_json['statusCode'] = 1 else: res_json['statusCode'] = 0 res_json['msg'] = '预览数据异常,请检查后重试' return res_json
def test_resolve_banner(self): # create dummy request object request = type('Request', (object,), {}) request.urlconf = 'banner.tests' request.path = '/some/path' # setup unpublished banner unpublished_banner = Banner() unpublished_banner.save() # setup banneroption option = BannerOption(banner=unpublished_banner, banner_preferences=preferences.BannerPreferences, url_name="test_view_name", position="header") option.save() # unpublished banners should not be returned self.failIfEqual(unpublished_banner, banner_inclusion_tags.resolve_banner(request, 'header')) # setup published banner web_site = Site(domain="web.address.com") web_site.save() settings.SITE_ID = web_site.id published_banner = Banner(state="published") published_banner.save() published_banner.sites.add(web_site) # setup banneroption option = BannerOption(banner=published_banner, banner_preferences=preferences.BannerPreferences, url_name="test_view_name", position="header") option.save() # published banners should be returned self.failUnlessEqual(published_banner, banner_inclusion_tags.resolve_banner(request, 'header')) # banner should not be returned of its position does not correspond to a gizmo slot self.failIfEqual(published_banner, banner_inclusion_tags.resolve_banner(request, 'bogus slot')) # setup another published banner web_site = Site(domain="web.address.com") web_site.save() settings.SITE_ID = web_site.id published_banner2 = Banner(state="published") published_banner2.save() published_banner2.sites.add(web_site) # setup banneroption by url option = BannerOption(banner=published_banner2, banner_preferences=preferences.BannerPreferences, url_name="test_view_name", url="/some/path", position="header") option.save() # banner with url specified takes priority self.failIfEqual(published_banner2, banner_inclusion_tags.resolve_banner(request, 'header')) # setup another published banner web_site = Site(domain="web.address.com") web_site.save() settings.SITE_ID = web_site.id published_banner3 = Banner(state="published") published_banner3.save() published_banner3.sites.add(web_site) # setup banneroption option = BannerOption(banner=published_banner3, banner_preferences=preferences.BannerPreferences, url_name="test_view_name", position="header", is_default=True) option.save() # in case no banner is found directly, fall back to default for gizmo slot request.path='/no/specified/banners' self.failUnlessEqual(published_banner3, banner_inclusion_tags.resolve_banner(request, 'header'))