Exemplo n.º 1
0
def tile_view_pre(request, tile_id, template_name="kinger/revision/axis_tile_view_pre.html"):
    user = request.user
    data = json.dumps({'status':"error"})
    ty = request.GET.get('ty')
    cid = request.GET.get('cid')
    if ty == 'theme' and tile_id == '0':
        if cid != '0':
            tiles = Tile.objects.filter(new_category_id=cid)[:1]
        else:
            tiles = Tile.objects.filter(new_category_id__gt=2400,new_category_id__lte=2412)[:1]
        if tiles.count():
            tile = tiles[0]
        else:
            return HttpResponse(data)
    else:
        try:
            tile = Tile.objects.get(pk=tile_id)
        except:
            return HttpResponse(data)
    
    is_last_page = True
    if tile.n_comments > 0:
        comments = Comment.objects.for_model(tile).select_related('user')\
            .order_by("-submit_date").filter(is_public=True).filter(is_removed=False)
        if comments.count() > 5:
            comments = comments[0:5]
            is_last_page = False 
    else:
        comments = None
        
    ctx = {}
    ctx.update({"tile": tile,"comments": comments,"is_last_page":is_last_page})
    data = render(request, template_name,ctx)
    con=data.content
    return ajax_ok('成功',con)
Exemplo n.º 2
0
def get_tile_n_comments(request):
    tid = request.POST.get("tid")
    try:
        tile = Tile.objects.get(id=tid)
        comments = Comment.objects.for_model(tile).select_related('user').filter(is_public=True).filter(is_removed=False)
        data = json.dumps({'num':comments.count()})
        return HttpResponse(data)
    except:
        data = json.dumps({'num':0})
        return HttpResponse(data)
Exemplo n.º 3
0
def show_comment(request, id='', template_name="kinger/revision/includes/comments_show.html"):
    tile = Tile.objects.get(id=id)
    is_last_page = True
    if tile.n_comments > 0:
        comments = Comment.objects.for_model(tile).select_related('user')\
            .order_by("-submit_date").filter(is_public=True).filter(is_removed=False)
        if comments.count() > 5:
            is_last_page = False 
    else:
        comments = None
    return render(request, template_name,{"tile": tile,'comments':comments,"is_last_page":is_last_page})
Exemplo n.º 4
0
 def render(self, context):
     obj = self.obj.resolve(context)
     content_type = ContentType.objects.get_for_model(obj)
     comments = Comment.objects.filter(
         site=Site.objects.get_current(),
         content_type=content_type,
         object_pk=unicode(obj.pk),
         flags__flag='editors comment')
     if comments.count() == 0:
         context[self.as_varname] = None
     else:
         context[self.as_varname] = comments[0]
         for extra in list(comments[1:]):
             extra.delete()
     return ''
Exemplo n.º 5
0
 def __init__(self, data=None, instance=None):
     forms.Form.__init__(self, data)
     self.instance = instance
     if self.instance:
         self.content_type = ContentType.objects.get_for_model(
             self.instance)
         comments = Comment.objects.filter(site=self.instance.site,
                                           content_type=self.content_type,
                                           object_pk=unicode(
                                               self.instance.pk),
                                           flags__flag='editors comment')
         if not comments.count():
             self.comment = None
         else:
             self.comment = comments[0]
             for extra in list(comments[1:]):
                 extra.delete()
             self.initial['editors_comment'] = self.comment.comment
     else:
         self.comment = None
Exemplo n.º 6
0
 def __init__(self, data=None, instance=None):
     forms.Form.__init__(self, data)
     self.instance = instance
     if self.instance:
         self.content_type = ContentType.objects.get_for_model(
             self.instance)
         comments = Comment.objects.filter(
             site=self.instance.site,
             content_type=self.content_type,
             object_pk=unicode(self.instance.pk),
             flags__flag='editors comment')
         if not comments.count():
             self.comment = None
         else:
             self.comment = comments[0]
             for extra in list(comments[1:]):
                 extra.delete()
             self.initial['editors_comment'] = self.comment.comment
     else:
         self.comment = None
Exemplo n.º 7
0
def more_comment(request, tile_id,template_name="kinger/revision/includes/comments_show.html"):
    """ 加载更多评论 """
    tile = get_object_or_404(Tile, pk=tile_id)
    cpage = int(request.GET.get("cpage",1))
    
    is_last_page = True 
    if tile.n_comments > 0:
        comments = Comment.objects.for_model(tile).select_related('user')\
            .order_by("-submit_date").filter(is_public=True).filter(is_removed=False)
    else:
        comments = None

    start = (cpage - 1) * 10 + 5
#    start = 0
    end = cpage * 10 + 5
    
    if end < comments.count():
        is_last_page = False 

    comments = comments[start:end]
    data = render(request, template_name,{"tile": tile,'comments':comments,"cpage":cpage,"is_last_page":is_last_page})
    con=data.content
    return ajax_ok('成功',con)
Exemplo n.º 8
0
def delete_comment(request, comment_id,template_name="kinger/revision/includes/comments_show.html"):
    """ 评论删除后,减少对应*瓦片*的评论数(冗余字段), 并跳转且作出提示 """
    comment = get_object_or_404(Comment, pk=comment_id, site__pk=settings.SITE_ID)
    if request.user == comment.user:
        perform_delete(request, comment)
        comment.content_object.after_del_comments()
#        messages.success(request, _("Comment deleted success"))
    else:
#        messages.error(request, _("You can't delete this comment"))
        pass
        
    tile = comment.content_object
    is_last_page = True
    if tile.n_comments > 0:
        comments = Comment.objects.for_model(tile).select_related('user')\
            .order_by("-submit_date").filter(is_public=True).filter(is_removed=False)
        if comments.count() > 5:
            is_last_page = False
    else:
        comments = None
    data = render(request, template_name,{"tile": tile,'comments':comments,"is_last_page":is_last_page})
    con=data.content
#    print con,'ccccccccccc'
    return ajax_ok('成功',con)
Exemplo n.º 9
0
    def handle(self, **options):
        current_site = Site.objects.get_current()
        client = DisqusClient()
        verbosity = int(options.get('verbosity'))
        dry_run = bool(options.get('dry_run'))
        state_file = options.get('state_file')
        last_exported_id = None

        if state_file is not None and os.path.exists(state_file):
            last_exported_id = self._get_last_state(state_file)

        comments = self._get_comments_to_export(last_exported_id)
        comments_count = comments.count()
        if verbosity >= 1:
            print("Exporting %d comment(s)" % comments_count)

        # if this is a dry run, we output the comments and exit
        if dry_run:
            print(comments)
            return
        # if no comments were found we also exit
        if not comments_count:
            return

        # Get a list of all forums for an API key. Each API key can have
        # multiple forums associated. This application only supports the one
        # set in the DISQUS_WEBSITE_SHORTNAME variable
        forum_list = client.get_forum_list(
            user_api_key=settings.DISQUS_API_KEY)
        try:
            forum = [
                f for f in forum_list
                if f['shortname'] == settings.DISQUS_WEBSITE_SHORTNAME
            ][0]
        except IndexError:
            raise CommandError("Could not find forum. " +
                               "Please check your " +
                               "'DISQUS_WEBSITE_SHORTNAME' setting.")

        # Get the forum API key
        forum_api_key = client.get_forum_api_key(
            user_api_key=settings.DISQUS_API_KEY, forum_id=forum['id'])

        for comment in comments:
            if verbosity >= 1:
                print("Exporting comment '%s'" % comment)

            # Try to find a thread with the comments URL.
            url = 'http://%s%s' % (current_site.domain,
                                   comment.content_object.get_absolute_url())
            thread = client.get_thread_by_url(url=url,
                                              forum_api_key=forum_api_key)

            # if no thread with the URL could be found, we create a new one.
            # to do this, we first need to create the thread and then
            # update the thread with a URL.
            if not thread:
                thread = client.thread_by_identifier(
                    forum_api_key=forum_api_key,
                    identifier=str(comment.content_object),
                    title=str(comment.content_object),
                )['thread']
                client.update_thread(forum_api_key=forum_api_key,
                                     thread_id=thread['id'],
                                     url=url)

            # name and email are optional in contrib.comments but required
            # in DISQUS. If they are not set, dummy values will be used
            client.create_post(
                forum_api_key=forum_api_key,
                thread_id=thread['id'],
                message=comment.comment.encode('utf-8'),
                author_name=comment.userinfo.get('name',
                                                 'nobody').encode('utf-8'),
                author_email=comment.userinfo.get('email',
                                                  '*****@*****.**'),
                author_url=comment.userinfo.get('url', ''),
                created_at=comment.submit_date.strftime('%Y-%m-%dT%H:%M'))
            if state_file is not None:
                self._save_state(state_file, comment.pk)
Exemplo n.º 10
0
    def handle(self, **options):
        current_site = Site.objects.get_current()
        client = DisqusClient()
        verbosity = int(options.get('verbosity'))
        dry_run = bool(options.get('dry_run'))
        state_file = options.get('state_file')
        last_exported_id = None

        if state_file is not None and os.path.exists(state_file):
            last_exported_id = self._get_last_state(state_file)

        comments = self._get_comments_to_export(last_exported_id)
        comments_count = comments.count()
        if verbosity >= 1:
            print "Exporting %d comment(s)" % comments_count

        # if this is a dry run, we output the comments and exit
        if dry_run:
            print comments
            return
        # if no comments were found we also exit
        if not comments_count:
            return

        # Get a list of all forums for an API key. Each API key can have 
        # multiple forums associated. This application only supports the one 
        # set in the DISQUS_WEBSITE_SHORTNAME variable
        forum_list = client.get_forum_list(user_api_key=settings.DISQUS_API_KEY)
        try:
            forum = [f for f in forum_list\
                     if f['shortname'] == settings.DISQUS_WEBSITE_SHORTNAME][0]
        except IndexError:
            raise CommandError("Could not find forum. " +
                               "Please check your " +
                               "'DISQUS_WEBSITE_SHORTNAME' setting.")

        # Get the forum API key
        forum_api_key = client.get_forum_api_key(
            user_api_key=settings.DISQUS_API_KEY,
            forum_id=forum['id'])

        for comment in comments:
            if verbosity >= 1:
                print "Exporting comment '%s'" % comment

            # Try to find a thread with the comments URL.
            url = 'http://%s%s' % (
                current_site.domain,
                comment.content_object.get_absolute_url())
            thread = client.get_thread_by_url(
                url=url,
                forum_api_key=forum_api_key)

            # if no thread with the URL could be found, we create a new one.
            # to do this, we first need to create the thread and then 
            # update the thread with a URL.
            if not thread:
                thread = client.thread_by_identifier(
                    forum_api_key=forum_api_key,
                    identifier=unicode(comment.content_object),
                    title=unicode(comment.content_object),
                )['thread']
                client.update_thread(
                    forum_api_key=forum_api_key,
                    thread_id=thread['id'],
                    url=url)

            # name and email are optional in contrib.comments but required
            # in DISQUS. If they are not set, dummy values will be used
            client.create_post(
                forum_api_key=forum_api_key,
                thread_id=thread['id'],
                message=comment.comment.encode('utf-8'),
                author_name=comment.userinfo.get('name',
                                                 'nobody').encode('utf-8'),
                author_email=comment.userinfo.get('email',
                                                  '*****@*****.**'),
                author_url=comment.userinfo.get('url', ''),
                created_at=comment.submit_date.strftime('%Y-%m-%dT%H:%M'))
            if state_file is not None:
                self._save_state(state_file, comment.pk)
Exemplo n.º 11
0
    def handle(self, **options):        
       	if Site._meta.installed: 
       	    current_site = Site.objects.get_current()
       	else: 
       	    current_site = RequestSite(request)
        
        verbosity = int(options.get('verbosity'))
        dry_run = int(options.get('dry_run'))            
        
        comments = self._get_comments_to_export()
        comments_count = comments.count()
        print "Exporting %d comment(s)" % comments_count
        if dry_run:
            print comments
            return
        
        if comments_count > 0:            
            # Get a list of all forums for an API key. Each API key can have 
            # multiple forums associated. This application only supports the one 
            # specified under DISQUS_WEBSITE_SHORTNAME
            forum_list = call('get_forum_list', 
                              {'user_api_key': settings.DISQUS_API_KEY})
            try:
                forum = [f for f in forum_list if f['shortname'] == settings.DISQUS_WEBSITE_SHORTNAME][0]
            except IndexError:
                raise CommandError("Could not find forum. " + 
                                   "Check your 'DISQUS_WEBSITE_SHORTNAME' setting.")
            
            # Get the API key for the forum. Each forum has an unique API key
            forum_api_key = call('get_forum_api_key', 
                                 {'user_api_key': settings.DISQUS_API_KEY, 
                                  'forum_id': forum['id']})
            
            for comment in comments:
                if verbosity >= 1:
                    print "Exporting comment '%s'" % comment
                
                # Construct the url under which the comment should appear
                content_obj = comment.content_object
                content_obj_url = 'http://%s%s' % (current_site.domain, 
                                                   content_obj.get_absolute_url())
                
                # Try to get the thread by the url, if it doesn't exist we
                # create a new thread with this url.
                thread = call('get_thread_by_url', {'forum_api_key': forum_api_key, 
                                                    'url': content_obj_url})
                if not thread:
                    # create a new thread
                    thread = call('thread_by_identifier', {
                        'forum_api_key': forum_api_key,
                        'identifier': content_obj,
                        'title': str(content_obj)}, True)['thread']
                    
                    # set the url of the thread
                    call('update_thread', {'forum_api_key': forum_api_key,
                                           'thread_id': thread['id'],
                                           'url': content_obj_url}, True)
                

                # name and email are optional in contrib.comments but required
                # in DISQUS. If they are not set there will be dummy values 
                # provided
                post_data = {'forum_api_key': forum_api_key,
                             'thread_id': thread['id'],
                             'message': comment.comment.encode("utf-8"),
                             'author_name': comment.user_name.encode("utf-8") or 'nobody',
                             'author_email': comment.user_email or '*****@*****.**',
                             'author_url': comment.user_url,
                             'created_at':  comment.submit_date.strftime("%Y-%m-%dT%H:%M")}
                call('create_post', post_data, True)
Exemplo n.º 12
0
def theme_view(request,template_name="kinger/revision/axis_theme_view.html"):
    """ 主题详情页"""
    tile_id = request.GET.get('tid')
    ty = request.GET.get('ty')
    if ty == 'theme':
        themes = Tile.objects.filter(new_category_id__gt=2400,new_category_id__lte=2412)[:1]
        tile_id = themes[0].id if themes.count() else 0
    cid = request.GET.get('cid')
    if tile_id:
        tile = get_object_or_404(Tile, pk=tile_id)
    else:
        try:
            tiles_all = Tile.objects.filter(new_category__id__startswith='24').exclude(new_category__id=2400)
            if cid:
                tiles_all = tiles_all.filter(new_category__id=cid)
            tile = tiles_all[0]
        except:
            data = {'status':False,'msg':"暂无主题内容"}
            return HttpResponse(json.dumps(data))
    user = request.user
    tile.view_count += 1
    tile.save()
    helpers.add_daily_record_visitor(user,tile)
    set_user_access(user)
    
    channel = request.GET.get("channel")
    type = request.GET.get("ty","")
    month = request.GET.get("month","")
    tpage = int(request.GET.get("tpage",1))
    
    tiles = Tile.objects.filter(new_category_id__gt=2400,new_category_id__lte=2412)
    month_tiles = tiles.filter(new_category=tile.new_category)[0:3]
    month_tiles_pks = [m.id for m in month_tiles]
    year_tiles = tiles.order_by('-n_comments')[0:4]
    

    today = tile.start_time
    today_tiles = Tile.objects.get_tiles_date(date=today, tiles=tiles).order_by("-microsecond")
    
    try:
        next_day = Tile.objects.get_tiles_date_grater(date=today, tiles=tiles.filter(microsecond__gt=tile.microsecond,id__in=month_tiles_pks)).exclude(id=tile.id).order_by("microsecond")
        next_day = next_day[0]   
    except:
        next_day = None
    try:
        yesterday = Tile.objects.get_tiles_date_less(date=today, tiles=tiles.filter(microsecond__lt=tile.microsecond,id__in=month_tiles_pks)).exclude(id=tile.id).order_by("-microsecond")
        yesterday = yesterday[0]       
    except:
        yesterday = None
    
    is_last_page = True 
    if tile.n_comments > 0:
        comments = Comment.objects.for_model(tile).select_related('user')\
            .order_by("-submit_date").filter(is_public=True).filter(is_removed=False)
        if comments.count() > 5:
            comments = comments[0:5]
            is_last_page = False 
    else:
        comments = None
        
    ctx = {}
    emo_config = helpers.emo_config()
    tile_all = [t for t in tiles]
    tile_order = tile_all.index(tile)
#    if tpage == (tile_order / 10) + 1:
#        order = (tile_order + 1) % 10
#    else:
#        order = None
    if tpage == 1:
        tpage = (tile_order / 10) + 1
    start = (tpage - 1) * 10
    end = tpage * 10
    is_last_tpage = True if end >= len(tile_all) else False
    tiles = tile_all[start:end]
    
    is_ajax = True if request.method == 'POST' else False
    ctx.update({"tile": tile, "cur_tile": tile, "today_tiles": today_tiles, "ty":type, "comments": comments,"year_tiles":year_tiles,\
        "yesterday": yesterday, "next_day": next_day,"month": month,"channel": channel,"tpage":tpage,'month_tiles':month_tiles,\
         "tiles": tiles,"emo_config":emo_config,'is_last_tpage':is_last_tpage,"is_last_page":is_last_page,'is_ajax':is_ajax})
    if is_ajax:
        data = render(request, template_name,ctx)
        con=data.content
        return ajax_ok('成功',con)
    else:
        template_name = "kinger/revision/axis_theme_view_page.html"
        return render(request, template_name,ctx)
Exemplo n.º 13
0
def tile_view(request, tile_id, template_name="kinger/revision/axis_tile_view.html"):
    """ 瓦片详情页, 会显示与该瓦片相关的今日记录, 根据分类过滤"""
    user = request.user
    if not user.is_authenticated():
        data = json.dumps({'status':"error"})
        return HttpResponse(data)
    tile = get_object_or_404(Tile, pk=tile_id)
    tile.view_count += 1
    tile.save()
    helpers.add_daily_record_visitor(user,tile)
    set_user_access(user)
    
    channel = request.GET.get("channel")
    type = request.GET.get("ty","")
    month = request.GET.get("month","")
    tpage = int(request.GET.get("tpage",1))

    if channel == "edu":
        tiles = Tile.objects.get_tiles_edu(user)
    elif channel == "life":
        tiles = Tile.objects.get_tiles_life(user)
    else:
        tiles = Tile.objects.get_tiles_baby(user).exclude(new_category_id=9)      
        # 禁止访问其它用户的记录
        if tile.creator != user:
            try:
                tiles.get(pk=tile_id)
            except ObjectDoesNotExist:
                if not tile.is_public:
                    return render(request, "403.html")
    
    tiles = tiles.exclude(new_category_id__in=range(1130,1138)).order_by("-microsecond")
    recommends = Tile.objects.get_tiles_edu(user).order_by("-microsecond")[0:100]
    recommend_tiles = [recommends[i] for i in random.sample(range(100),4)]
    today = tile.start_time
    today_tiles = Tile.objects.get_tiles_date(date=today, tiles=tiles)
    daily_category = get_daily_category()
    if daily_category:
        today_tiles = today_tiles.exclude(new_category__parent=daily_category)
    today_tiles = today_tiles.order_by("-microsecond")
    
    try:
        next_day = Tile.objects.get_tiles_date_grater(date=today, tiles=tiles.filter(microsecond__gt=tile.microsecond)).exclude(id=tile.id).order_by("microsecond")
        next_day = next_day.exclude(new_category__parent=daily_category)[0] if daily_category else next_day[0]   
    except:
        next_day = None
    try:
        yesterday = Tile.objects.get_tiles_date_less(date=today, tiles=tiles.filter(microsecond__lt=tile.microsecond)).exclude(id=tile.id).order_by("-microsecond")
        yesterday = yesterday.exclude(new_category__parent=daily_category)[0] if daily_category else yesterday[0]       
    except:
        yesterday = None
     
    is_last_page = True 
    if tile.n_comments > 0:
        comments = Comment.objects.for_model(tile).select_related('user')\
            .order_by("-submit_date").filter(is_public=True).filter(is_removed=False)
        if comments.count() > 5:
            comments = comments[0:5]
            is_last_page = False 
    else:
        comments = None
        
    ctx = {}

    emo_config = helpers.emo_config()
    tile_all = [t for t in tiles]
    try:
        tile_order = tile_all.index(tile)
    except:
        tile_order = 0
    print tile_order,'ttttttttttttttttttt'
#    if tpage == (tile_order / 10) + 1:
#        order = (tile_order + 1) % 10
#    else:
    order = (tile_order) % 10
    print order,'oooooooooooooooooooooooooo'
    if tpage == 1:
        tpage = (tile_order / 10) + 1
    start = (tpage - 1) * 10
    end = tpage * 10
    is_last_tpage = True if end >= len(tile_all) else False
    tiles = tile_all[start:end]
    
    is_ajax = True if request.method == 'POST' else False
    ctx.update({"tile": tile, "cur_tile": tile, "today_tiles": today_tiles, "ty":type, "comments": comments,\
        "yesterday": yesterday, "next_day": next_day,"month": month,"channel": channel,"tpage":tpage,\
         "tiles": tiles,"emo_config":emo_config,"order":order,'is_last_tpage':is_last_tpage,\
         "is_last_page":is_last_page,"recommend_tiles":recommend_tiles,'is_ajax':is_ajax})

    if is_ajax:
        data = render(request, template_name,ctx)
        con=data.content
        return ajax_ok('成功',con)
    else:
        template_name = "kinger/revision/tile_view.html"
        return render(request, template_name,ctx)