Example #1
0
def group_list(request):
    username = request.user.username

    if request.method == "POST":
        """
        Add a new group.
        """
        result = {}
        content_type = "application/json; charset=utf-8"

        form = GroupAddForm(request.POST)
        if form.is_valid():
            group_name = form.cleaned_data["group_name"]

            # Check whether group name is duplicated.
            if request.cloud_mode:
                checked_groups = get_personal_groups_by_user(username)
            else:
                checked_groups = get_personal_groups(-1, -1)
            for g in checked_groups:
                if g.group_name == group_name:
                    result["error"] = _(u"There is already a group with that name.")
                    return HttpResponse(json.dumps(result), status=400, content_type=content_type)

            # Group name is valid, create that group.
            try:
                ccnet_threaded_rpc.create_group(group_name.encode("utf-8"), username)
                return HttpResponse(json.dumps({"success": True}), content_type=content_type)
            except SearpcError, e:
                result["error"] = _(e.msg)
                return HttpResponse(json.dumps(result), status=500, content_type=content_type)
        else:
            return HttpResponseBadRequest(json.dumps(form.errors), content_type=content_type)
Example #2
0
def share_repo(request):
    """
    Handle repo share request
    """
    if request.method != 'POST':
        raise Http404
    
    form = RepoShareForm(request.POST)
    if not form.is_valid():
        # TODO: may display error msg on form 
        raise Http404
    
    email_or_group = form.cleaned_data['email_or_group']
    repo_id = form.cleaned_data['repo_id']
    permission = form.cleaned_data['permission']
    from_email = request.user.username

    repo = get_repo(repo_id)
    if not repo:
        raise Http404

    is_encrypted = True if repo.encrypted else False
        
    # Test whether user is the repo owner.
    if not validate_owner(request, repo_id):
        return render_permission_error(request, _(u'Only the owner of the library has permission to share it.'))
    
    to_email_list = string2list(email_or_group)
    for to_email in to_email_list:
        if to_email == 'all':
            ''' Share to public '''

            # ignore 'all' if we're running in cloud mode
            if not CLOUD_MODE:
                try:
                    seafserv_threaded_rpc.set_inner_pub_repo(repo_id, permission)
                except:
                    msg = _(u'Failed to share to all members')
                    message.add_message(request, message.ERROR, msg)
                    continue

                msg = _(u'Shared to all members successfully, go check it at <a href="%s">Share</a>.') % \
                    (reverse('share_admin'))
                messages.add_message(request, messages.INFO, msg)
                
        elif to_email.find('@') == -1:
            ''' Share repo to group '''
            # TODO: if we know group id, then we can simplly call group_share_repo
            group_name = to_email

            # get all personal groups
            groups = get_personal_groups(-1, -1)
            find = False
            for group in groups:
                # for every group that user joined, if group name matchs,
                # then has find the group
                if group.props.group_name == group_name:
                    from seahub.group.views import group_share_repo
                    group_share_repo(request, repo_id, int(group.props.id),
                                     from_email, permission)
                    find = True
                    msg = _(u'Shared to %(group)s successfully,go check it at <a href="%(share)s">Share</a>.') % \
                            {'group':group_name, 'share':reverse('share_admin')}
                    
                    messages.add_message(request, messages.INFO, msg)
                    break
            if not find:
                msg = _(u'Failed to share to %s,as it does not exists.') % group_name
                messages.add_message(request, messages.ERROR, msg)
        else:
            ''' Share repo to user '''
            # Add email to contacts.
            mail_sended.send(sender=None, user=request.user.username,
                             email=to_email)

            if not is_registered_user(to_email):
                # Generate shared link and send mail if user has not registered.
                # kwargs = {'repo_id': repo_id,
                #           'repo_owner': from_email,
                #           'anon_email': to_email,
                #           'is_encrypted': is_encrypted,
                #           }
                # anonymous_share(request, **kwargs)
                msg = _(u'Failed to share to %s, as the email is not registered.') % to_email
                messages.add_message(request, messages.ERROR, msg)
                continue
            else:
                # Record share info to db.
                try:
                    seafserv_threaded_rpc.add_share(repo_id, from_email, to_email,
                                                    permission)
                except SearpcError, e:
                    msg = _(u'Failed to share to %s .') % to_email
                    messages.add_message(request, messages.ERROR, msg)
                    continue

                msg = _(u'Shared to %(email)s successfully,go check it at <a href="%(share)s">Share</a>.') % \
                        {'email':to_email, 'share':reverse('share_admin')}
                messages.add_message(request, messages.INFO, msg)