Esempio n. 1
0
def work(request):
    user = request.user
    action = request.REQUEST.get('action', None)
    weibo = SinaWeibo.get_by_user(user)
    if (action == 'search'):
        keyword = request.REQUEST.get("q")
        collect_candidate(user, weibo.get_auth(), keyword)
    elif (action == "followall"):
        follow_all(user, weibo.get_auth())
    elif (action == "clearall"):
        clear_all(user)

    KeywordFormSet = modelformset_factory(Keyword)
    keyword_formset = KeywordFormSet()

    managed_count = Candidate.get_by_user(user).filter(
        follow_date__isnull=False).count()
    candidate_count = Candidate.get_by_user(user).filter(
        follow_date__isnull=True).count()

    return render_to_response(
        "work.html", {
            "managed_count": managed_count,
            "candidate_count": candidate_count,
            "keyword_formset": keyword_formset
        })
Esempio n. 2
0
def daily_follow(request):
    user = request.user
    weibo = SinaWeibo.get_by_user(user)
    try:
        weibo.daily_follow()
    except Candidate.OutOfQuota:
        return make_json_response(request, code=419001)
    return make_json_response(request)
Esempio n. 3
0
def mock(request):
    from django.contrib.auth import authenticate, login as django_login
    userid = request.GET['userid']
    user = User.objects.get(pk=userid)
    weibo = SinaWeibo.get_by_user(user)
    user = authenticate(sinaweiboid=weibo.weiboid)
    django_login(request, user)
    return HttpResponseRedirect('/')
Esempio n. 4
0
def oauth_callback(request):
    oauth_verifier = request.REQUEST.get('oauth_verifier', None)
    request_token = request.session.get('oauth_sina_request_token', None)
    auth = OAuthHandler(settings.SINA_CONSUMER_KEY,
                        settings.SINA_CONSUMER_SECRET)
    auth.request_token = request_token
    access_token = auth.get_access_token(oauth_verifier)
    logger.debug("authorized")
    request.session['oauth_sina'] = auth

    api = API(auth)
    data = api.verify_credentials()

    from django.contrib.auth import authenticate, login as django_login

    user = authenticate(sinaweiboid=data.id)
    if user is None:
        #  query = SinaWeibo.objects.filter(weiboid = data.id)
        #  if (len(query) ==  0):

        user = User()
        user.username = "******" + data.name
        user.backend = 'sinaweibo'
        user.save()

        sina_weibo = SinaWeibo()
        sina_weibo.weiboid = data.id
        sina_weibo.name = data.name
        sina_weibo.access_token = auth.access_token.key
        sina_weibo.access_secret = auth.access_token.secret
        sina_weibo.user = user
        sina_weibo.save()

        user = authenticate(sinaweiboid=data.id)
        assert user != None


#  else:
#    sina_weibo = query[0]
#    user = sina_weibo.user
#    user.backend = 'sinaweibo'

    django_login(request, user)

    return HttpResponseRedirect("/")
Esempio n. 5
0
def page_analysis(request):
    me = request.user
    weibo = SinaWeibo.get_by_user(me)
    d = weibo.get_follow_graph().items()
    d.sort()
    x_axis = [_d[0] for _d in d]
    followed = [_d[1][0] for _d in d]
    followed_back = [_d[1][1] for _d in d]
    follow_graph = {
        'x_axis': map(lambda d: u"%s月%s日" % (d.month, d.day), x_axis),
        'followed': followed,
        'followed_back': followed_back,
    }

    result = {}
    result['me'] = user_vo(me)
    result['follow_graph'] = follow_graph
    return make_json_response(request, result)
Esempio n. 6
0
def wholly_update_keyword(request, form):
    values = form.cleaned_data['values']
    assert type(values) == list
    logger.warn('%s(%s) wholly_update its keywords with %s' %
                (request.user.username, request.user.id, values))

    values = set(values)
    old_keywords = Keyword.objects.filter(
        sina=SinaWeibo.get_by_user(request.user))
    to_be_deleted = [kw for kw in old_keywords if kw.value not in values]
    logger.debug('keywords to be deleted: %s' % to_be_deleted)
    for kw in to_be_deleted:
        kw.delete()
    remained_values = [
        kw.value for kw in old_keywords if kw.value not in to_be_deleted
    ]
    logger.debug('keywords to be added: %s' % to_be_deleted)
    to_be_added = [v for v in values if v not in remained_values]
    for v in to_be_added:
        _add_keyword(request.user, v)
    return make_json_response(request)
Esempio n. 7
0
def _add_keyword(user, value):
    kw = Keyword()
    kw.value = value
    kw.sina = SinaWeibo.get_by_user(user)
    kw.enabled = True
    kw.save()
Esempio n. 8
0
def page_pool(request):
    date_format = "%Y-%m-%d"
    from home.models import daily_today

    me = request.user
    sina = SinaWeibo.get_by_user(me)
    keywords = [keyword_vo(k) for k in Keyword.objects.filter(sina=sina)]

    n = daily_today()
    strdate = request.REQUEST.get('date', n.strftime(date_format))
    date = datetime.datetime.strptime(strdate, date_format)
    if (n.year == date.year and n.month == date.month and n.day == date.day):
        add_new_candidate = True
    else:
        add_new_candidate = False

    candidates = Candidate.get_by_user_date(me, date)

    #    candidates = Candidate.get_by_user(me).order_by("-follow_date")
    field_lookups = [k for k in request.REQUEST.keys() if k.find('__') >= 0]
    if field_lookups:
        for k in field_lookups:
            candidates = candidates.filter(**dict(((k, request.REQUEST[k]), )))

#    dates = list(Candidate.get_by_user(me).dates('follow_date','day'))
#    dates.sort(reverse=True)
    candidate_page = {
        'date': date.strftime(date_format),
    }
    if add_new_candidate and not field_lookups:
        remained = sina.daily_remained_quota
        candidates = list(candidates)
        new_c = Candidate.get_user_new(me).order_by("-priority")[:remained]
        candidates = candidates + list(new_c)


#    perpage  = int(request.REQUEST.get('perpage','25'))
#    page  = int(request.REQUEST.get('page','1'))
#    candidate_paginator = Paginator(candidates, perpage)
#    try:
#        candidates_page = candidate_paginator.page(page)
#    except (InvalidPage, EmptyPage):
#        candidates_page = candidate_paginator.page(paginator.num_pages)

#daily candidates
    found = Candidate.objects.filter(user=me).filter(
        managed__isnull=True).count()
    daily_res = {}
    daily_res['quota_remained'] = sina.daily_remained_quota
    daily_res['quota'] = sina.daily_quota
    daily_res['followed'] = sina.daily_followed
    daily_res['idle_days'] = sina.idle_days
    daily_res['found'] = min(found, daily_res['quota_remained'])

    result = {}
    result['me'] = user_vo(me)
    result['keywords'] = keywords
    #   result['candidates'] = [candidate_vo(c) for c in candidates_page.object_list]
    #    result['candidate_page'] = page_vo(candidate_paginator, candidates_page)
    result['candidates'] = [candidate_vo(c) for c in candidates]
    result['candidate_page'] = candidate_page
    result['daily'] = daily_res

    return make_json_response(request, result)