<s> import os <EOL> from os . path import join <EOL> import urllib <EOL> from django . conf import settings <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from django . utils . encoding import smart_str <EOL> from django . core . paginator import Paginator , EmptyPage , PageNotAnInteger <EOL> from django import forms <EOL> from django . core . files . storage import FileSystemStorage <EOL> from django . contrib . contenttypes . models import ContentType <EOL> from zorna . utilit import get_upload_forms_attachments <EOL> from zorna . forms import fields <EOL> from zorna . acl . models import get_allowed_objects <EOL> from zorna . forms . models import FormsFieldEntry , FormsWorkspace , FormsForm , FormsFormEntry <EOL> from zorna . forms . forms import FormForForm <EOL> from zorna . communities . api import detete_message_community_extra <EOL> def isUserManager ( request , slug ) : <EOL> try : <EOL> fw = FormsWorkspace . objects . get ( slug = slug ) <EOL> ao = get_allowed_objects ( request . user , FormsWorkspace , '<STR_LIT>' ) <EOL> if fw . pk in ao : <EOL> return fw <EOL> else : <EOL> return False <EOL> except : <EOL> return False <EOL> def forms_get_entries ( slug_or_form , * args , ** kwargs ) : <EOL> """<STR_LIT:U+0020>""" <EOL> return FormsFieldEntry . objects . forms_get_entries ( slug_or_form , * args , ** kwargs ) <EOL> def forms_get_entry ( entry ) : <EOL> if not isinstance ( entry , FormsFormEntry ) : <EOL> entry = FormsFormEntry . objects . get ( pk = entry ) <EOL> columns , entries = forms_get_entries ( entry . form , entries = [ entry . pk ] ) <EOL> return columns , entries [ <NUM_LIT:0> ] <EOL> def forms_delete_entry ( entry ) : <EOL> if not isinstance ( entry , FormsFormEntry ) : <EOL> entry = FormsFormEntry . objects . get ( pk = entry ) <EOL> form = entry . form <EOL> fs = FileSystemStorage ( location = get_upload_forms_attachments ( ) ) <EOL> path = join ( fs . location , str ( form . pk ) , str ( entry . pk ) ) <EOL> if os . path . isdir ( path ) : <EOL> import shutil <EOL> shutil . rmtree ( path ) <EOL> ct = ContentType . objects . get_for_model ( FormsFormEntry ) <EOL> detete_message_community_extra ( entry , ct ) <EOL> FormsFieldEntry . objects . filter ( field__reference__startswith = '<STR_LIT>' % <EOL> form . slug , value = '<STR_LIT:%s>' % entry . pk ) . update ( value = '<STR_LIT>' ) <EOL> entry . delete ( ) <EOL> def forms_get_form ( slug ) : <EOL> try : <EOL> form = FormsForm . objects . get ( slug = slug ) <EOL> except FormsForm . DoesNotExist : <EOL> return None <EOL> return form <EOL> def forms_get_form_edit_entry ( request , entry ) : <EOL> try : <EOL> entry = FormsFormEntry . objects . get ( pk = entry ) <EOL> form = entry . form <EOL> except : <EOL> return None <EOL> rget = request . GET <EOL> args = ( form , request . POST or None , request . FILES or None ) <EOL> form_for_form = FormForForm ( * args , instance = entry , rget = rget ) <EOL> return form_for_form <EOL> def forms_get_form_add_entry ( request , form_slug , ** kwargs ) : <EOL> form = forms_get_form ( form_slug ) <EOL> if form : <EOL> kwargs [ '<STR_LIT>' ] = request . GET <EOL> args = ( form , request . POST or None , request . FILES or None ) <EOL> form_for_form = FormForForm ( * args , ** kwargs ) <EOL> return form_for_form <EOL> else : <EOL> return None <EOL> def get_users_profile_form_entries ( users ) : <EOL> try : <EOL> form = FormsForm . objects . get ( slug = settings . ZORNA_USER_PROFILE_FORM ) <EOL> try : <EOL> entries = FormsFormEntry . objects . filter ( <EOL> form = form , account__in = users ) <EOL> except FormsFormEntry . DoesNotExist : <EOL> entries = None <EOL> except Exception as e : <EOL> entries = None <EOL> if entries : <EOL> return forms_get_entries ( form , entries = entries ) <EOL> else : <EOL> return None , None <EOL> def get_users_profile_form_entry ( user ) : <EOL> c , r = get_users_profile_form_entries ( [ user ] ) <EOL> if r : <EOL> return c , r [ <NUM_LIT:0> ] <EOL> else : <EOL> return None , None <EOL> def forms_form_browse_entries ( request , slug , entries = None ) : <EOL> try : <EOL> form = FormsForm . objects . select_related ( depth = <NUM_LIT:1> ) . get ( slug = slug ) <EOL> except : <EOL> return None <EOL> extra_context = { } <EOL> kwargs = { } <EOL> sort = '<STR_LIT>' <EOL> if entries : <EOL> kwargs [ '<STR_LIT>' ] = entries <EOL> extra_context [ '<STR_LIT>' ] = { } <EOL> extra_context [ '<STR_LIT>' ] = [ ] <EOL> form_fields = form . fields . select_related ( ) . all ( ) . order_by ( '<STR_LIT:label>' ) <EOL> form . fields_reference = { } <EOL> hidden = request . GET . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if hidden : <EOL> hidden = hidden . split ( '<STR_LIT:U+002C>' ) <EOL> else : <EOL> hidden = [ ] <EOL> for f in form_fields : <EOL> fl = request . GET . get ( f . slug , None ) <EOL> if fl : <EOL> kwargs [ f . slug ] = smart_str ( fl ) <EOL> if f . for_sort : <EOL> sort = f . slug <EOL> if not f . visible_in_list : <EOL> hidden . append ( f . slug ) <EOL> extra_context [ '<STR_LIT>' ] = urllib . urlencode ( kwargs ) <EOL> kwargs [ '<STR_LIT:f>' ] = request . GET . get ( '<STR_LIT:f>' , '<STR_LIT>' ) <EOL> kwargs [ '<STR_LIT:q>' ] = request . GET . get ( '<STR_LIT:q>' , '<STR_LIT>' ) <EOL> kwargs [ '<STR_LIT:o>' ] = request . GET . get ( '<STR_LIT:o>' , sort ) <EOL> kwargs [ '<STR_LIT>' ] = request . GET . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> kwargs [ '<STR_LIT>' ] = request . GET . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> extra_context [ '<STR_LIT>' ] = [ ] <EOL> if kwargs [ '<STR_LIT>' ] : <EOL> try : <EOL> bte = kwargs [ '<STR_LIT>' ] . split ( '<STR_LIT::>' ) <EOL> extra_context [ '<STR_LIT>' ] = entry_id = bte [ <NUM_LIT:1> ] <EOL> bte = bte [ <NUM_LIT:0> ] . split ( '<STR_LIT:.>' ) <EOL> extra_context [ '<STR_LIT>' ] = form_slug = bte [ <NUM_LIT:0> ] <EOL> extra_context [ '<STR_LIT>' ] = form_field = bte [ <NUM_LIT:1> ] <EOL> while entry_id : <EOL> e = FormsFormEntry . objects . select_related ( ) . get ( <EOL> pk = entry_id , form__slug = form_slug ) <EOL> c , r = forms_get_entry ( e ) <EOL> extra_context [ '<STR_LIT>' ] . append ( { '<STR_LIT>' : r [ <EOL> form_field ] , '<STR_LIT>' : e } ) <EOL> hidden . append ( r [ form_field ] [ '<STR_LIT>' ] ) <EOL> if not e . form . bind_to_entry : <EOL> break <EOL> bte = e . form . bind_to_entry . split ( '<STR_LIT:.>' ) <EOL> form_slug = bte [ <NUM_LIT:0> ] <EOL> form_field = bte [ <NUM_LIT:1> ] <EOL> entry_id = e . entry . pk <EOL> except Exception as e : <EOL> extra_context [ '<STR_LIT>' ] = [ ] <EOL> extra_context [ '<STR_LIT>' ] . reverse ( ) <EOL> kwargs [ '<STR_LIT>' ] = '<STR_LIT:U+002C>' . join ( hidden ) <EOL> columns , entries = forms_get_entries ( form , ** kwargs ) <EOL> for f in form_fields : <EOL> if '<STR_LIT:.>' in f . reference and f . is_a ( * fields . CHOICES ) : <EOL> fl = request . GET . get ( f . slug , None ) <EOL> choices = [ ( '<STR_LIT>' , '<STR_LIT>' % f . label ) ] <EOL> for e in form . fields_reference [ f . pk ] : <EOL> choices . append ( ( e [ <NUM_LIT:0> ] , e [ <NUM_LIT:1> ] ) ) <EOL> s = forms . Select ( choices = choices ) <EOL> extra_context [ '<STR_LIT>' ] [ f . label ] = s . render ( f . slug , fl , None ) <EOL> else : <EOL> extra_context [ '<STR_LIT>' ] . append ( [ f . label , f . slug ] ) <EOL> if fl : <EOL> kwargs [ f . slug ] = fl <EOL> paginator = Paginator ( entries , <NUM_LIT> ) <EOL> page = int ( request . GET . get ( '<STR_LIT>' , <NUM_LIT:1> ) ) <EOL> try : <EOL> rows = paginator . page ( page ) <EOL> except PageNotAnInteger : <EOL> rows = paginator . page ( <NUM_LIT:1> ) <EOL> except EmptyPage : <EOL> rows = paginator . page ( paginator . num_pages ) <EOL> extra_context [ '<STR_LIT:q>' ] = kwargs [ '<STR_LIT:q>' ] <EOL> extra_context [ '<STR_LIT:f>' ] = kwargs [ '<STR_LIT:f>' ] <EOL> extra_context [ '<STR_LIT>' ] = kwargs [ '<STR_LIT:o>' ] <EOL> extra_context [ '<STR_LIT>' ] = kwargs [ '<STR_LIT>' ] <EOL> extra_context [ '<STR_LIT>' ] = _ ( u'<STR_LIT>' ) <EOL> extra_context [ '<STR_LIT>' ] = form <EOL> extra_context [ '<STR_LIT>' ] = columns <EOL> extra_context [ '<STR_LIT>' ] = entries <EOL> extra_context [ '<STR_LIT>' ] = rows <EOL> extra_context [ '<STR_LIT>' ] = page <EOL> extra_context [ '<STR_LIT>' ] = paginator <EOL> extra_context [ '<STR_LIT>' ] = kwargs [ '<STR_LIT>' ] <EOL> try : <EOL> r = form . bind_to_entry . split ( '<STR_LIT:.>' ) <EOL> extra_context [ '<STR_LIT>' ] = r [ <NUM_LIT:0> ] <EOL> extra_context [ '<STR_LIT>' ] = r [ <NUM_LIT:1> ] <EOL> except : <EOL> pass <EOL> extra_context [ '<STR_LIT>' ] = kwargs [ '<STR_LIT>' ] <EOL> return extra_context </s>
