Esempio n. 1
0
def create_group(request):
    """create a new user group"""
    mm = MessageManager(request)
    if request.user.is_authenticated() and request.user.__class__ is cUser:
        if request.POST:
            form = CreateGroupForm(request.POST)
            if form.is_valid():
                data = request.POST.copy()
                # make sure the group name is unique
                try:
                    cGroup.objects.get(name=data["title"])
                    mm.set_error("that group name is already taken")
                    return render(request,'accounts/groups/create.html', mm.messages(), {'form':form})
                except cGroup.DoesNotExist:
                    # create group
                    g = cGroup()
                    g.name = data["title"]
                    g.description = data["description"]
                    g.type = data["type"]
                    g.visibility = data["visibility"]
                    g.open_registration = data.has_key("open_registration")
                    g.save()
                    
                    # add user to group
                    request.user.groups.add(g)
                    g.managers.add(request.user)
                    
                    # Build the invitation key                                                                                                                    
                    salt = sha.new(str(random.random())).hexdigest()[:5]
                    g.invitation_key = sha.new(salt+g.name).hexdigest()[:13]
                    g.key_expires = datetime.datetime.today() + datetime.timedelta(2)
                    g.save()
                    manage_url = reverse('manage-group', None, (), {'gid':g.pk})
                    invite_url = "%s%s/%s" % (SITE_URL, reverse('join-group', None, (), {'gid':g.pk}), g.invitation_key)
                    mm.set_success("Successfully Created Group \"%s\"!<p> We automatically generated an invitation url that you can share with your friends. For more information check out the <a href=\"%s\">Administration Page</a>.</p><p>Invitation Url: <a href=\"%s\">%s</a></p>" % (g.name, manage_url, invite_url, invite_url))
                    # TODO: take to new group page
                    return redirect(g.get_absolute_url())
            else:
                return render(request,'accounts/groups/create.html', mm.messages(), {'form':form})
        else:
            # prepare new form for user
            form = CreateGroupForm(initial={'open_registration':True})
            return render(request,'accounts/groups/create.html', mm.messages(), {'form':form})
    else:
        mm.set_error("you are not allowed to create a group")
        return redirect(request.META.get('HTTP_REFERER','/'))
Esempio n. 2
0
def edit_group(request, gid):
    """edit a group"""
    mm = MessageManager(request)
    g = get_object_or_404(cGroup, pk=gid)
    if request.user.is_authenticated() and request.user.is_group_manager(g):
        if request.POST:
            form = CreateGroupForm(request.POST)
            if form.is_valid():
                data = request.POST.copy()
                # make sure the group name is unique
                #try:
                #    cGroup.objects.get(name=data["title"])
                #    mm.set_error("that group name is already taken")
                #    return render(request,'accounts/groups/create.html', mm.messages(), {'form':form})
                #except cGroup.DoesNotExist:
                    # create group
                #g = cGroup()
                g.name = data["title"]
                g.description = data["description"]
                g.type = data["type"]
                g.visibility = data["visibility"]
                g.open_registration = data.has_key("open_registration")
                g.save()
                
                # add user to group
                #request.user.groups.add(g)
                
                #g.managers.add(request.user)   
                #g.save()
                
                mm.set_success("edits were successfull")
                # TODO: take to new group page
                return redirect(g.get_absolute_url())
            else:
                return render(request,'accounts/groups/edit.html', mm.messages(), {'form':form, 'group':g})
        else:
            # prepare new form for user
            form = CreateGroupForm(initial={'title': g.name, 'description':g.description, 'type':g.type, 'open_registration':g.open_registration})
            return render(request,'accounts/groups/edit.html', mm.messages(), {'form':form, 'group':g})
    else:
        mm.set_error("you are not allowed to edit this group")
        return redirect(g.get_absolute-url())