Esempio n. 1
0
def project(request, slug, template_name="projects/project.html"):
    project = get_object_or_404(Project, slug=slug)
    
    if project.deleted:
        raise Http404
    
    photos = project.photos.all()
    
    if request.user.is_authenticated() and request.method == "POST" and request.user == project.creator:
        if request.POST["action"] == "update":
            user_file_form = UserFileForm(Account.objects.get(user = request.user))
            adduser_form = AddUserForm(project=project)
            project_form = ProjectUpdateForm(request.POST, instance=project)
            file_form = UploadFileForm()
            if project_form.is_valid():
                project = project_form.save()
        elif request.POST["action"] == "add":
            user_file_form = UserFileForm(Account.objects.get(user = request.user))
            project_form = ProjectUpdateForm(instance=project)
            adduser_form = AddUserForm(project, request.POST)
            file_form = UploadFileForm()
            if adduser_form.is_valid():
                adduser_form.save(project, request.user)
                adduser_form = AddUserForm(project=project) # @@@ is this the right way to clear it?
        elif request.POST["action"] == "upload":
            user_file_form = UserFileForm(Account.objects.get(user = request.user))
            adduser_form = AddUserForm(project=project)
            project_form = ProjectUpdateForm(instance=project)  
	    file_form = UploadFileForm(request.POST, request.FILES)
            if file_form.is_valid():
                uFile = handle_uploaded_file(request.FILES['file'], request.user)
                project.files.add(uFile)
                project.save()
                request.user.message_set.create(message="uploaded %(file)s"%{'file':request.FILES['file'].name})
        elif request.POST["action"] == "remove":
            user_file_form = UserFileForm(Account.objects.get(user = request.user))
            adduser_form = AddUserForm(project=project)
            project_form = ProjectUpdateForm(instance=project)  
	    file_form = UploadFileForm()
	    fname = request.POST['file']
	    remove_uploaded_file(fname, project)
            request.user.message_set.create(message="removed %(file)s from project"%{'file':fname})
        elif request.POST["action"] == "userfile":
            adduser_form = AddUserForm(project=project)
            project_form = ProjectUpdateForm(instance=project)  
	    file_form = UploadFileForm()
	    user_file_form = UserFileForm(Account.objects.get(user = request.user))
	    #if user_file_form.is_valid():
	    acc = Account.objects.get(user = request.user)
            uFile = acc.files.get(name = request.POST['files'])
            project.files.add(uFile)
            project.save()
            request.user.message_set.create(message="added %(file)s to project"%{'file':uFile.name})
                 
        else:
            user_file_form = UserFileForm(Account.objects.get(user = request.user))
            file_form = UploadFileForm()
            project_form = ProjectUpdateForm(instance=project)
            adduser_form = AddUserForm(project=project)
    elif request.user.is_authenticated() and request.user == project.creator:
        user_file_form = UserFileForm(Account.objects.get(user = request.user))
        file_form = UploadFileForm()
        adduser_form = AddUserForm(project=project)
        project_form = ProjectUpdateForm(instance=project)
    else:
        user_file_form = UserFileForm(None)
        file_form = UploadFileForm()
        adduser_form = AddUserForm(project=project)
        project_form = ProjectUpdateForm(instance=project)
    
    topics = project.topics.all()[:5]
    articles = Article.objects.filter(
        content_type=get_ct(project),
        object_id=project.id).order_by('-last_update')
    total_articles = articles.count()
    articles = articles[:5]
    
    total_tasks = project.tasks.count()
    tasks = project.tasks.order_by("-modified")[:10]

    # tweets = TweetInstance.objects.tweets_for(project).order_by("-sent")

    are_member = project.has_member(request.user)

    return render_to_response(template_name, {
        "project_form": project_form,
        "adduser_form": adduser_form,
        "project": project,
        "photos": photos,
        "topics": topics,
        "articles": articles,
        "total_tasks": total_tasks,
        "tasks": tasks,
        "total_articles": total_articles,
        "are_member": are_member,
        "file_form": file_form,
        "user_file_form":user_file_form,
    }, context_instance=RequestContext(request))
Esempio n. 2
0
def profile(request, username, template_name="profiles/profile.html"):
    other_user = get_object_or_404(User, username=username)
    if request.user.is_authenticated():
        is_friend = Friendship.objects.are_friends(request.user, other_user)
        is_following = Following.objects.is_following(request.user, other_user)
        other_friends = Friendship.objects.friends_for_user(other_user)
        if request.user == other_user:
            is_me = True
        else:
            is_me = False
    else:
        other_friends = []
        is_friend = False
        is_me = False
        is_following = False
    
    if request.user.is_authenticated() and request.method == "POST" and not is_me:
        
        # @@@ some of this should go in zwitschern itself
        
        if request.POST["action"] == "follow":
            Following.objects.follow(request.user, other_user)
            is_following = True
            request.user.message_set.create(message=_("You are now following %(other_user)s") % {'other_user': other_user})
            if notification:
                notification.send([other_user], "tweet_follow", {"user": request.user})
        elif request.POST["action"] == "unfollow":
            Following.objects.unfollow(request.user, other_user)
            is_following = False
            request.user.message_set.create(message=_("You have stopped following %(other_user)s") % {'other_user': other_user})
    
    if is_friend:
        invite_form = None
        previous_invitations_to = None
        previous_invitations_from = None
    else:
        if request.user.is_authenticated() and request.method == "POST":
            if request.POST["action"] == "delete":
                file_form = UploadFileForm()
                fname = request.POST['file']
                delete_uploaded_file(fname, request.user)
                invite_form = InviteFriendForm(request.user, {
                'to_user': username,
                'message': ugettext("Let's be friends!"),
                })
                request.user.message_set.create(message=_("You have deleted %(file)s") % {'file': fname})
            elif request.POST["action"] == "upload":
                invite_form = InviteFriendForm(request.user, {
                'to_user': username,
                'message': ugettext("Let's be friends!"),
                })
                file_form = UploadFileForm(request.POST, request.FILES)
                if file_form.is_valid():
                    uFile = handle_uploaded_file(request.FILES['file'], request.user)
                    myAccount = Account.objects.get(user = request.user)
                    myAccount.files.add(uFile)
                    myAccount.save()
                    request.user.message_set.create(message="uploaded %(file)s"%{'file':request.FILES['file'].name})
            elif request.POST["action"] == "invite":
                file_form = UploadFileForm()
                invite_form = InviteFriendForm(request.user, request.POST)
                if invite_form.is_valid():
                    invite_form.save()
            else:
                file_form = UploadFileForm()
                invite_form = InviteFriendForm(request.user, {
                    'to_user': username,
                    'message': ugettext("Let's be friends!"),
                })
                if request.POST["action"] == "accept": # @@@ perhaps the form should just post to friends and be redirected here
                    invitation_id = request.POST["invitation"]
                    try:
                        invitation = FriendshipInvitation.objects.get(id=invitation_id)
                        if invitation.to_user == request.user:
                            invitation.accept()
                            request.user.message_set.create(message=_("You have accepted the friendship request from %(from_user)s") % {'from_user': invitation.from_user})
                            is_friend = True
                            other_friends = Friendship.objects.friends_for_user(other_user)
                    except FriendshipInvitation.DoesNotExist:
                        pass
        else:
            file_form = UploadFileForm()
            invite_form = InviteFriendForm(request.user, {
                'to_user': username,
                'message': ugettext("Let's be friends!"),
            })
    previous_invitations_to = FriendshipInvitation.objects.filter(to_user=other_user, from_user=request.user)
    previous_invitations_from = FriendshipInvitation.objects.filter(to_user=request.user, from_user=other_user)
    
    if is_me:
        file_form = UploadFileForm()
        myAccount = Account.objects.get(user = request.user)
        file_list = myAccount.files.all()
        if request.method == "POST":
            if request.POST["action"] == "update":
                profile_form = ProfileForm(request.POST, instance=other_user.get_profile())
                if profile_form.is_valid():
                    profile = profile_form.save(commit=False)
                    profile.user = other_user
                    profile.save()
            else:
                profile_form = ProfileForm(instance=other_user.get_profile())
        else:
            profile_form = ProfileForm(instance=other_user.get_profile())
    else:
        file_form = None
        file_list = None
        profile_form = None

    return render_to_response(template_name, {
        "profile_form": profile_form,
        "is_me": is_me,
        "is_friend": is_friend,
        "is_following": is_following,
        "other_user": other_user,
        "other_friends": other_friends,
        "invite_form": invite_form,
        "previous_invitations_to": previous_invitations_to,
        "previous_invitations_from": previous_invitations_from,
        "file_list":file_list,
        "file_form":file_form
    }, context_instance=RequestContext(request))
Esempio n. 3
0
def task(request, id, template_name="projects/task.html"):
    task = get_object_or_404(Task, id=id)
    project = task.project
    
    if project.deleted:
        raise Http404
    
    is_member = project.has_member(request.user)
    
    if is_member and request.method == "POST":
        if request.POST["action"] == "assign":
            #dependency_form = TaskDependencyForm(project.tasks.all())
            user_file_form = UserFileForm(Account.objects.get(user = request.user))
            status_form = StatusForm(instance=task)
            assign_form = AssignForm(project, request.POST, instance=task)
	    file_form = UploadFileForm()
            if assign_form.is_valid():
                task = assign_form.save()
                request.user.message_set.create(message="assigned task to '%s'" % task.assignee)
                if notification:
                    notification.send(project.member_users.all(), "projects_task_assignment", {"user": request.user, "task": task, "project": project, "assignee": task.assignee})
        elif request.POST["action"] == "update_status":
            #dependency_form = TaskDependencyForm(project.tasks.all())
            user_file_form = UserFileForm(Account.objects.get(user = request.user))
            assign_form = AssignForm(project, instance=task)
            status_form = StatusForm(request.POST, instance=task)
	    file_form = UploadFileForm()
            if status_form.is_valid():
                task = status_form.save()
                request.user.message_set.create(message="updated your status on the task")
                if notification:
                    notification.send(project.member_users.all(), "projects_task_status", {"user": request.user, "task": task, "project": project})
	elif request.POST["action"] == "upload":
            #dependency_form = TaskDependencyForm(project.tasks.all())
            user_file_form = UserFileForm(Account.objects.get(user = request.user))
            assign_form = AssignForm(project, instance=task)
            status_form = StatusForm(instance=task)
	    file_form = UploadFileForm(request.POST, request.FILES)
            if file_form.is_valid():
                uFile = handle_uploaded_file(request.FILES['file'], request.user)
                task.files.add(uFile)
                task.save()
                request.user.message_set.create(message="uploaded %(file)s"%{'file':request.FILES['file'].name})
        elif request.POST["action"] == "remove":
            #dependency_form = TaskDependencyForm(project.tasks.all())
            user_file_form = UserFileForm(Account.objects.get(user = request.user))
            assign_form = AssignForm(project, instance=task)
            status_form = StatusForm(instance=task)
	    file_form = UploadFileForm()
	    fname = request.POST['file']
	    remove_uploaded_file(fname, task)
            request.user.message_set.create(message="removed %(file)s from task"%{'file':fname})
        elif request.POST["action"] == "userfile":
            #dependency_form = TaskDependencyForm(project.tasks.all())
            assign_form = AssignForm(project, instance=task)
            status_form = StatusForm(instance=task)
	    file_form = UploadFileForm()
	    user_file_form = UserFileForm(Account.objects.get(user = request.user))
	    #if user_file_form.is_valid():
	    acc = Account.objects.get(user = request.user)
            uFile = acc.files.get(name = request.POST['files'])
            task.files.add(uFile)
            task.save()
            request.user.message_set.create(message="added %(file)s to task"%{'file':uFile.name})
##        elif request.POST["action"] == "dependency":
##            dependency_form = TaskDependencyForm(project.tasks.all())
##            assign_form = AssignForm(project, instance=task)
##            status_form = StatusForm(instance=task)
##	    file_form = UploadFileForm()
##	    user_file_form = UserFileForm(Account.objects.get(user = request.user))
##	    #if user_file_form.is_valid():
##            dependency = Task.objects.get(summary = request.POST['tasks'])
##            task.dependencies.add(dependency)
##            task.save()
##            generate_graph(project, project.tasks.all())
##            request.user.message_set.create(message="added %(task)s as dependency"%{'task':dependency.summary})
        else:
            if is_member:
                user_file_form = UserFileForm(Account.objects.get(user = request.user))
                #dependency_form = TaskDependencyForm(project.tasks.all())
            else:
                user_file_form = UserFileForm(None)
                #dependency_form = TaskDependencyForm(None)
            assign_form = AssignForm(project, instance=task)
            status_form = StatusForm(instance=task)
	    file_form = UploadFileForm()
            if request.POST["action"] == "mark_resolved" and request.user == task.assignee:
                task.state = '2'
                task.save()
                request.user.message_set.create(message="task marked resolved")
                if notification:
                    notification.send(project.member_users.all(), "projects_task_change", {"user": request.user, "task": task, "project": project, "new_state": "resolved"})
            elif request.POST["action"] == "mark_closed" and request.user == task.creator:
                task.state = '3'
                task.save()
                request.user.message_set.create(message="task marked closed")
                if notification:
                    notification.send(project.member_users.all(), "projects_task_change", {"user": request.user, "task": task, "project": project, "new_state": "closed"})
            elif request.POST["action"] == "reopen" and is_member:
                task.state = '1'
                task.save()
                request.user.message_set.create(message="task reopened")
                if notification:
                    notification.send(project.member_users.all(), "projects_task_change", {"user": request.user, "task": task, "project": project, "new_state": "reopened"})
    else:
        if is_member:
            user_file_form = UserFileForm(Account.objects.get(user = request.user))
            #dependency_form = TaskDependencyForm(project.tasks.all())
        else:
            user_file_form = UserFileForm(None)
            #dependency_form = TaskDependencyForm(None)
        assign_form = AssignForm(project, instance=task)
        status_form = StatusForm(instance=task)
        file_form = UploadFileForm()
    
    return render_to_response(template_name, {
        "task": task,
        "is_member": is_member,
        "assign_form": assign_form,
        "status_form": status_form,
        "file_form": file_form,
        "user_file_form": user_file_form,
        #"dependency_form":dependency_form,
    }, context_instance=RequestContext(request))