def signup(request):
    if request.user:
        user = request.user
    else:
        user = None
    if request.method == 'POST':
        userForm = SignUpForm(request.POST)
        profileForm = SignUpProfileForm(request.POST)
        if not userForm.is_valid() or not profileForm.is_valid():
            return render(request, 'auth_new/signup.html', {'userForm': userForm, 'profileForm':profileForm})
        else:
            username = userForm.cleaned_data.get('username')
            email = userForm.cleaned_data.get('username')
            password = userForm.cleaned_data.get('password')
            realname = profileForm.cleaned_data.get('realname')
            major = profileForm.cleaned_data.get('major')
            enter_year = profileForm.cleaned_data.get('enter_year')
            User.objects.create_user(username=username, password=password, email=email)
            user = authenticate(username=username, password=password)

            user.profile.realname = realname
            user.profile.major = major
            user.profile.enter_year = enter_year


            user.save()

            login(request, user)
            welcome_post = u'{0} has joined the network.'.format(user.username, user.username)
            feed = Feed(user=user, post=welcome_post)
            feed.save()
            return redirect('/')
    else:
        return render(request, 'auth_new/signup.html', {'userForm': SignUpForm(), 'profileForm': SignUpProfileForm(), 'user': user})
def post(request):
    last_feed = request.POST.get('last_feed')
    user = request.user
    csrf_token = unicode(csrf(request)['csrf_token'])
    feed = Feed()
    feed.user = user
    post = request.POST['post']
    post = post.strip()
    if len(post) > 0:
        feed.post = post[:255]
        feed.save()
    html = _html_feeds(last_feed, user, csrf_token)
    return HttpResponse(html)
def load(request):
    from_feed = request.GET.get('from_feed')
    page = request.GET.get('page')
    feed_source = request.GET.get('feed_source')
    all_feeds = Feed.get_feeds(from_feed)
    if feed_source != 'all':
        all_feeds = all_feeds.filter(user__id=feed_source)
    paginator = Paginator(all_feeds, FEEDS_NUM_PAGES)
    try:
        feeds = paginator.page(page)
    except PageNotAnInteger:
        return HttpResponseBadRequest()
    except EmptyPage:
        feeds = []
    html = u''
    csrf_token = unicode(csrf(request)['csrf_token'])
    for feed in feeds:
        html = u'{0}{1}'.format(
            html,
            render_to_string('feeds/partial_feed.html', {
                'feed': feed,
                'user': request.user,
                'csrf_token': csrf_token
            }))
    return HttpResponse(html)
def check(request):
    last_feed = request.GET.get('last_feed')
    feed_source = request.GET.get('feed_source')
    feeds = Feed.get_feeds_after(last_feed)
    if feed_source != 'all':
        feeds = feeds.filter(user__id=feed_source)
    count = feeds.count()
    return HttpResponse(count)
def check(request):
    last_feed = request.GET.get('last_feed')
    feed_source = request.GET.get('feed_source')
    feeds = Feed.get_feeds_after(last_feed)
    if feed_source != 'all':
        feeds = feeds.filter(user__id=feed_source)
    count = feeds.count()
    return HttpResponse(count)
Exemple #6
0
def signup(request):
    if request.user:
        user = request.user
    else:
        user = None
    if request.method == 'POST':
        userForm = SignUpForm(request.POST)
        profileForm = SignUpProfileForm(request.POST)
        if not userForm.is_valid() or not profileForm.is_valid():
            return render(request, 'auth_new/signup.html', {
                'userForm': userForm,
                'profileForm': profileForm
            })
        else:
            username = userForm.cleaned_data.get('username')
            email = userForm.cleaned_data.get('username')
            password = userForm.cleaned_data.get('password')
            realname = profileForm.cleaned_data.get('realname')
            major = profileForm.cleaned_data.get('major')
            enter_year = profileForm.cleaned_data.get('enter_year')
            User.objects.create_user(username=username,
                                     password=password,
                                     email=email)
            user = authenticate(username=username, password=password)

            user.profile.realname = realname
            user.profile.major = major
            user.profile.enter_year = enter_year

            user.save()

            login(request, user)
            welcome_post = u'{0} has joined the network.'.format(
                user.username, user.username)
            feed = Feed(user=user, post=welcome_post)
            feed.save()
            return redirect('/')
    else:
        return render(
            request, 'auth_new/signup.html', {
                'userForm': SignUpForm(),
                'profileForm': SignUpProfileForm(),
                'user': user
            })
def update(request):
    first_feed = request.GET.get('first_feed')
    last_feed = request.GET.get('last_feed')
    feed_source = request.GET.get('feed_source')
    feeds = Feed.get_feeds().filter(id__range=(last_feed, first_feed))
    if feed_source != 'all':
        feeds = feeds.filter(user__id=feed_source)
    dump = {}
    for feed in feeds:
        dump[feed.pk] = {'likes': feed.likes, 'comments': feed.comments}
    data = json.dumps(dump)
    return HttpResponse(data, content_type='application/json')
def update(request):
    first_feed = request.GET.get('first_feed')
    last_feed = request.GET.get('last_feed')
    feed_source = request.GET.get('feed_source')
    feeds = Feed.get_feeds().filter(id__range=(last_feed, first_feed))
    if feed_source != 'all':
        feeds = feeds.filter(user__id=feed_source)
    dump = {}
    for feed in feeds:
        dump[feed.pk] = {'likes': feed.likes, 'comments': feed.comments}
    data = json.dumps(dump)
    return HttpResponse(data, content_type='application/json')
def _html_feeds(last_feed, user, csrf_token, feed_source='all'):
    feeds = Feed.get_feeds_after(last_feed)
    if feed_source != 'all':
        feeds = feeds.filter(user__id=feed_source)
    html = u''
    for feed in feeds:
        html = u'{0}{1}'.format(html, render_to_string('feeds/partial_feed.html', {
            'feed': feed,
            'user': user,
            'csrf_token': csrf_token
            })
        )
    return html
def _html_feeds(last_feed, user, csrf_token, feed_source='all'):
    feeds = Feed.get_feeds_after(last_feed)
    if feed_source != 'all':
        feeds = feeds.filter(user__id=feed_source)
    html = u''
    for feed in feeds:
        html = u'{0}{1}'.format(
            html,
            render_to_string('feeds/partial_feed.html', {
                'feed': feed,
                'user': user,
                'csrf_token': csrf_token
            }))
    return html
def feeds(request):
    if request.user:
        user = request.user
    else:
        user = None
    all_feeds = Feed.get_feeds()
    paginator = Paginator(all_feeds, FEEDS_NUM_PAGES)
    feeds = paginator.page(1)
    from_feed = -1
    if feeds:
        from_feed = feeds[0].id
    return render(request, 'feeds/square.html', {
        'feeds': feeds,
        'from_feed': from_feed,
        'page': 1,
        'user': user,
    })
def feeds(request):
    if request.user:
        user = request.user
    else:
        user = None
    all_feeds = Feed.get_feeds()
    paginator = Paginator(all_feeds, FEEDS_NUM_PAGES)
    feeds = paginator.page(1)
    from_feed = -1
    if feeds:
        from_feed = feeds[0].id
    return render(request, 'feeds/square.html', {
        'feeds': feeds,
        'from_feed': from_feed, 
        'page': 1,
        'user': user,
        })
def post(request):
    last_feed = request.POST.get('last_feed')
    user = request.user
    csrf_token = unicode(csrf(request)['csrf_token'])
    feed = Feed()
    feed.user = user
    post = request.POST['post']
    post = post.strip()
    if len(post) > 0:
        feed.post = post[:255]
        feed.save()
    html = _html_feeds(last_feed, user, csrf_token)
    return HttpResponse(html)
def load(request):
    from_feed = request.GET.get('from_feed')
    page = request.GET.get('page')
    feed_source = request.GET.get('feed_source')
    all_feeds = Feed.get_feeds(from_feed)
    if feed_source != 'all':
        all_feeds = all_feeds.filter(user__id=feed_source)
    paginator = Paginator(all_feeds, FEEDS_NUM_PAGES)
    try:
        feeds = paginator.page(page)
    except PageNotAnInteger:
        return HttpResponseBadRequest()
    except EmptyPage:
        feeds = []
    html = u''
    csrf_token = unicode(csrf(request)['csrf_token'])
    for feed in feeds:
        html = u'{0}{1}'.format(html, render_to_string('feeds/partial_feed.html', {
            'feed': feed,
            'user': request.user,
            'csrf_token': csrf_token
            })
        )
    return HttpResponse(html)