'Handle sharing a visualization with a particular user.'

@web.expose
@web.require_login('share    Galaxy    visualizations')
def share(self, trans, id=None, email='', use_panels=False):

    msg = mtype = None
    visualization = self.get_visualization(trans, id, check_ownership=True)
    if email:
        other = trans.sa_session.query(model.User).filter(and_((model.User.table.c.email == email), (model.User.table.c.deleted == false()))).first()
        if (not other):
            mtype = 'error'
            msg = ("User    '%s'    does    not    exist" % escape(email))
        elif (other == trans.get_user()):
            mtype = 'error'
            msg = 'You    cannot    share    a    visualization    with    yourself'
        elif (trans.sa_session.query(model.VisualizationUserShareAssociation).filter_by(user=other, visualization=visualization).count() > 0):
            mtype = 'error'
            msg = ("Visualization    already    shared    with    '%s'" % escape(email))
        else:
            share = model.VisualizationUserShareAssociation()
            share.visualization = visualization
            share.user = other
            session = trans.sa_session
            session.add(share)
            self.create_item_slug(session, visualization)
            session.flush()
            viz_title = escape(visualization.title)
            other_email = escape(other.email)
            trans.set_message(("Visualization    '%s'    shared    with    user    '%s'" % (viz_title, other_email)))
            return trans.response.send_redirect(web.url_for(controller='visualization', action='sharing', id=id))
    return trans.fill_template('/ind_share_base.mako', message=msg, messagetype=mtype, item=visualization, email=email, use_panels=use_panels)
