Ejemplo n.º 1
0
def api_topic(request, board, pid, start=None):
    lily = Lily()
    try:
        topic = lily.fetch_topic(board, pid, start)
    except Exception as e:
        return build_response(exc=e)
    return build_response(topic.json())
Ejemplo n.º 2
0
def get_active_connection(username):
    try:
        user = User.objects.get(username=username)
    except User.DoesNotExist:
        return None
    profile = user.get_profile()

    lily = Lily(profile.last_session)
    if mc.get('act.{0}'.format(user.id)) or lily.is_logged_in():
        logger.debug('{0}: last_session valid. Success'.format(user))
        mc.set('act.{0}'.format(user.id), 1, time=60)
        return lily
    if profile.lily_password is not None:
        lily = Lily()
        session = lily.login(user.username, profile.lily_password)
        if session:
            profile.last_session = session.dumps()
            profile.save()
            logger.debug('{0}: saved password valid. Success'.format(user))
            mc.set('act.{0}'.format(user.id), 1, time=60)
            return lily
        else:
            profile.lily_password = None
            profile.save()
            logger.debug('{0}: password changed. Failure'.format(user))
            return None
    else:
        profile.last_session = None
        profile.save()
        logger.debug('{0}: no saved password. Failure'.format(user))
        return None
Ejemplo n.º 3
0
def api_board(request, board, start=None):
    lily = Lily()
    try:
        page = lily.fetch_page(board, start)
    except Exception as e:
        return build_response(exc=e)
    return build_response(page.json())
Ejemplo n.º 4
0
def api_board(request, board, start=None):
    lily = Lily()
    try:
        page = lily.fetch_page(board, start)
    except Exception as e:
        return build_response(exc=e)
    return build_response(page.json())
Ejemplo n.º 5
0
def api_topic(request, board, pid, start=None):
    lily = Lily()
    try:
        topic = lily.fetch_topic(board, pid, start)
    except Exception as e:
        return build_response(exc=e)
    return build_response(topic.json())
Ejemplo n.º 6
0
 def authenticate(self, username=None, password=None, save_password=False):
     logger.debug('Authenticating {0}'.format(username))
     lily = Lily()
     username = username.lower()
     session = lily.login(username, password)
     if session:
         try:
             user = User.objects.get(username=username)
             user.set_password(password)
             user.save()
         except User.DoesNotExist:
             user = User(username=username, password=password)
             user.save()
             logger.info('Created new {0}'.format(user))
         profile, created = LilyProfile.objects.get_or_create(
             user=user, defaults={'display_name': session.uid})
         if save_password:
             profile.lily_password = password
         else:
             profile.lily_password = None
         profile.last_session = session.dumps()
         profile.save()
         return user
     else:
         return None
Ejemplo n.º 7
0
def compose(request, board):
    is_reply = False
    post = None
    pid = None
    num = None
    if request.method == 'POST':
        # it's unlikely the session is expired
        lily = get_active_connection(request.user.username)
        form = ComposeForm(data=request.POST)
        if form.is_valid():
            title = form.cleaned_data['title']
            body = form.cleaned_data['body']
            pid = form.cleaned_data.get('pid', None)
            gid = form.cleaned_data.get('gid', None)
            ret = lily.compose(board, title, body, pid, gid)
            if ret:
                if pid is not None:
                    return HttpResponseRedirect('/topic/{0}/{1}'.format(
                        board, pid))
                else:
                    return HttpResponseRedirect('/board/{0}'.format(board))
            else:
                # XXX
                form._errors['__all__'] = u'发送失败'
    else:
        params = request.GET
        pid = params.get('pid', None)
        num = params.get('num', None)
        form = ComposeForm()
    if pid is not None:
        is_reply = True
        lily = Lily()
        post = lily.fetch_post(board, pid, num)
        form.fields['title'].widget = forms.HiddenInput()
        form.fields['title'].widget.attrs['value'] = 'Re: {0}'.format(
            post.title.encode('utf-8'))
        form.fields['pid'].widget.attrs['value'] = post.pid
        form.fields['gid'].widget.attrs['value'] = post.gid

    return render_to_response('compose.html',
                              dict(form=form,
                                   board=board,
                                   is_reply=is_reply,
                                   post=post),
                              context_instance=RequestContext(request))
Ejemplo n.º 8
0
def home(request):
    if request.user.is_authenticated():
        c = mc.get('fav.{0}'.format(request.user.id))
        lily = Lily(request.user.get_profile().last_session)
        if c:
            fav = [(i, lily.board_manager.board_text(i)) for i in c.split('|')]
        else:
            try:
                fav = lily.fetch_favorites()
                mc.set('fav.{0}'.format(request.user.id), '|'.join(fav), time=900)
                fav = [(i, lily.board_manager.board_text(i)) for i in fav]
            except Error:
                fav = None
    else:
        fav = None
    board_manager = BoardManager()
    return render_to_response('home.html',
            dict(favorites=fav, board_manager=board_manager),
            context_instance=RequestContext(request))
Ejemplo n.º 9
0
def home(request):
    if request.user.is_authenticated():
        c = mc.get('fav.{0}'.format(request.user.id))
        lily = Lily(request.user.get_profile().last_session)
        if c:
            fav = [(i, lily.board_manager.board_text(i)) for i in c.split('|')]
        else:
            try:
                fav = lily.fetch_favorites()
                mc.set('fav.{0}'.format(request.user.id),
                       '|'.join(fav),
                       time=900)
                fav = [(i, lily.board_manager.board_text(i)) for i in fav]
            except Error:
                fav = None
    else:
        fav = None
    board_manager = BoardManager()
    return render_to_response('home.html',
                              dict(favorites=fav, board_manager=board_manager),
                              context_instance=RequestContext(request))
Ejemplo n.º 10
0
def compose(request, board):
    is_reply = False
    post = None
    pid = None
    num = None
    if request.method == 'POST':
        # it's unlikely the session is expired
        lily = get_active_connection(request.user.username)
        form = ComposeForm(data=request.POST)
        if form.is_valid():
            title = form.cleaned_data['title']
            body = form.cleaned_data['body']
            pid = form.cleaned_data.get('pid', None)
            gid = form.cleaned_data.get('gid', None)
            ret = lily.compose(board, title, body, pid, gid)
            if ret:
                if pid is not None:
                    return HttpResponseRedirect('/topic/{0}/{1}'.format(board, pid))
                else:
                    return HttpResponseRedirect('/board/{0}'.format(board))
            else:
                # XXX
                form._errors['__all__'] = u'发送失败'
    else:
        params = request.GET
        pid = params.get('pid', None)
        num = params.get('num', None)
        form = ComposeForm()
    if pid is not None:
        is_reply = True
        lily = Lily()
        post = lily.fetch_post(board, pid, num)
        form.fields['title'].widget = forms.HiddenInput()
        form.fields['title'].widget.attrs['value'] = 'Re: {0}'.format(post.title.encode('utf-8'))
        form.fields['pid'].widget.attrs['value'] = post.pid
        form.fields['gid'].widget.attrs['value'] = post.gid

    return render_to_response('compose.html',
            dict(form=form, board=board, is_reply=is_reply, post=post),
            context_instance=RequestContext(request))
Ejemplo n.º 11
0
def get_active_connection(username):
    try:
        user = User.objects.get(username=username)
    except User.DoesNotExist:
        return None
    profile = user.get_profile()

    lily = Lily(profile.last_session)
    if mc.get('act.{0}'.format(user.id)) or lily.is_logged_in():
        logger.debug('{0}: last_session valid. Success'.format(user))
        mc.set('act.{0}'.format(user.id), 1, time=60)
        return lily
    if profile.lily_password is not None:
        lily = Lily()
        session = lily.login(user.username, profile.lily_password)
        if session:
            profile.last_session = session.dumps()
            profile.save()
            logger.debug('{0}: saved password valid. Success'.format(user))
            mc.set('act.{0}'.format(user.id), 1, time=60)
            return lily
        else:
            profile.lily_password = None
            profile.save()
            logger.debug('{0}: password changed. Failure'.format(user))
            return None
    else:
        profile.last_session = None
        profile.save()
        logger.debug('{0}: no saved password. Failure'.format(user))
        return None
Ejemplo n.º 12
0
 def authenticate(self, username=None, password=None, save_password=False):
     logger.debug('Authenticating {0}'.format(username))
     lily = Lily()
     username = username.lower()
     session = lily.login(username, password)
     if session:
         try:
             user = User.objects.get(username=username)
             user.set_password(password)
             user.save()
         except User.DoesNotExist:
             user = User(username=username, password=password)
             user.save()
             logger.info('Created new {0}'.format(user))
         profile, created = LilyProfile.objects.get_or_create(user=user, defaults={'display_name': session.uid})
         if save_password:
             profile.lily_password = password
         else:
             profile.lily_password = None
         profile.last_session = session.dumps()
         profile.save()
         return user
     else:
         return None
Ejemplo n.º 13
0
def top10(request):
    lily = Lily()
    page = lily.fetch_top10()
    return render_to_response('top10.html', dict(page=page))
Ejemplo n.º 14
0
def top10(request):
    lily = Lily()
    page = lily.fetch_top10()
    return render_to_response('top10.html', dict(page=page))
Ejemplo n.º 15
0
def hot(request):
    lily = Lily()
    page = lily.fetch_hot()
    return render_to_response('hot.html', dict(page=page, board_manager=lily.board_manager))
Ejemplo n.º 16
0
def hot(request):
    lily = Lily()
    page = lily.fetch_hot()
    return render_to_response(
        'hot.html', dict(page=page, board_manager=lily.board_manager))
Ejemplo n.º 17
0
def section(request, sid):
    lily = Lily()
    sec = lily.board_manager[int(sid)]
    return render_to_response(
        'section.html',
        dict(section_text=unicode(sec), board_list=sec.board_list))