Example #1
0
def register(request):
    if request.user.is_authenticated(): # Check if the user is logged in
        messages.add_message(request, messages.INFO, "You're currently logged in to Comet. If you want to register another account please <a href='/logout'>logout</a> first.")
        return redirect("frontpage") # User is logged in, return them to index

    if request.POST: # Some data was posted
        # Create a form instance and populate it with the Post data
        form = RegistrationForm(request.POST)

        # Check whether the form is valid.
        if form.is_valid():
            # Form data is valid, send a verification email.
            data = form.cleaned_data

            user_url = generate()
            while User.objects.filter(user_url=user_url).exists():
                user_url = generate()

            # You need to call user.objects.create_user rather than accessing
            # the user manager directly.
            User.objects.create_user(
                email=data["email"],
                username=data["username"],
                password=data["password"],
                user_url=user_url,
            )

            # Authenticate
            user = authenticate(
                username=data["email"],
                password=data["password"],
            )
            login_user(request, user) # Log the user in.

            if "next" in request.GET:
                return redirect(request.GET["next"])
            return redirect("messages")

        # Form data was invalid, render the page with error messages
        next_dir = ""
        if "next" in request.GET:
            next_dir=request.GET["next"]
        return renderRegister(request, next_dir=next_dir, form=form)
    else: # No data was posted, render a regular page
        next_dir = ""
        if "next" in request.GET:
            next_dir=request.GET["next"]
        return renderRegister(request, next_dir=next_dir)
Example #2
0
def create(request):
    """
    Creates a new public or private group channel.

    TODO Redo without form
    """
    if not request.POST:
        messages.add_message(request, messages.ERROR, "A group could not be created because no data was posted.")
        return redirect("messages")

    form = CreateChatForm(request.POST)
    if not form.is_valid():
        messages.add_message(request, messages.ERROR, "The data you entered was invalid.")
        return redirect("messages")

    # Generate a new identifier
    channel_url = generate()
    while Channel.objects.filter(channel_url=channel_url).exists():
        channel_url = generate()

    data = form.cleaned_data

    # Create a new group
    group = Channel.objects.create(
        name=data["name"],
        is_public=data["is_public"],
        channel_url=channel_url,
        is_group=True,
    )

    # Add user permissions
    user_perms = ChannelPermissions.objects.create(
        channel=group,
        user=request.user,
        is_creator=True,
    )

    if data["is_public"]:
        messages.add_message(request, messages.INFO, "Group '%s' successfully created. Add people to this group by giving them the URL or by clicking 'add users'." % data["name"])
    else:
        messages.add_message(request, messages.INFO, "Group '%s' successfully created. Add people to this group by clicking 'add users'." % data["name"])
    return redirect(group)