<s> import os <EOL> from django import template <EOL> from django . contrib . contenttypes . models import ContentType <EOL> from django . contrib . auth . models import User <EOL> from django . utils import simplejson <EOL> from django . template import TemplateSyntaxError <EOL> from django . template import Variable <EOL> from zorna . communities . models import Community , MessageCommunity , EventCommunity , PageCommunity <EOL> from zorna . acl . models import ACLPermission <EOL> from zorna import defines <EOL> from zorna . utilit import get_upload_path , get_upload_communities <EOL> from zorna . acl . models import get_acl_for_model , get_allowed_objects , get_acl_by_object <EOL> from zorna . communities . views import get_all_messages , get_messages_extra_by_content_type <EOL> register = template . Library ( ) <EOL> def auto_completion_search_members ( context , input_suggest ) : <EOL> """<STR_LIT>""" <EOL> request = context [ '<STR_LIT>' ] <EOL> input_suggest = input_suggest <EOL> data = [ ] <EOL> ao = set ( [ ] ) <EOL> ao_member = get_allowed_objects ( request . user , Community , '<STR_LIT>' ) <EOL> ao = ao . union ( set ( ao_member ) ) <EOL> ao_manage = get_allowed_objects ( request . user , Community , '<STR_LIT>' ) <EOL> ao = ao . union ( set ( ao_manage ) ) <EOL> communities = Community . objects . filter ( id__in = ao ) . order_by ( '<STR_LIT:name>' ) <EOL> objects = set ( [ ] ) <EOL> for com in communities : <EOL> objects = objects . union ( set ( get_acl_by_object ( com , '<STR_LIT>' ) ) ) <EOL> objects = objects . union ( set ( get_acl_by_object ( com , '<STR_LIT>' ) ) ) <EOL> data . append ( [ com . name , ( "<STR_LIT>" % str ( com . id ) ) ] ) <EOL> data . extend ( [ ( "<STR_LIT>" % ( x . last_name , x . first_name ) , ( "<STR_LIT>" % str ( x . id ) ) ) <EOL> for x in objects ] ) <EOL> members_data = simplejson . dumps ( data ) <EOL> return locals ( ) <EOL> auto_completion_search_members = register . inclusion_tag ( <EOL> '<STR_LIT>' , <EOL> takes_context = True <EOL> ) ( auto_completion_search_members ) <EOL> class user_communities ( template . Node ) : <EOL> def __init__ ( self , var_name ) : <EOL> self . var_name = var_name <EOL> def render ( self , context ) : <EOL> request = context [ '<STR_LIT>' ] <EOL> check = get_acl_for_model ( Community ) <EOL> allowed_objects = get_allowed_objects ( <EOL> request . user , Community , '<STR_LIT>' ) <EOL> communities = Community . objects . filter ( <EOL> id__in = allowed_objects ) . order_by ( '<STR_LIT:name>' ) <EOL> for com in communities : <EOL> com . manager = check . manage_community ( com , request . user ) <EOL> com . member = True <EOL> context [ self . var_name ] = communities <EOL> return '<STR_LIT>' <EOL> @ register . tag ( name = "<STR_LIT>" ) <EOL> def get_user_communities ( parser , token ) : <EOL> bits = token . split_contents ( ) <EOL> if <NUM_LIT:3> != len ( bits ) : <EOL> raise TemplateSyntaxError ( '<STR_LIT>' % bits [ <NUM_LIT:0> ] ) <EOL> if bits [ - <NUM_LIT:2> ] != '<STR_LIT>' : <EOL> raise TemplateSyntaxError ( <EOL> '<STR_LIT>' % bits [ <NUM_LIT:0> ] ) <EOL> varname = bits [ - <NUM_LIT:1> ] <EOL> return user_communities ( varname ) <EOL> class community_messages ( template . Node ) : <EOL> def __init__ ( self , community , var_name ) : <EOL> self . var_name = var_name <EOL> self . community = community <EOL> def render ( self , context ) : <EOL> messages = MessageCommunity . objects . select_related ( ) . filter ( <EOL> communities__exact = self . community ) <EOL> context [ self . var_name ] = messages <EOL> return '<STR_LIT>' <EOL> @ register . tag ( name = "<STR_LIT>" ) <EOL> def get_community_messages ( parser , token ) : <EOL> """<STR_LIT:U+0020>""" <EOL> bits = token . split_contents ( ) <EOL> if <NUM_LIT:4> != len ( bits ) : <EOL> raise TemplateSyntaxError ( '<STR_LIT>' % bits [ <NUM_LIT:0> ] ) <EOL> if bits [ - <NUM_LIT:2> ] != '<STR_LIT>' : <EOL> raise TemplateSyntaxError ( <EOL> '<STR_LIT>' % bits [ <NUM_LIT:0> ] ) <EOL> community = bits [ <NUM_LIT:1> ] <EOL> varname = bits [ - <NUM_LIT:1> ] <EOL> return community_messages ( community , varname ) <EOL> class attachments_message ( template . Node ) : <EOL> def __init__ ( self , message , var_name ) : <EOL> self . var_name = var_name <EOL> self . message = message <EOL> def render ( self , context ) : <EOL> path = "<STR_LIT>" % ( get_upload_path ( ) , self . message ) <EOL> try : <EOL> context [ self . var_name ] = [ f for f in os . listdir ( path ) ] <EOL> except : <EOL> context [ self . var_name ] = [ ] <EOL> return '<STR_LIT>' <EOL> @ register . tag ( name = "<STR_LIT>" ) <EOL> def get_attachments_message ( parser , token ) : <EOL> bits = token . split_contents ( ) <EOL> if <NUM_LIT:4> != len ( bits ) : <EOL> raise TemplateSyntaxError ( '<STR_LIT>' % bits [ <NUM_LIT:0> ] ) <EOL> if bits [ - <NUM_LIT:2> ] != '<STR_LIT>' : <EOL> raise TemplateSyntaxError ( <EOL> '<STR_LIT>' % bits [ <NUM_LIT:0> ] ) <EOL> message = bits [ <NUM_LIT:1> ] <EOL> varname = bits [ - <NUM_LIT:1> ] <EOL> return attachments_message ( message , varname ) <EOL> class last_communities_attachments ( template . Node ) : <EOL> def __init__ ( self , community , number , var_name ) : <EOL> self . community = Variable ( community ) <EOL> self . var_name = var_name <EOL> self . number = number <EOL> def render ( self , context ) : <EOL> request = context [ '<STR_LIT>' ] <EOL> community = self . community . resolve ( context ) <EOL> messages = get_all_messages ( request , community . pk if community else <NUM_LIT:0> ) <EOL> messages = messages . order_by ( '<STR_LIT>' ) <EOL> context [ self . var_name ] = [ ] <EOL> nbfiles = self . number <EOL> for msg in messages : <EOL> if nbfiles == <NUM_LIT:0> : <EOL> break <EOL> path = "<STR_LIT>" % ( get_upload_communities ( ) , msg . pk ) <EOL> try : <EOL> files = [ ] <EOL> for f in os . listdir ( path ) : <EOL> files . append ( f ) <EOL> nbfiles -= <NUM_LIT:1> <EOL> if nbfiles == <NUM_LIT:0> : <EOL> break <EOL> context [ self . var_name ] . append ( { '<STR_LIT:message>' : msg , '<STR_LIT>' : files } ) <EOL> except : <EOL> pass <EOL> return '<STR_LIT>' <EOL> @ register . tag ( name = "<STR_LIT>" ) <EOL> def get_last_communities_attachments ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> bits = token . split_contents ( ) <EOL> if <NUM_LIT:5> != len ( bits ) : <EOL> raise TemplateSyntaxError ( '<STR_LIT>' % bits [ <NUM_LIT:0> ] ) <EOL> if bits [ - <NUM_LIT:2> ] != '<STR_LIT>' : <EOL> raise TemplateSyntaxError ( <EOL> '<STR_LIT>' % bits [ <NUM_LIT:0> ] ) <EOL> community = bits [ <NUM_LIT:1> ] <EOL> number = int ( bits [ <NUM_LIT:2> ] ) <EOL> varname = bits [ - <NUM_LIT:1> ] <EOL> return last_communities_attachments ( community , number , varname ) <EOL> class last_communities_events ( template . Node ) : <EOL> def __init__ ( self , community , number , var_name ) : <EOL> self . community = Variable ( community ) <EOL> self . var_name = var_name <EOL> self . number = number <EOL> def render ( self , context ) : <EOL> request = context [ '<STR_LIT>' ] <EOL> community = self . community . resolve ( context ) <EOL> ct = ContentType . objects . get_for_model ( EventCommunity ) <EOL> messages = get_messages_extra_by_content_type ( <EOL> request , ct , community . pk if community else <NUM_LIT:0> ) <EOL> messages = messages . order_by ( '<STR_LIT>' ) <EOL> extra = { } <EOL> for m in messages : <EOL> extra [ m . object_id ] = m <EOL> m = EventCommunity . objects . filter ( <EOL> pk__in = extra . keys ( ) ) . order_by ( '<STR_LIT>' ) [ <NUM_LIT:0> : self . number ] <EOL> for c in m : <EOL> c . message = extra [ c . pk ] . message <EOL> context [ self . var_name ] = m <EOL> return '<STR_LIT>' <EOL> @ register . tag ( name = "<STR_LIT>" ) <EOL> def get_last_communities_events ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> bits = token . split_contents ( ) <EOL> if <NUM_LIT:5> != len ( bits ) : <EOL> raise TemplateSyntaxError ( '<STR_LIT>' % bits [ <NUM_LIT:0> ] ) <EOL> if bits [ - <NUM_LIT:2> ] != '<STR_LIT>' : <EOL> raise TemplateSyntaxError ( <EOL> '<STR_LIT>' % bits [ <NUM_LIT:0> ] ) <EOL> community = bits [ <NUM_LIT:1> ] <EOL> number = int ( bits [ <NUM_LIT:2> ] ) <EOL> varname = bits [ - <NUM_LIT:1> ] <EOL> return last_communities_events ( community , number , varname ) <EOL> class last_communities_pages ( template . Node ) : <EOL> def __init__ ( self , community , number , var_name ) : <EOL> self . community = Variable ( community ) <EOL> self . var_name = var_name <EOL> self . number = number <EOL> def render ( self , context ) : <EOL> request = context [ '<STR_LIT>' ] <EOL> community = self . community . resolve ( context ) <EOL> ct = ContentType . objects . get_for_model ( PageCommunity ) <EOL> messages = get_messages_extra_by_content_type ( <EOL> request , ct , community . pk if community else <NUM_LIT:0> ) <EOL> messages = messages . order_by ( '<STR_LIT>' ) <EOL> extra = { } <EOL> for m in messages : <EOL> extra [ m . object_id ] = m <EOL> m = PageCommunity . objects . filter ( <EOL> pk__in = extra . keys ( ) ) [ <NUM_LIT:0> : self . number ] <EOL> for c in m : <EOL> c . message = extra [ c . pk ] . message <EOL> context [ self . var_name ] = m <EOL> return '<STR_LIT>' <EOL> @ register . tag ( name = "<STR_LIT>" ) <EOL> def get_last_communities_pages ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> bits = token . split_contents ( ) <EOL> if <NUM_LIT:5> != len ( bits ) : <EOL> raise TemplateSyntaxError ( '<STR_LIT>' % bits [ <NUM_LIT:0> ] ) <EOL> if bits [ - <NUM_LIT:2> ] != '<STR_LIT>' : <EOL> raise TemplateSyntaxError ( <EOL> '<STR_LIT>' % bits [ <NUM_LIT:0> ] ) <EOL> community = bits [ <NUM_LIT:1> ] <EOL> number = int ( bits [ <NUM_LIT:2> ] ) <EOL> varname = bits [ - <NUM_LIT:1> ] <EOL> return last_communities_pages ( community , number , varname ) </s>
