Ejemplo n.º 1
0
def home(request):

    #if user does not have a user profile, create one
    profile = UserProfile.objects.filter(user=request.user)
    if not profile:
        profile = UserProfile(user=request.user, avatar=DEFAULT_AVATAR, about_me=" ",
                              birthdate=datetime.date.today(), hometown=" ", present_location=" ")
        if request.user.is_superuser or request.user.is_staff:
            profile.user_type = 3
        profile.save()

    boards = Board.objects.all()
    topic_form = TopicForm()
    board_form = BoardForm()
    profile = UserProfile.objects.get(user=request.user)

    if request.method == 'POST':
        #user clicks the 'Add Topic'
        if 'addtopic' in request.POST:
            topic_form = TopicForm(request.POST)
            if topic_form.is_valid():
                topic_form.save()
                return HttpResponseRedirect(REDIRECT_HOME)
        #user clicks the 'Add Board' button
        elif 'addboard' in request.POST:
            board_form = BoardForm(request.POST, request.FILES)
            if board_form.is_valid():
                board = board_form.save(commit=False)
                if Board.objects.all():
                    last_board = Board.objects.aggregate(last_rank=Max('rank'))
                    board.rank = last_board['last_rank']+1
                else:
                    board.rank = 1
                board.save()

                return HttpResponseRedirect(REDIRECT_HOME)

    #group boards according to their topics
    grouping = SortedDict()
    for entry in boards:
        grouping.setdefault(entry.topic.name, [])
        grouping[entry.topic.name].append(entry)

    context = {
        'boards': grouping,
        'profile': profile,
        'reorder': True,
        'topic_form': topic_form,
        'board_form': board_form,
    }
    return render(request, 'bulletinboard/home.html', context)
Ejemplo n.º 2
0
def new_user(request):
    if request.method == 'POST':
        try:
            username = request.POST['username']
            password = request.POST['password']
            email = request.POST['username']

            if len(username) == 0 or len(password) == 0 or len(request.POST['c_password']) == 0:
                return render(request, 'bulletinboard/newUser.html', {'message': 'All fields are required.'})
            if password != request.POST['c_password']:
                return render(request, 'bulletinboard/newUser.html', {'message': 'Passwords do not match.'})

            user = User.objects.create_user(username=username, email=email, password=password)
            user.save()

            #create an instance of the user profile for the newly created user
            profile = UserProfile(user=user, avatar=DEFAULT_AVATAR, about_me=" ", birthdate=datetime.date.today(), hometown=" ", present_location=" ")
            profile.save()
            return HttpResponseRedirect('/accounts/login/')
        except:
            #if there is an error, return to newUser.html
            return render(request, 'bulletinboard/newUser.html', {'message': 'Username is already taken.'})
    return render(request, 'bulletinboard/newUser.html', {})