Beispiel #1
0
    def handle(self, *args, **options):
        save_article_list = set()

        # 存文章
        for category_name, article_fliepath_li in article_dict.items():
            for article_filepath in article_fliepath_li:
                article_name = article_filepath.split('/')[-1]
                article_name_no_tail = article_name.split('.')[0]
                # 不允许存在相同标题的文章
                if(article_name_no_tail in save_article_list):
                    self.stdout.write(self.style.ERROR('{} 存储失败,因为有重复的文章\n'.format(article_name_no_tail)))
                    continue
                with open(article_filepath, 'r', encoding='utf-8') as fp:
                    body = fp.read()
                    user = get_user_model().objects.get_or_create(email='*****@*****.**', username='******',
                                                                  password='******')[0]
                    category = Category.objects.get_or_create(name=category_name, parent_category=None)[0]
                    category.save()
                    article = Article.objects.get_or_create(category=category,
                                                            title=article_name_no_tail,
                                                            body=body,
                                                            author=user
                                                            )[0]
                    article.save()
                    self.stdout.write(self.style.SUCCESS('{} 文章保存成功 \n'.format(article_name_no_tail)))
                    save_article_list.add(article_name_no_tail)
                    from DjangoBlog.utils import cache
                    cache.clear()
        self.stdout.write(self.style.SUCCESS('请确认需要保存的文章 {}\n'.format(save_article_list)))
Beispiel #2
0
    def handle(self, *args, **options):
        user = \
            get_user_model().objects.get_or_create(email='*****@*****.**', username='******',
                                                   password='******')[0]

        pcategory = Category.objects.get_or_create(name='我是父类目',
                                                   parent_category=None)[0]

        category = Category.objects.get_or_create(name='子类目',
                                                  parent_category=pcategory)[0]

        category.save()
        basetag = Tag()
        basetag.name = "标签"
        basetag.save()
        for i in range(1, 20):
            article = Article.objects.get_or_create(
                category=category,
                title='nice title ' + str(i),
                body='nice content ' + str(i),
                author=user)[0]
            tag = Tag()
            tag.name = "标签" + str(i)
            tag.save()
            article.tags.add(tag)
            article.tags.add(basetag)
            article.save()

        from DjangoBlog.utils import cache
        cache.clear()
        self.stdout.write(self.style.SUCCESS('created test datas \n'))
Beispiel #3
0
def model_post_save_callback(sender, instance, created, raw, using,
                             update_fields, **kwargs):
    clearcache = False
    if isinstance(instance, LogEntry):
        return
    if 'get_full_url' in dir(instance):
        is_update_views = update_fields == {'views'}
        if not is_update_views:
            clearcache = True
    if isinstance(instance, Comment):

        path = instance.article.get_absolute_url()
        site = get_current_site().domain
        if site.find(':') > 0:
            site = site[0:site.find(':')]

        expire_view_cache(path,
                          servername=site,
                          serverport=80,
                          key_prefix='blogdetail')
        if cache.get('seo_processor'):
            cache.delete('seo_processor')
        comment_cache_key = 'article_comments_{id}'.format(
            id=instance.article.id)
        cache.delete(comment_cache_key)
        delete_sidebar_cache(instance.author.username)
        delete_view_cache('article_comments', [str(instance.article.pk)])

        _thread.start_new(send_comment_email, (instance, ))

    if clearcache:
        cache.clear()
Beispiel #4
0
def model_post_save_callback(sender, instance, created, raw, using, update_fields, **kwargs):
    clearcache = False
    if isinstance(instance, LogEntry):
        return
    if 'get_full_url' in dir(instance):
        is_update_views = update_fields == {'views'}
        if not settings.TESTING and not is_update_views:
            try:
                notify_url = instance.get_full_url()
                SpiderNotify.baidu_notify([notify_url])
            except Exception as ex:
                logger.error("notify sipder", ex)
        if not is_update_views:
            clearcache = True
    if isinstance(instance, Comment):

        path = instance.article.get_absolute_url()
        site = Site.objects.get_current().domain
        if site.find(':') > 0:
            site = site[0:site.find(':')]

        expire_view_cache(path, servername=site, serverport=80, key_prefix='blogdetail')
        if cache.get('seo_processor'):
            cache.delete('seo_processor')
        comment_cache_key = 'article_comments_{id}'.format(id=instance.article.id)
        cache.delete(comment_cache_key)
        delete_view_cache(instance.author.username)

        _thread.start_new(send_comment_email, (instance,))

    if clearcache:
        cache.clear()
    def handle(self, *args, **options):
        user = \
            get_user_model().objects.get_or_create(email='*****@*****.**', username='******',
                                                   password='******')[0]

        pcategory = Category.objects.get_or_create(name='我是父类目', parent_category=None)[0]

        category = Category.objects.get_or_create(name='子类目', parent_category=pcategory)[0]

        category.save()
        basetag = Tag()
        basetag.name = "标签"
        basetag.save()
        for i in range(1, 20):
            article = Article.objects.get_or_create(category=category,
                                                    title='nice title ' + str(i),
                                                    body='nice content ' + str(i),
                                                    author=user
                                                    )[0]
            tag = Tag()
            tag.name = "标签" + str(i)
            tag.save()
            article.tags.add(tag)
            article.tags.add(basetag)
            article.save()

        from DjangoBlog.utils import cache
        cache.clear()
        self.stdout.write(self.style.SUCCESS('created test datas \n'))
Beispiel #6
0
def oauth_user_login_signal_handler(sender, **kwargs):
    id = kwargs['id']
    oauthuser = OAuthUser.objects.get(id=id)
    site = Site.objects.get_current().domain
    if oauthuser.picture and not oauthuser.picture.find(site) >= 0:
        from DjangoBlog.utils import save_user_avatar
        oauthuser.picture = save_user_avatar(oauthuser.picture)
        oauthuser.save()

    delete_view_cache(oauthuser.author.username)

    cache.clear()
Beispiel #7
0
    def form_valid(self, form):
        form = AuthenticationForm(data=self.request.POST, request=self.request)

        if form.is_valid():
            from DjangoBlog.utils import cache
            if cache and cache is not None:
                cache.clear()
            auth.login(self.request, form.get_user())

            return HttpResponseRedirect('/')
        else:
            return self.render_to_response({'form': form})
Beispiel #8
0
def refresh_memcache(request):
    try:

        if request.user.is_superuser:
            from DjangoBlog.utils import cache
            if cache != None: cache.clear()
            return HttpResponse("ok")
        else:
            from django.http import HttpResponseForbidden
            return HttpResponseForbidden()
    except Exception as e:
        return HttpResponse(e)
Beispiel #9
0
def refresh_memcache(request):
    try:

        if request.user.is_superuser:
            from DjangoBlog.utils import cache
            if cache and cache is not None:
                cache.clear()
            return HttpResponse("ok")
        else:
            return HttpResponseForbidden()
    except Exception as e:
        logger.error(e)
        return HttpResponse(e)
Beispiel #10
0
def refresh_memcache(request):
    try:
        if request.user.is_superuser:
            from DjangoBlog.utils import cache
            if cache and cache is not None:
                cache.clear()
                logger.info("cache has been cleared successfully")
            return HttpResponseRedirect('/')
        else:
            return HttpResponseForbidden()
    except Exception as e:
        logger.error(e)
        return HttpResponse(e)
Beispiel #11
0
def refresh_memcache(request):
    try:

        if request.user.is_superuser:
            from DjangoBlog.utils import cache
            if cache and cache is not None:
                cache.clear()
            return HttpResponse("ok")
        else:
            from django.http import HttpResponseForbidden
            return HttpResponseForbidden()
    except Exception as e:
        return HttpResponse(e)
Beispiel #12
0
    def form_valid(self, form):
        form = AuthenticationForm(data=self.request.POST, request=self.request)

        if form.is_valid():
            from DjangoBlog.utils import cache
            if cache and cache is not None:
                cache.clear()
            print(self.redirect_field_name)
            redirect_to = self.request.GET.get(self.redirect_field_name)
            auth.login(self.request, form.get_user())
            return super(LoginView, self).form_valid(form)
            # return HttpResponseRedirect('/')
        else:
            return self.render_to_response({'form': form})
Beispiel #13
0
def oauth_user_login_signal_handler(sender, **kwargs):
    id = kwargs['id']
    oauthuser = OAuthUser.objects.get(id=id)
    site = get_current_site().domain
    if oauthuser.picture and not oauthuser.picture.find(site) >= 0:
        from DjangoBlog.utils import save_user_avatar
        oauthuser.picture = save_user_avatar(oauthuser.picture)
        oauthuser.save()

    delete_sidebar_cache(
        oauthuser.author.username
    )  #用户认证通过后,发送信号过来,信号里删除用户的缓存碎片,.author.username通过外键关联到用户模型

    cache.clear()
Beispiel #14
0
 def form_valid(self, form):
     form = AuthenticationForm(data=self.request.POST, request=self.request)
     if form.is_valid():
         from DjangoBlog.utils import cache
         if cache and cache is not None:
             cache.clear()
         logger.info(self.redirect_field_name)
         auth.login(self.request, form.get_user())
         user = BlogUser.objects.get(email=form.get_user())
         self.request.session["user_id"] = user.id
         self.request.session["user"] = user.email
         logger.info('有人登录拉!!!===> 访客:{},登录成功咯.'.format(user.username))
         return super(LoginView, self).form_valid(form)
         # return HttpResponseRedirect('/')
     else:
         return self.render_to_response({'form': form})
Beispiel #15
0
    def form_valid(self, form):
        form = AuthenticationForm(data=self.request.POST, request=self.request)

        if form.is_valid():
            from DjangoBlog.utils import cache
            if cache and cache is not None:
                cache.clear()
            print(self.redirect_field_name)
            redirect_to = self.request.GET.get(self.redirect_field_name)
            auth.login(self.request, form.get_user())
            return super(LoginView, self).form_valid(form)
            # return HttpResponseRedirect('/')
        else:
            return self.render_to_response({
                'form': form
            })
Beispiel #16
0
    def form_valid(self, form):
        form = CustomAuthForm(data=self.request.POST, request=self.request)

        if form.is_valid():
            from DjangoBlog.utils import cache
            if cache and cache is not None:
                cache.clear()

            auth.login(self.request, form.get_user())
            if self.request.is_ajax():
                logger.info("ajax succesfull login request")
                return JsonResponse({"message": "Success"},
                                    content_type="application/json")

            return super(LoginView, self).form_valid(form)
        else:
            messages.error(self.request, form.errors)
            return self.render_to_response({'form': form})
Beispiel #17
0
    def handle(self, *args, **options):
        user = \
            get_user_model().objects.get_or_create(email='*****@*****.**', username='******',
                                                   password='******')[0]

        pcategory = Category.objects.get_or_create(name='pcategory',
                                                   parent_category=None)[0]

        category = Category.objects.get_or_create(name='category',
                                                  parent_category=pcategory)[0]

        category.save()
        for i in range(1, 10):
            article = Article.objects.get_or_create(
                category=category,
                title='nice title ' + str(i),
                body='nice content ' + str(i),
                author=user)
        from DjangoBlog.utils import cache
        cache.clear()
        self.stdout.write(self.style.SUCCESS('created test datas \n'))
Beispiel #18
0
def article_save_callback(sender, **kwargs):
    id = kwargs['id']
    is_update_views = kwargs['is_update_views']
    type = sender.__name__
    obj = None
    from blog.models import Article, Category, Tag
    if type == 'Article':
        obj = Article.objects.get(id=id)
    elif type == 'Category':
        obj = Category.objects.get(id=id)
    elif type == 'Tag':
        obj = Tag.objects.get(id=id)
    if obj is not None:
        if not settings.TESTING and not is_update_views:
            try:
                notify_url = obj.get_full_url()
                SpiderNotify.baidu_notify([notify_url])
            except Exception as ex:
                logger.error("notify sipder", ex)

    from DjangoBlog.utils import cache
    cache.clear()
Beispiel #19
0
 def save(self, *args, **kwargs):
     super().save(*args, **kwargs)
     cache.clear()
Beispiel #20
0
 def get(self, request, *args, **kwargs):
     from DjangoBlog.utils import cache
     cache.clear()
     logout(request)
     return super(LogoutView, self).get(request, *args, **kwargs)
Beispiel #21
0
 def handle(self, *args, **options):
     cache.clear()
     self.stdout.write(self.style.SUCCESS('Cleared cache\n'))
Beispiel #22
0
def user_auth_callback(sender, request, user, **kwargs):
    if user and user.username:
        logger.info(user)
        delete_view_cache(user.username)
        cache.clear()
Beispiel #23
0
 def save_model(self, request, obj, form, change):
     super(ArticlelAdmin, self).save_model(request, obj, form, change)
     from DjangoBlog.utils import cache
     cache.clear()
Beispiel #24
0
 def save(self, *args, **kwargs):
     super().save(*args, **kwargs)
     from DjangoBlog.utils import cache
     cache.clear()
Beispiel #25
0
 def get(self, request, *args, **kwargs):
     from DjangoBlog.utils import cache
     cache.clear()
     logout(request)
     return super(LogoutView, self).get(request, *args, **kwargs)
Beispiel #26
0
 def handle(self, *args, **options):
     cache.clear()
     self.stdout.write(self.style.SUCCESS('Cleared cache\n'))
Beispiel #27
0
 def handle(self, *args, **options):
     cache.clear()
     self.stdout.write(self.style.SUCCESS('Кэш удален\n'))
Beispiel #28
0
 def save(self, *args, **kwargs):
     super().save(*args, **kwargs)
     from DjangoBlog.utils import cache
     cache.clear()
Beispiel #29
0
 def save_model(self, request, obj, form, change):
     super(ArticlelAdmin, self).save_model(request, obj, form, change)
     from DjangoBlog.utils import cache
     cache.clear()
Beispiel #30
0
def user_auth_callback(sender, request, user, **kwargs):
    logger.info(user)
    cache.clear()
 def save(self, *args, **kwargs):
     # print("models.save!!!!!!!++++++++++!!!")
     super().save(*args, **kwargs)
     from DjangoBlog.utils import cache
     cache.clear()