def __new__(cls, name, bases, attrs): fields = [(field_name, attrs.pop(field_name)) for field_name, obj in \ attrs.items() if isinstance(obj, forms.Field)] fields.sort(lambda x, y: cmp(x[1].creation_counter, y[1].creation_counter)) for base in bases[::-1]: if hasattr(base, 'base_fields'): fields = base.base_fields.items() + fields attrs['base_fields'] = SortedDict(fields) if ('Meta' in attrs and hasattr(attrs['Meta'], 'model') and issubclass(attrs['Meta'].model, Model)): doc_fields = SortedDict() formfield_generator = getattr(attrs['Meta'], 'formfield_generator', RediscoFormFieldGenerator)() for field_name, field in iter_valid_fields(attrs['Meta']): doc_fields[field_name] = formfield_generator.generate( field_name, field) doc_fields[field_name].clean = redisco_validate_wrapper( doc_fields[field_name].clean, field.validate) doc_fields.update(attrs['base_fields']) attrs['base_fields'] = doc_fields attrs['_meta'] = attrs.get('Meta', object()) return super(RediscoFormMetaClass, cls).__new__(cls, name, bases, attrs)
def get_project_list(user=None, access=None, hidden=False): """ Returns a set of all projects a user has some level of access to. """ if access is None: access = MEMBER_USER # If we're not requesting specific access include all # public projects if access <= MEMBER_USER: qs = Project.objects.filter(public=True) if not hidden: qs = qs.filter(status=0) projects = SortedDict((p.pk, p) for p in qs) else: projects = SortedDict() # If the user is authenticated, include their memberships if user and user.is_authenticated(): qs = ProjectMember.objects.filter(user=user)\ .select_related('project') if not hidden: qs = qs.filter(project__status=0) projects.update(SortedDict((pm.project_id, pm.project) for pm in qs if pm.type <= access)) return projects
def get_available_skins(selected=None): """selected is a name of preferred skin if it's None, then information about all skins will be returned otherwise, only data about selected and default skins will be returned selected skin is guaranteed to be the first item in the dictionary """ skins = SortedDict() if hasattr(django_settings, 'ASKBOT_EXTRA_SKINS_DIR'): skins.update(get_skins_from_dir(django_settings.ASKBOT_EXTRA_SKINS_DIR)) stock_dir = os.path.normpath(os.path.dirname(__file__)) stock_skins = get_skins_from_dir(stock_dir) default_dir = stock_skins.pop('default') skins.update(stock_skins) if selected: if selected in skins: selected_dir = skins[selected] skins.clear() skins[selected] = selected_dir else: assert(selected == 'default') skins = SortedDict() #re-insert default as a last item skins['default'] = default_dir return skins
def get_containers(template): # Build a tree of the templates we're using, placing the root template first. levels = build_extension_tree(template.nodelist) contentlet_specs = [] contentreference_specs = SortedDict() blocks = {} for level in reversed(levels): level.initialize() contentlet_specs.extend( itertools.ifilter(lambda x: x not in contentlet_specs, level.contentlet_specs)) contentreference_specs.update(level.contentreference_specs) for name, block in level.blocks.items(): if block.block_super: blocks.setdefault(name, []).append(block) else: blocks[name] = [block] for block_list in blocks.values(): for block in block_list: block.initialize() contentlet_specs.extend( itertools.ifilter(lambda x: x not in contentlet_specs, block.contentlet_specs)) contentreference_specs.update(block.contentreference_specs) return contentlet_specs, contentreference_specs
def get_project_list(user=None, access=None, hidden=False, key='id'): """ Returns a SortedDict of all projects a user has some level of access to. """ if access is None: access = MEMBER_USER base_qs = Project.objects if not hidden: base_qs = base_qs.filter(status=0) # If we're not requesting specific access include all # public projects if access <= MEMBER_USER: qs = base_qs.filter(public=True) projects = SortedDict((getattr(p, key), p) for p in qs) else: projects = SortedDict() # If the user is authenticated, include their memberships if user and user.is_authenticated(): teams = get_team_list(user, access).values() qs = base_qs.filter(team__in=teams, ) projects.update(SortedDict((getattr(p, key), p) for p in qs)) return projects
def get_field_type(connection, table_name, row): """ Given the database connection, the table name, and the cursor row description, this routine will return the given field type name, as well as any additional keyword parameters and notes for the field. """ field_params = SortedDict() field_notes = [] try: field_type = connection.introspection.get_field_type(row[1], row) except KeyError: field_type = 'TextField' field_notes.append('This field type is a guess.') # This is a hook for DATA_TYPES_REVERSE to return a tuple of # (field_type, field_params_dict). if isinstance(field_type, tuple): field_type, new_params = field_type field_params.update(new_params) # Add max_length for all CharFields. if field_type == 'CharField' and row[3]: field_params['max_length'] = int(row[3]) if field_type == 'DecimalField': field_params['max_digits'] = row[4] field_params['decimal_places'] = row[5] return field_type, field_params, field_notes
def get(self, request, namespace=None, report_slug=None, widget_id=None): try: all_fields = SortedDict() if widget_id: w = Widget.objects.get(pk=widget_id) all_fields = w.collect_fields() else: report = Report.objects.get(namespace=namespace, slug=report_slug) fields_by_section = report.collect_fields_by_section() for c in fields_by_section.values(): all_fields.update(c) except: raise Http404 form = TableFieldForm(all_fields, use_widgets=False) # create object from the tablefield keywords # then populate it with the initial data that got generated by default keys = form._tablefields.keys() criteria = dict(zip(keys, [None]*len(keys))) criteria.update(form.data) return HttpResponse(json.dumps(criteria))
def urlencode(self): """Returns the urlencoded result of the parameters, ready to append in a query string or similar.""" parameters = SortedDict() parameters.update(self.sorted_items()) parameters['api_sig'] = self.hashed() return urlencode(parameters)
def get_available_skins(selected=None): """selected is a name of preferred skin if it's None, then information about all skins will be returned otherwise, only data about selected and default skins will be returned selected skin is guaranteed to be the first item in the dictionary """ skins = SortedDict() if hasattr(django_settings, 'ASKBOT_EXTRA_SKINS_DIR'): skins.update(get_skins_from_dir( django_settings.ASKBOT_EXTRA_SKINS_DIR)) stock_dir = os.path.normpath(os.path.dirname(__file__)) stock_skins = get_skins_from_dir(stock_dir) default_dir = stock_skins.pop('default') skins.update(stock_skins) if selected: if selected in skins: selected_dir = skins[selected] skins.clear() skins[selected] = selected_dir else: assert (selected == 'default') skins = SortedDict() #re-insert default as a last item skins['default'] = default_dir return skins
def get_project_list(user=None, access=None, hidden=False, key='id'): """ Returns a SortedDict of all projects a user has some level of access to. """ if access is None: access = MEMBER_USER base_qs = Project.objects if not hidden: base_qs = base_qs.filter(status=0) # If we're not requesting specific access include all # public projects if access <= MEMBER_USER: qs = base_qs.filter(public=True) projects = SortedDict((getattr(p, key), p) for p in qs) else: projects = SortedDict() # If the user is authenticated, include their memberships if user and user.is_authenticated(): teams = get_team_list(user, access).values() qs = base_qs.filter( team__in=teams, ) projects.update(SortedDict((getattr(p, key), p) for p in qs)) return projects
class TableForm(BaseColumnForm): def __init__(self, **kwargs): BaseColumnForm.__init__(self, dialect, **kwargs) # store the previous contents of self.fields and start again: achieves desired ordering of fields _temp = self.fields self.fields = SortedDict() self.fields['name'] = forms.CharField(widget=forms.TextInput(attrs={'class':'required'})) if dialect == 'mysql': self.fields['charset'] = forms.ChoiceField( choices = fns.make_choices(charsets), initial='latin1' ) engine_list, default_engine = [], '' for tup in engines: engine_list.append((tup[0],)) if tup[1] == 'DEFAULT': default_engine = tup[0] self.fields['engine'] = forms.ChoiceField( required = False, choices = fns.make_choices( engine_list ), initial = default_engine ) self.fields.update(_temp)
def get_available_skins(selected=None): """Returns a dictionary of skin name --> directory where "templates" and "media" subdirectories can be found. selected is a name of preferred skin if it's None, then information about all skins will be returned otherwise, only data about selected and default skins will be returned selected skin is guaranteed to be the first item in the dictionary """ skins = SortedDict() if hasattr(django_settings, 'ASKBOT_EXTRA_SKINS_DIR'): skins.update(get_skins_from_dir(django_settings.ASKBOT_EXTRA_SKINS_DIR)) if 'default' in skins: raise ValueError('"default" is not an acceptable name for a custom skin') if selected in skins: selected_dir = skins[selected] skins.clear() skins[selected] = selected_dir elif selected == 'default': skins = SortedDict() elif selected: raise ValueError( 'skin ' + str(selected) + \ ' not found, please check ASKBOT_EXTRA_SKINS_DIR setting ' + \ 'or in the corresponding directory' ) #insert default as a last item skins['default'] = askbot.get_install_directory() return skins
def create_schema(name, fields, attrs={}, module='dockit.models', base=SchemaBase, parents=(Schema,), **kwargs): all_attrs = SortedDict(fields) all_attrs.update(attrs) all_attrs['__module__'] = module if kwargs: all_attrs['Meta'] = UserMeta(**kwargs) return base.__new__(base, name, parents, all_attrs)
def get_field_type(connection, table_name, row): """ Given the database connection, the table name, and the cursor row description, this routine will return the given field type name, as well as any additional keyword parameters and notes for the field. """ field_params = SortedDict() field_notes = [] try: field_type = connection.introspection.get_field_type(row[1], row) except KeyError: field_type = 'TextField' field_notes.append('This field type is a guess.') # This is a hook for DATA_TYPES_REVERSE to return a tuple of # (field_type, field_params_dict). if type(field_type) is tuple: field_type, new_params = field_type field_params.update(new_params) # Add max_length for all CharFields. if field_type == 'CharField' and row[3]: field_params['max_length'] = int(row[3]) if field_type == 'DecimalField': field_params['max_digits'] = row[4] field_params['decimal_places'] = row[5] return field_type, field_params, field_notes
def __get_pagelet_form__(self,pagelet,user,post={}): """ Generates pagelet form """ if pagelet.opts.type == "form_only": fields = SortedDict({}) else: fields = SortedDict({'title':forms.CharField(label='Title',max_length=255,initial=pagelet.title),\ 'description':forms.CharField(label='Description',widget=forms.Textarea(attrs={'rows':30,'cols':85}),initial=pagelet.description)}) for fid,perm in self.__get_pagelet_field_perms__(pagelet,user).items(): if perm == 'r': readonly = True else: readonly = False field = Field.objects.get(id=fid) fields.update(self.__make_form_field__(field.get_form_field_name(),field.type,field.label,field.required,field.max_length,\ field.get_initial(),field.get_choices(),field.help_text,field.rows,field.cols,readonly)) PageletForm = self.__make_form__(fields,'PostPageletForm') if post: form = PageletForm(post) elif pagelet.post_data: form = PageletForm(initial=self.get_pagelet_post_data(pagelet,user)) else: form = PageletForm() field_order = self.__get_pagelet_fields_order__(pagelet,user) if field_order: form.fields.keyOrder = field_order return form
def get(self, request, namespace=None, report_slug=None, widget_id=None): try: all_fields = SortedDict() if widget_id: w = Widget.objects.get(pk=widget_id) all_fields = w.collect_fields() else: report = Report.objects.get(namespace=namespace, slug=report_slug) fields_by_section = report.collect_fields_by_section() for c in fields_by_section.values(): all_fields.update(c) except: raise Http404 form = TableFieldForm(all_fields, use_widgets=False) # create object from the tablefield keywords # then populate it with the initial data that got generated by default keys = form._tablefields.keys() criteria = dict(zip(keys, [None]*len(keys))) criteria.update(form.data) return HttpResponse(json.dumps(criteria))
def containers(self): """ Returns a tuple where the first item is a list of names of contentlets referenced by containers, and the second item is a list of tuples of names and contenttypes of contentreferences referenced by containers. This will break if there is a recursive extends or includes in the template code. Due to the use of an empty Context, any extends or include tags with dynamic arguments probably won't work. """ template = DjangoTemplate(self.code) # Build a tree of the templates we're using, placing the root template first. levels = build_extension_tree(template.nodelist) contentlet_specs = [] contentreference_specs = SortedDict() blocks = {} for level in reversed(levels): level.initialize() contentlet_specs.extend(itertools.ifilter(lambda x: x not in contentlet_specs, level.contentlet_specs)) contentreference_specs.update(level.contentreference_specs) for name, block in level.blocks.items(): if block.block_super: blocks.setdefault(name, []).append(block) else: blocks[name] = [block] for block_list in blocks.values(): for block in block_list: block.initialize() contentlet_specs.extend(itertools.ifilter(lambda x: x not in contentlet_specs, block.contentlet_specs)) contentreference_specs.update(block.contentreference_specs) return contentlet_specs, contentreference_specs
def get_containers(template): # Build a tree of the templates we're using, placing the root template first. levels = build_extension_tree(template.nodelist) contentlet_specs = [] contentreference_specs = SortedDict() blocks = {} for level in reversed(levels): level.initialize() contentlet_specs.extend(itertools.ifilter(lambda x: x not in contentlet_specs, level.contentlet_specs)) contentreference_specs.update(level.contentreference_specs) for name, block in level.blocks.items(): if block.block_super: blocks.setdefault(name, []).append(block) else: blocks[name] = [block] for block_list in blocks.values(): for block in block_list: block.initialize() contentlet_specs.extend(itertools.ifilter(lambda x: x not in contentlet_specs, block.contentlet_specs)) contentreference_specs.update(block.contentreference_specs) return contentlet_specs, contentreference_specs
def create_document(name, fields, attrs={}, module="dockit.models", base=DocumentBase, parents=(Document,), **kwargs): all_attrs = SortedDict(fields) all_attrs.update(attrs) all_attrs["__module__"] = module if kwargs: all_attrs["Meta"] = UserMeta(**kwargs) return base.__new__(base, name, parents, all_attrs)
def __new__(cls, name, bases, attrs): # get all valid existing Fields and sort them fields = [(field_name, attrs.pop(field_name)) for field_name, obj in \ attrs.items() if isinstance(obj, forms.Field)] fields.sort(lambda x, y: cmp(x[1].creation_counter, y[1].creation_counter)) # get all Fields from base classes for base in bases[::-1]: if hasattr(base, 'base_fields'): fields = base.base_fields.items() + fields # add the fields as "our" base fields attrs['base_fields'] = SortedDict(fields) # Meta class available? if 'Meta' in attrs and hasattr(attrs['Meta'], 'document') and \ issubclass(attrs['Meta'].document, BaseDocument): doc_fields = SortedDict() formfield_generator = getattr(attrs['Meta'], 'formfield_generator', \ MongoFormFieldGenerator)() widgets = getattr(attrs["Meta"], "widgets", {}) # walk through the document fields for field_name, field in iter_valid_fields(attrs['Meta']): # add field and override clean method to respect mongoengine-validator # use to get a custom widget if hasattr(field, 'get_custom_widget'): widget = field.get_custom_widget() else: widget = widgets.get(field_name, None) if widget: doc_fields[field_name] = formfield_generator.generate( field, widget=widget) else: doc_fields[field_name] = formfield_generator.generate( field) if not isinstance(field, FileField): doc_fields[field_name].clean = mongoengine_validate_wrapper( field, doc_fields[field_name].clean, field._validate) # write the new document fields to base_fields doc_fields.update(attrs['base_fields']) attrs['base_fields'] = doc_fields # maybe we need the Meta class later attrs['_meta'] = attrs.get('Meta', object()) new_class = super(MongoFormMetaClass, cls).__new__(cls, name, bases, attrs) if 'media' not in attrs: new_class.media = media_property(new_class) return new_class
class GoogleAPIProfileAdminForm(ProfileAdminForm): class Meta(ProfileAdminForm.Meta): fields = ['site', 'profile_id', 'display_features', 'is_enabled'] def __init__(self, *args, **kwargs): super(GoogleAPIProfileAdminForm, self).__init__(*args, **kwargs) self.fields['profile_id'].label = 'View (profile)' analytics_username = self.get_account_summaries()['username'] self.fields['profile_id'].help_text = 'Google Analytics views ' \ '(profiles) for {0}'.format(analytics_username) self.fields['profile_id'].required = True self.fields['profile_id'].widget = forms.Select( choices=self.get_profile_choices()) def get_profiles_list(self): service = self.instance.get_analytics_google_api_client() request = service.management().profiles().list( webPropertyId='~all', accountId='~all', fields='items(id,name,webPropertyId),username') return request.execute() def get_account_summaries(self): # TODO: Pagination? if not hasattr(self, '_account_summaries'): service = self.instance.get_analytics_google_api_client() request = service.management().accountSummaries().list() self._account_summaries = request.execute() return self._account_summaries def get_profiles(self): if not hasattr(self, '_profiles'): self._profiles = SortedDict() for account in self.get_account_summaries()['items']: for web_property in account['webProperties']: for profile in web_property['profiles']: self._profiles.update({ profile['id']: { 'profile': profile, 'web_property': web_property, 'account': account } }) return self._profiles def get_profile_choices(self): choices = [] for profile_id, data in self.get_profiles().items(): label = ' | '.join([ data['account']['name'], data['web_property']['name'], data['profile']['name'] ]) choices += [(profile_id, label)] return choices def save(self, *args, **kwargs): profile = self.get_profiles()[self.cleaned_data['profile_id']] self.instance.web_property_id = profile['web_property']['id'] return super(GoogleAPIProfileAdminForm, self).save(*args, **kwargs)
def flatten_list(self, l): flat_list = SortedDict() for index, item in enumerate(l): index = text_type(index) flat_item = self.flatten_item(item) nested_item = self.nest_flat_item(flat_item, index) flat_list.update(nested_item) return flat_list
def log_header_dict(self, *args, **kwargs): """ To be implemented in child classes. This will define what goes in the log header. This can be different for each log record dependant on the state of the object. """ d = SortedDict() d.update(kwargs) return d
def flatten_dict(self, d): flat_dict = SortedDict() for key, item in d.items(): key = text_type(key) flat_item = self.flatten_item(item) nested_item = self.nest_flat_item(flat_item, key) flat_dict.update(nested_item) return flat_dict
def flatten_dict(self, d): flat_dict = SortedDict() for key, item in d.items(): key = text_type(key) flat_item = self.flatten_item(item) nested_item = self.nest_flat_item(flat_item, key) flat_dict.update(nested_item) return flat_dict
def flatten_list(self, l): flat_list = SortedDict() for index, item in enumerate(l): index = text_type(index) flat_item = self.flatten_item(item) nested_item = self.nest_flat_item(flat_item, index) flat_list.update(nested_item) return flat_list
def parse_lookup(kwarg_items, opts): # Helper function that handles converting API kwargs # (e.g. "name__exact": "tom") to SQL. # Returns a tuple of (joins, where, params). # 'joins' is a sorted dictionary describing the tables that must be joined # to complete the query. The dictionary is sorted because creation order # is significant; it is a dictionary to ensure uniqueness of alias names. # # Each key-value pair follows the form # alias: (table, join_type, condition) # where # alias is the AS alias for the joined table # table is the actual table name to be joined # join_type is the type of join (INNER JOIN, LEFT OUTER JOIN, etc) # condition is the where-like statement over which narrows the join. # alias will be derived from the lookup list name. # # At present, this method only every returns INNER JOINs; the option is # there for others to implement custom Q()s, etc that return other join # types. joins, where, params = SortedDict(), [], [] for kwarg, value in kwarg_items: path = kwarg.split(LOOKUP_SEPARATOR) # Extract the last elements of the kwarg. # The very-last is the lookup_type (equals, like, etc). # The second-last is the table column on which the lookup_type is # to be performed. If this name is 'pk', it will be substituted with # the name of the primary key. # If there is only one part, or the last part is not a query # term, assume that the query is an __exact lookup_type = path.pop() if lookup_type == 'pk': lookup_type = 'exact' path.append(None) elif len(path) == 0 or lookup_type not in QUERY_TERMS: path.append(lookup_type) lookup_type = 'exact' if len(path) < 1: raise TypeError, "Cannot parse keyword query %r" % kwarg if value is None: # Interpret '__exact=None' as the sql '= NULL'; otherwise, reject # all uses of None as a query value. if lookup_type != 'exact': raise ValueError, "Cannot use None as a query value" elif callable(value): value = value() joins2, where2, params2 = lookup_inner(path, lookup_type, value, opts, opts.db_table, None) joins.update(joins2) where.extend(where2) params.extend(params2) return joins, where, params
def __new__(cls, name, bases, attrs): # get all valid existing Fields and sort them fields = [(field_name, attrs.pop(field_name)) for field_name, obj in \ attrs.items() if isinstance(obj, forms.Field)] fields.sort(lambda x, y: cmp(x[1].creation_counter, y[1].creation_counter)) # get all Fields from base classes for base in bases[::-1]: if hasattr(base, 'base_fields'): fields = base.base_fields.items() + fields # add the fields as "our" base fields attrs['base_fields'] = SortedDict(fields) # Meta class available? if 'Meta' in attrs and hasattr(attrs['Meta'], 'document') and \ issubclass(attrs['Meta'].document, BaseDocument): doc_fields = SortedDict() formfield_generator = getattr(attrs['Meta'], 'formfield_generator', \ MongoFormFieldGenerator)() # walk through the document fields for field_name, field in iter_valid_fields(attrs['Meta']): # add field and override clean method to respect mongoengine-validator doc_fields[field_name] = formfield_generator.generate(field_name, field) if doc_fields[field_name] is not None: doc_fields[field_name].clean = mongoengine_validate_wrapper( doc_fields[field_name].clean, field._validate) # write the new document fields to base_fields doc_fields.update(attrs['base_fields']) attrs['base_fields'] = doc_fields # sorting with respect to Meta.fields (like django forms) def get_indexes(x, y): meta_field_list = getattr(attrs['Meta'], 'fields') try: return meta_field_list.index(x[0]), meta_field_list.index(y[0]) except ValueError: return None, None if 'Meta' in attrs and hasattr(attrs['Meta'], 'fields'): flds = attrs['base_fields'].items() flds.sort(lambda x, y: cmp(*get_indexes(x,y))) attrs['base_fields']=SortedDict(flds) # maybe we need the Meta class later attrs['_meta'] = attrs.get('Meta', object()) return super(MongoFormMetaClass, cls).__new__(cls, name, bases, attrs)
def get_links(cls): """ Get fields materialising dependencies between 2 classes that don't lead to a delete cascading. """ dct = SortedDict() dct.update(many_to_many_fields(cls)) dct.update(reverse_many_to_many_fields(cls)) return dct
def __new__(cls, name, bases, attrs): # get all valid existing Fields and sort them fields = [(field_name, attrs.pop(field_name)) for field_name, obj in \ attrs.items() if isinstance(obj, forms.Field)] fields.sort(lambda x, y: cmp(x[1].creation_counter, y[1].creation_counter)) # get all Fields from base classes for base in bases[::-1]: if hasattr(base, 'base_fields'): fields = base.base_fields.items() + fields # add the fields as "our" base fields attrs['base_fields'] = SortedDict(fields) # Meta class available? if 'Meta' in attrs and hasattr(attrs['Meta'], 'document') and \ issubclass(attrs['Meta'].document, BaseDocument): doc_fields = SortedDict() formfield_generator = getattr(attrs['Meta'], 'formfield_generator', \ MongoFormFieldGenerator)() # walk through the document fields for field_name, field in iter_valid_fields(attrs['Meta']): # add field and override clean method to respect mongoengine-validator doc_fields[field_name] = formfield_generator.generate(field_name, field) if doc_fields[field_name] is not None: doc_fields[field_name].clean = mongoengine_validate_wrapper( doc_fields[field_name].clean, field._validate) # write the new document fields to base_fields doc_fields.update(attrs['base_fields']) attrs['base_fields'] = doc_fields # sorting with respect to Meta.fields (like django forms) def get_indexes(x, y): meta_field_list = getattr(attrs['Meta'], 'fields') try: return meta_field_list.index(x[0]), meta_field_list.index(y[0]) except ValueError: return None, None if 'Meta' in attrs and hasattr(attrs['Meta'], 'fields'): flds = attrs['base_fields'].items() flds.sort(lambda x, y: cmp(*get_indexes(x,y))) attrs['base_fields']=SortedDict(flds) # maybe we need the Meta class later attrs['_meta'] = attrs.get('Meta', object()) return super(MongoFormMetaClass, cls).__new__(cls, name, bases, attrs)
def parse_lookup(kwarg_items, opts): # Helper function that handles converting API kwargs # (e.g. "name__exact": "tom") to SQL. # Returns a tuple of (joins, where, params). # 'joins' is a sorted dictionary describing the tables that must be joined # to complete the query. The dictionary is sorted because creation order # is significant; it is a dictionary to ensure uniqueness of alias names. # # Each key-value pair follows the form # alias: (table, join_type, condition) # where # alias is the AS alias for the joined table # table is the actual table name to be joined # join_type is the type of join (INNER JOIN, LEFT OUTER JOIN, etc) # condition is the where-like statement over which narrows the join. # alias will be derived from the lookup list name. # # At present, this method only every returns INNER JOINs; the option is # there for others to implement custom Q()s, etc that return other join # types. joins, where, params = SortedDict(), [], [] for kwarg, value in kwarg_items: path = kwarg.split(LOOKUP_SEPARATOR) # Extract the last elements of the kwarg. # The very-last is the lookup_type (equals, like, etc). # The second-last is the table column on which the lookup_type is # to be performed. If this name is 'pk', it will be substituted with # the name of the primary key. # If there is only one part, or the last part is not a query # term, assume that the query is an __exact lookup_type = path.pop() if lookup_type == 'pk': lookup_type = 'exact' path.append(None) elif len(path) == 0 or lookup_type not in QUERY_TERMS: path.append(lookup_type) lookup_type = 'exact' if len(path) < 1: raise TypeError, "Cannot parse keyword query %r" % kwarg if value is None: # Interpret '__exact=None' as the sql '= NULL'; otherwise, reject # all uses of None as a query value. if lookup_type != 'exact': raise ValueError, "Cannot use None as a query value" elif callable(value): value = value() joins2, where2, params2 = lookup_inner(path, lookup_type, value, opts, opts.db_table, None) joins.update(joins2) where.extend(where2) params.extend(params2) return joins, where, params
def _populate_menu(r,name): lookups = SortedDict() for pattern in (r.url_patterns): # MenuURLPattern exportet as appname.projname - isinstance not work if type(pattern).__name__ == "MenuURLPattern" and pattern.menu == name: lookups[pattern.name]=pattern elif isinstance(pattern, RegexURLResolver): lookups.update(_populate_menu(pattern,name)) return lookups
def __init__(self, *args, **kwargs): """ This form is used only to create a partner. Had to change self.fields into an OrderDict to preserve order then 'append' to the new fields because new fields need to be first. """ self.user = kwargs.pop('user', '') super(NewPartnerForm, self).__init__(*args, **kwargs) # add location fields to form if this is a new contact if not self.instance.name: notes = self.fields.pop('notes') self.fields.update(LocationForm().fields) self.fields['city'].required = False self.fields['state'].required = False # move notes field to the end self.fields['notes'] = notes for field in self.fields.itervalues(): # primary contact information isn't required to create a partner field.required = False model_fields = SortedDict(self.fields) new_fields = { 'partnername': forms.CharField( label="Partner Organization", max_length=255, required=True, help_text="Name of the Organization", widget=forms.TextInput( attrs={'placeholder': 'Partner Organization', 'id': 'id_partner-partnername'})), 'partnersource': forms.CharField( label="Source", max_length=255, required=False, help_text="Website, event, or other source where you found the partner", widget=forms.TextInput( attrs={'placeholder': 'Source', 'id': 'id_partner-partnersource'})), 'partnerurl': forms.URLField( label="URL", max_length=255, required=False, help_text="Full url. ie http://partnerorganization.org", widget=forms.TextInput(attrs={'placeholder': 'URL', 'id': 'id_partner-partnerurl'})), 'partner-tags': forms.CharField( label='Tags', max_length=255, required=False, help_text="ie 'Disability', 'veteran-outreach', etc. Separate tags with a comma.", widget=forms.TextInput(attrs={'id': 'p-tags', 'placeholder': 'Tags'})) } ordered_fields = SortedDict(new_fields) ordered_fields.update(model_fields) self.fields = ordered_fields autofocus_input(self, 'partnername')
def get_template_titles(template_names): """ Looks for title pattern in each template. Returns a dictionary """ templates = SortedDict() for name in template_names: template, source = get_template(name) m = re.search(TITLE_PATTERN, template) title = m.group("title") if m else name templates.update({name: title}) return templates
def tabulated_data_series(self): tabulated_data = [] first_level_locations = self.location_parent.get_children().order_by('name')[:10] for location in first_level_locations: for child_location, answers in self.hierarchical_count_for(location).items(): tab_data = SortedDict({location.type.name: location.name}) tab_data[child_location.type.name] = child_location.name tab_data.update(answers) tab_data.update({'Total': sum(answers.values())}) tabulated_data.append(tab_data) return tabulated_data
def __build_form__(self,model,obj_id=None,obj_name=None,edit=False): klass = self.__get_class__(model) spec = self.__get_form_spec__(model,edit)() #Build form based on model fields fields = SortedDict({}) if hasattr(spec,'extra_fields'): for name,field in spec.extra_fields.items(): fields.update({name:field}) for field in klass._fields.values(): if not field.editable: continue if hasattr(spec,'exclude_fields'): if field.name in spec.exclude_fields: continue if not field.label: label = headerify(field.name) else: label = field.name if field.required: label += ' *' if field.type == 'ListField' and field.field.type == 'ReferenceField': choices = field.field.document_type_obj.get_for_choices() elif field.type == 'ReferenceField': if field.document_type_obj == "self": choices = field.document_type.get_for_choices() # Remove self reference from choices if obj_id: try: choices.remove((obj_id,obj_name)) except ValueError: pass else: if model == "site" and field.document_type_obj.__name__ == "Category": choices = [ (cat.id,cat.name) for cat in Category.objects(type="sitehome") ] choices.insert(0,("","=== Select ==")) else: choices = field.document_type_obj.get_for_choices() elif field.choices: choices = field.choices else: choices = None if hasattr(field,'max_length') and field.max_length: max_length = field.max_length else: max_length = 255 if field.default: default = field.default else: default = "" field_dict = self.__make_form_field__(name=field.name,field_type=field.type,label=label,required=field.required,\ max_length=max_length,initial=default,choices=choices,help_text=field.help_text) if field_dict: fields.update(field_dict) return self.__make_form__(fields,'AddEditForm')
def new_lookup_inner(path, lookup_type, value, opts, table, column): """ Patch django.db.models.query.lookup_inner from the outside to recognize the translation fields. Ideally this should go into lookup_inner. """ # check if there is anything to do for us here. If not, send it # all back to the original lookup_inner. if not hasattr(opts, 'translation_model'): return old_lookup_inner(path, lookup_type, value, opts, table, column) translation_opts = opts.translation_model._meta if path[0] not in translation_opts.translated_fields: return old_lookup_inner(path, lookup_type, value, opts, table, column) # If we got here then path[0] _is_ a translatable field (or a # localised version of one) in a model with translations. joins, where, params = SortedDict(), [], [] name = path.pop(0) current_table = table field, language_id = translation_opts.translated_fields[name] if language_id is None: language_id = get_default_language() translation_table = get_translation_table_alias(translation_opts.db_table, language_id) new_table = (current_table + "__" + translation_table) # add the join necessary for the current step qn = backend.quote_name condition = ('((%s.master_id = %s.id) AND (%s.language_id = %s))' % (new_table, current_table, new_table, language_id)) joins[qn(new_table)] = (qn(translation_opts.db_table), 'LEFT JOIN', condition) if path: joins2, where2, params2 = \ models.query.lookup_inner(path, lookup_type, value, translation_opts, new_table, translation_opts.pk.column) joins.update(joins2) where.extend(where2) params.extend(params2) else: trans_table_name = opts.translation_model._meta.db_table where.append(get_where_clause(lookup_type, new_table + '.', field.attname, value)) params.extend(field.get_db_prep_lookup(lookup_type, value)) return joins, where, params
def __new__(cls, name, bases, attrs): # get all valid existing Fields and sort them fields = [(field_name, attrs.pop(field_name)) for field_name, obj in \ attrs.items() if isinstance(obj, forms.Field)] fields.sort(lambda x, y: cmp(x[1].creation_counter, y[1].creation_counter)) # get all Fields from base classes for base in bases[::-1]: if hasattr(base, 'base_fields'): fields = base.base_fields.items() + fields # add the fields as "our" base fields attrs['base_fields'] = SortedDict(fields) # Meta class available? if 'Meta' in attrs and hasattr(attrs['Meta'], 'document') and \ issubclass(attrs['Meta'].document, BaseDocument): doc_fields = SortedDict() formfield_generator = getattr(attrs['Meta'], 'formfield_generator', \ MongoFormFieldGenerator)() widgets = getattr(attrs["Meta"], "widgets", {}) # walk through the document fields for field_name, field in iter_valid_fields(attrs['Meta']): # add field and override clean method to respect mongoengine-validator # use to get a custom widget if hasattr(field, 'get_custom_widget'): widget = field.get_custom_widget() else: widget = widgets.get(field_name, None) if widget: doc_fields[field_name] = formfield_generator.generate( field, widget=widget) else: doc_fields[field_name] = formfield_generator.generate( field) doc_fields[field_name].clean = mongoengine_validate_wrapper( field, doc_fields[field_name].clean, field._validate) # write the new document fields to base_fields doc_fields.update(attrs['base_fields']) attrs['base_fields'] = doc_fields # maybe we need the Meta class later attrs['_meta'] = attrs.get('Meta', object()) return super(MongoFormMetaClass, cls).__new__(cls, name, bases, attrs)
def _do_clean_data(self, view_name, view_key, cols_list): data = Person.view(view_name, key=view_key, include_docs=True).all() cleaned_data = [] for row_dict in data: short_dict = SortedDict(zip(cols_list, map(lambda x: None, cols_list))) row_json = row_dict.to_json() for key in row_json: if key in cols_list: short_dict.update({key: row_json[key]}) if short_dict not in cleaned_data: cleaned_data.append(short_dict) return cleaned_data
def tabulated_data_series(self): tabulated_data = [] first_level_locations = self.location_parent.get_children().order_by( 'name')[:10] for location in first_level_locations: for child_location, answers in self.hierarchical_count_for( location).items(): tab_data = SortedDict({location.type.name: location.name}) tab_data[child_location.type.name] = child_location.name tab_data.update(answers) tab_data.update({'Total': sum(answers.values())}) tabulated_data.append(tab_data) return tabulated_data
def create_doc_dict(self, document, doc_key=None, owner_document=None): """ Generate a dictionary representation of the document. (no recursion) DO NOT CALL DIRECTLY """ # Get doc field for top level documents if owner_document: doc_field = owner_document._fields.get(doc_key, None) if doc_key else None else: doc_field = document._fields.get(doc_key, None) if doc_key else None # Generate the base fields for the document doc_dict = SortedDict({"_document": document if owner_document is None else owner_document, "_key": document.__class__.__name__.lower() if doc_key is None else doc_key, "_document_field": doc_field}) if not isinstance(document, TopLevelDocumentMetaclass) and doc_key: doc_dict.update({"_field_type": EmbeddedDocumentField}) if isinstance(document, TopLevelDocumentMetaclass) or \ isinstance(document, Document): # Note: 只有集合才能有此属性, 其余的如EmbeddedDocument没有这些属性. # 不在Add,Edit中显示,默认情况下在List下显示. only_show_in_list = getattr(document, 'only_show_in_list', ()) # 在Edit界面只读, 在Add界面不显示. # TODO: getattr from document, not from mongoadmin. # so can remove if isinstance(document, TopLevelDocumentMetaclass) show_in_edit = getattr(document.mongoadmin, 'show_in_edit', ()) # 与only_show_in_list相对的概念, 允许在Add, Edit界面中被编辑. allowed_edit = getattr(document, 'allowed_edit', document._fields_ordered) # 一定要有id, 否则无法修改数据. if 'id' not in allowed_edit: allowed_edit = ('id', ) + allowed_edit # 完全不在List,Add,Edit中显示, 在model中用作另外的用途. fake_list = getattr(document, 'fake_list', ()) else: only_show_in_list, fake_list, show_in_edit = (), (), () allowed_edit = document._fields_ordered for key in allowed_edit: if key in only_show_in_list or key in show_in_edit: continue if key in fake_list: continue field = document._fields.get(key) doc_dict[key] = field # doc_dict被显示在编辑界面(AddForm与EditForm)中. return doc_dict
def containers(self): """ Returns a tuple where the first item is a list of names of contentlets referenced by containers, and the second item is a list of tuples of names and contenttypes of contentreferences referenced by containers. This will break if there is a recursive extends or includes in the template code. Due to the use of an empty Context, any extends or include tags with dynamic arguments probably won't work. """ template = DjangoTemplate(self.code) def build_extension_tree(nodelist): nodelists = [] extends = None for node in nodelist: if not isinstance(node, TextNode): if isinstance(node, ExtendsNode): extends = node break if extends: if extends.nodelist: nodelists.append(LazyContainerFinder(extends.nodelist, extends=True)) loaded_template = getattr(extends, LOADED_TEMPLATE_ATTR) nodelists.extend(build_extension_tree(loaded_template.nodelist)) else: # Base case: root. nodelists.append(LazyContainerFinder(nodelist)) return nodelists # Build a tree of the templates we're using, placing the root template first. levels = build_extension_tree(template.nodelist)[::-1] contentlet_specs = set() contentreference_specs = SortedDict() blocks = {} for level in levels: level.initialize() contentlet_specs |= level.contentlet_specs contentreference_specs.update(level.contentreference_specs) for name, block in level.blocks.items(): if block.block_super: blocks.setdefault(name, []).append(block) else: blocks[name] = [block] for block_list in blocks.values(): for block in block_list: block.initialize() contentlet_specs |= block.contentlet_specs contentreference_specs.update(block.contentreference_specs) return contentlet_specs, contentreference_specs
def get_sql(self, opts): joins, where, params = SortedDict(), [], [] for val in self.args: try: joins2, where2, params2 = val.get_sql(opts) joins.update(joins2) where.extend(where2) params.extend(params2) except EmptyResultSet: if not isinstance(self, QOr): raise EmptyResultSet if where: return joins, ['(%s)' % self.operator.join(where)], params return joins, [], params
def get_sql(self, opts): joins, where, params = SortedDict(), [], [] for val in self.args: try: joins2, where2, params2 = val.get_sql(opts) joins.update(joins2) where.extend(where2) params.extend(params2) except EmptyResultSet: if not isinstance(self, QOr): raise EmptyResultSet if where: return joins, ['(%s)' % self.operator.join(where)], params return joins, [], params
def _get_recurring_data(self): """Populate request params for establishing recurring payments""" pre = 'paymentrequest_0_' params = SortedDict() if len(self.cart.recurring_lineitems) == 0: raise GatewayError( "The cart must have at least one recurring item to use recurring payments" ) if len(self.cart.recurring_lineitems) > 1: self.log.error( "Cannot have more than one subscription in one order for Paypal. Only processing the first one for %s", self.cart) item = self.cart.recurring_lineitems[0] params['currencycode'] = self.settings['CURRENCY_CODE'] params['amt'] = item.recurring_price params['desc'] = item.description params['shippingamt'] = item.recurring_shipping params['profilereference'] = self.cart.cart_uuid if self._is_immediate_payment(item): # We already created an initial payment, so move the recurring start out by one billing cycle if item.duration_unit == 'MONTH': delta = relativedelta(months=item.duration) elif item.duration_unit == 'DAY': delta = relativedelta(days=item.duration) startdate = datetime.today() + delta else: startdate = item.recurring_start params['profilestartdate'] = startdate.isoformat() params['billingperiod'] = BILLING_PERIOD[item.duration_unit] params['billingfrequency'] = item.duration if item.trial: params['trialbillingperiod'] = item.duration_unit params['trialbillingfrequency'] = item.duration params['trialtotalbillingcycles'] = item.trial_length params['trialamt'] = item.trial_price if self.cart.bill_street1: params.update(self._get_billing_address_params(pre)) params['email'] = self.cart.bill_email return params
def post(self, request, namespace=None, report_slug=None): # handle REST calls if report_slug is None: return self.http_method_not_allowed(request) logger.debug("Received POST for report %s, with params: %s" % (report_slug, request.POST)) report = get_object_or_404(Report, namespace=namespace, slug=report_slug) fields_by_section = report.collect_fields_by_section() all_fields = SortedDict() [all_fields.update(c) for c in fields_by_section.values()] form = TableFieldForm(all_fields, hidden_fields=report.hidden_fields, data=request.POST, files=request.FILES) response = [] for field in form.dynamic_fields(): response.append({'id': field.auto_id, 'html': str(field)}) return JsonResponse(response, safe=False)
def get_field_type(self, connection, table_name, row): """ Given the database connection, the table name, and the cursor row description, this routine will return the given field type name, as well as any additional keyword parameters and notes for the field. """ field_params = SortedDict() field_notes = [] try: if row[1] == 1700: # numric field_type = "FloatField" else: field_type = connection.introspection.get_field_type( row[1], row) except KeyError: field_type = 'TextField' field_notes.append('This field type is a guess.') # This is a hook for data_types_reverse to return a tuple of # (field_type, field_params_dict). if type(field_type) is tuple: field_type, new_params = field_type field_params.update(new_params) # Add max_length for all CharFields. if field_type == 'CharField' and row[3]: field_params['max_length'] = int(row[3]) if field_type == 'DecimalField': if row[4] is None or row[5] is None: field_notes.append( 'max_digits and decimal_places have been guessed, as this ' 'database handles decimal fields as float') field_params[ 'max_digits'] = row[4] if row[4] is not None else 10 field_params[ 'decimal_places'] = row[5] if row[5] is not None else 5 else: field_params['max_digits'] = row[4] field_params['decimal_places'] = row[5] return field_type, field_params, field_notes
def get_queries_by_score(query_string, search_fields, min_len=1): """ Returns a list of queries, with each element being a combination of Q objects. That combination aims to search keywords within a model by testing the given search fields. """ kwargs = { "findterms": re.compile(r'"([^"]+)"|(\S{%d,})' % min_len).findall } if min_len > 1 else {} terms = normalize_query(query_string, **kwargs) query_dict = SortedDict() for score in range(len(terms), 0, -1): queries = None term_combinations = itertools.combinations(terms, score) for term_combination in term_combinations: query = get_query("", search_fields, term_combination) queries = queries | (query) if queries is not None else (query) query_dict.update({score: queries}) return query_dict
def by_group(self): # get all attributes # find which have groups # return a dict of attribute groups result = self.get_query_set().filter(active=True).order_by('attribute__priority').prefetch_related() used_groups = [g.attribute.group.id for g in result if g.attribute.group is not None] _groups = AttributeGroup.objects.filter(id__in=used_groups).order_by('priority','name') grouped = SortedDict([(g.name,{'priority':g.priority,'label':g.label,'attributes':[]}) for g in _groups]) ungrouped = {'attributes':[]} for att in result: if att.attribute.group is not None: grouped[att.attribute.group.name]['attributes'].insert(att.priority,att) else: ungrouped['attributes'].append(att) if len(ungrouped['attributes']) > 0: grouped.update({'ungrouped':ungrouped}) return grouped
def before(self, api, kwargs=None, **extra): """Formats given keyword arguments in MetaTrader format. <command>-arg1=val1|arg2=val2|... """ # Thanks to the bug in NEWACCOUNT, we are forced to use # SortedDict. def ensure_unicode(string): if isinstance(string, str): return string.decode('cp1251') return unicode(string) kwargs = SortedDict(kwargs or {}) kwargs.update(extra) key = kwargs.pop("key", None) if self.encoded and not key: raise MT4Error("Key required to call %r" % self) qs = u"%s-%s" % (self.command.upper(), u"|".join(u"{}={}".format(*map(ensure_unicode, pair)) for pair in kwargs.iteritems())) return (qs, ), {"key": key, "sensitive_fields_regexp": getattr(self, "sensitive_fields_regexp", None)}
def post(self, request, namespace=None, report_slug=None): if namespace is None or report_slug is None: return self.http_method_not_allowed(request) logger.debug("Received POST for report %s, with params: %s" % (report_slug, request.POST)) report = get_object_or_404(Report, namespace=namespace, slug=report_slug) fields_by_section = report.collect_fields_by_section() all_fields = SortedDict() [all_fields.update(c) for c in fields_by_section.values()] form = TableFieldForm(all_fields, hidden_fields=report.hidden_fields, data=request.POST, files=request.FILES) if form.is_valid(): logger.debug('Form passed validation: %s' % form) formdata = form.cleaned_data logger.debug('Form cleaned data: %s' % formdata) # parse time and localize to user profile timezone timezone = pytz.timezone(request.user.timezone) form.apply_timezone(timezone) if formdata['debug']: logger.debug("Debugging report and rotating logs now ...") management.call_command('rotate_logs') logger.debug("Report %s validated form: %s" % (report_slug, formdata)) # construct report definition now = datetime.datetime.now(timezone) widgets = report.widget_definitions(form.as_text()) report_def = self.report_def(widgets, now, formdata['debug']) logger.debug("Sending widget definitions for report %s: %s" % (report_slug, report_def)) return JsonResponse(report_def, safe=False) else: # return form with errors attached in a HTTP 400 Error response return HttpResponse(str(form.errors), status=400)
def _get_sql_clause(self): opts = self.model._meta # Construct the fundamental parts of the query: SELECT X FROM Y WHERE Z. select = [ "%s.%s" % (backend.quote_name(opts.db_table), backend.quote_name(f.column)) for f in opts.fields ] tables = [quote_only_if_word(t) for t in self._tables] joins = SortedDict() where = self._where[:] params = self._params[:] # Convert self._filters into SQL. joins2, where2, params2 = self._filters.get_sql(opts) joins.update(joins2) where.extend(where2) params.extend(params2) # Add additional tables and WHERE clauses based on select_related. if self._select_related: fill_table_cache(opts, select, tables, where, old_prefix=opts.db_table, cache_tables_seen=[opts.db_table], max_depth=self._max_related_depth) # Add any additional SELECTs. if self._select: select.extend([ '(%s) AS %s' % (quote_only_if_word(s[1]), backend.quote_name(s[0])) for s in self._select.items() ]) # Start composing the body of the SQL statement. sql = [" FROM", backend.quote_name(opts.db_table)] # Compose the join dictionary into SQL describing the joins. if joins: sql.append(" ".join([ "%s %s AS %s ON %s" % (join_type, table, alias, condition) for (alias, (table, join_type, condition)) in joins.items() ])) # Compose the tables clause into SQL. if tables: sql.append(", " + ", ".join(tables)) # Compose the where clause into SQL. if where: sql.append(where and "WHERE " + " AND ".join(where)) # ORDER BY clause order_by = [] if self._order_by is not None: ordering_to_use = self._order_by else: ordering_to_use = opts.ordering for f in handle_legacy_orderlist(ordering_to_use): if f == '?': # Special case. order_by.append(backend.get_random_function_sql()) else: if f.startswith('-'): col_name = f[1:] order = "DESC" else: col_name = f order = "ASC" if "." in col_name: table_prefix, col_name = col_name.split('.', 1) table_prefix = backend.quote_name(table_prefix) + '.' else: # Use the database table as a column prefix if it wasn't given, # and if the requested column isn't a custom SELECT. if "." not in col_name and col_name not in (self._select or ()): table_prefix = backend.quote_name(opts.db_table) + '.' else: table_prefix = '' order_by.append( '%s%s %s' % (table_prefix, backend.quote_name(orderfield2column(col_name, opts)), order)) if order_by: sql.append("ORDER BY " + ", ".join(order_by)) # LIMIT and OFFSET clauses if self._limit is not None: sql.append("%s " % backend.get_limit_offset_sql(self._limit, self._offset)) else: assert self._offset is None, "'offset' is not allowed without 'limit'" return select, " ".join(sql), params
class StatValueRowInstance(object): """ Maps an import row onto a collection of StatValue objects. Values for 'statistic' (StatDescription) and 'country' are initialized with the instance loader (called per row). YearFields are added for each non-country column. Implements ORM like interface on row: .save .delete """ years = None pk = None # Has to be faked to keep import-export happy def __init__(self, statistic, country): self.statistic = statistic self.country = country self.years = SortedDict() self.load_statvalues() def get_country(self): return self.country def load_statvalues(self): values = StatValue.objects.filter(description=self.statistic, country=self.country) to_update = SortedDict(values.values_list('year', 'value')) self.years.update(to_update) def __deepcopy__(self, memo): cls = self.__class__ result = cls.__new__(cls) memo[id(self)] = result for k, v in self.__dict__.items(): setattr(result, k, deepcopy(v, memo)) return result def add_yearfield(self, year, value): self.years[year] = value def get_value(self, year): """ Return StatValue.value for year: Decimal (has value) None (if value is None) (NA) False (if key (year) doesn't exist) """ return self.years.get(year, False) def save(self): for year, value in self.years.items(): try: obj = StatValue.objects.get( description=self.statistic, country=self.country, year=year ) obj.value = value except StatValue.DoesNotExist: obj = StatValue( description=self.statistic, country=self.country, year=year, value=value) obj.save()
def __new__(cls, class_name, bases, attrs): """Constructor for a new ModelForm class instance. The signature of this method is determined by Python internals. All Django Field instances are removed from attrs and added to the base_fields attribute instead. Additional Field instances are added to this based on the Datastore Model class specified by the Meta attribute. """ fields = sorted(((field_name, attrs.pop(field_name)) for field_name, obj in attrs.items() if isinstance(obj, forms.Field)), key=lambda obj: obj[1].creation_counter) for base in bases[::-1]: if hasattr(base, 'base_fields'): fields = base.base_fields.items() + fields declared_fields = SortedDict() for field_name, obj in fields: declared_fields[field_name] = obj opts = ModelFormOptions(attrs.get('Meta', None)) attrs['_meta'] = opts base_models = [] for base in bases: base_opts = getattr(base, '_meta', None) base_model = getattr(base_opts, 'model', None) if base_model is not None: base_models.append(base_model) if len(base_models) > 1: raise ImproperlyConfigured( "%s's base classes define more than one model." % class_name) if opts.model is not None: if base_models and base_models[0] is not opts.model: raise ImproperlyConfigured( '%s defines a different model than its parent.' % class_name) model_fields = SortedDict() for name, prop in sorted(opts.model.properties().iteritems(), key=lambda prop: prop[1].creation_counter): if opts.fields and name not in opts.fields: continue if opts.exclude and name in opts.exclude: continue form_field = prop.get_form_field() if form_field is not None: model_fields[name] = form_field if opts.widgets and name in opts.widgets: model_fields[name].widget = opts.widgets[name] model_fields.update(declared_fields) attrs['base_fields'] = model_fields props = opts.model.properties() for name, field in model_fields.iteritems(): prop = props.get(name) if prop: def clean_for_property_field(value, initial=None, prop=prop, old_clean=field.clean): value = old_clean(value) djangoforms.property_clean(prop, value) return value field.clean = clean_for_property_field else: attrs['base_fields'] = declared_fields # We're intentionally not calling our super's __new__ method, but we _do_ # want call the __new__ method on its super class (which is type). # pylint: disable=bad-super-call return super(djangoforms.ModelFormMetaclass, cls).__new__(cls, class_name, bases, attrs)
def handle_inspection(self, options): connection = connections[options.get('database')] # 'table_name_filter' is a stealth option table_name_filter = options.get('table_name_filter') table2model = lambda table_name: table_name.title().replace( '_', '').replace(' ', '').replace('-', '') strip_prefix = lambda s: s[1:] if s.startswith("u'") else s cursor = connection.cursor() yield "# This is an auto-generated Django model module." yield "# You'll have to do the following manually to clean this up:" yield "# * Rearrange models' order" yield "# * Make sure each model has one field with primary_key=True" yield "# * Remove `managed = False` lines if you wish to allow Django to create and delete the table" yield "# Feel free to rename the models, but don't rename db_table values or field names." yield "#" yield "# Also note: You'll have to insert the output of 'django-admin.py sqlcustom [appname]'" yield "# into your database." yield "from __future__ import unicode_literals" yield '' yield 'from %s import models' % self.db_module yield '' known_models = [] for table_name in connection.introspection.table_names(cursor): if table_name_filter is not None and callable(table_name_filter): if not table_name_filter(table_name): continue yield 'class %s(models.Model):' % table2model(table_name) known_models.append(table2model(table_name)) try: relations = connection.introspection.get_relations( cursor, table_name) except NotImplementedError: relations = {} try: indexes = connection.introspection.get_indexes( cursor, table_name) except NotImplementedError: indexes = {} used_column_names = [ ] # Holds column names used in the table so far for i, row in enumerate( connection.introspection.get_table_description( cursor, table_name)): comment_notes = [ ] # Holds Field notes, to be displayed in a Python comment. extra_params = SortedDict( ) # Holds Field parameters such as 'db_column'. column_name = row[0] is_relation = i in relations att_name, params, notes = self.normalize_col_name( column_name, used_column_names, is_relation) extra_params.update(params) comment_notes.extend(notes) used_column_names.append(att_name) # Add primary_key and unique, if necessary. if column_name in indexes: if indexes[column_name]['primary_key']: extra_params['primary_key'] = True elif indexes[column_name]['unique']: extra_params['unique'] = True if is_relation: rel_to = "self" if relations[i][ 1] == table_name else table2model(relations[i][1]) if rel_to in known_models: field_type = 'ForeignKey(%s' % rel_to else: field_type = "ForeignKey('%s'" % rel_to else: # Calling `get_field_type` to get the field type string and any # additional paramters and notes. field_type, field_params, field_notes = self.get_field_type( connection, table_name, row) extra_params.update(field_params) comment_notes.extend(field_notes) field_type += '(' # Don't output 'id = meta.AutoField(primary_key=True)', because # that's assumed if it doesn't exist. if att_name == 'id' and field_type == 'AutoField(' and extra_params == { 'primary_key': True }: continue # Add 'null' and 'blank', if the 'null_ok' flag was present in the # table description. if row[6]: # If it's NULL... if field_type == 'BooleanField(': field_type = 'NullBooleanField(' else: extra_params['blank'] = True if not field_type in ('TextField(', 'CharField('): extra_params['null'] = True field_desc = '%s = models.%s' % (att_name, field_type) if extra_params: if not field_desc.endswith('('): field_desc += ', ' field_desc += ', '.join([ '%s=%s' % (k, strip_prefix(repr(v))) for k, v in extra_params.items() ]) field_desc += ')' if comment_notes: field_desc += ' # ' + ' '.join(comment_notes) yield ' %s' % field_desc for meta_line in self.get_meta(table_name): yield meta_line
def __init__(self, *args, **kwargs): """ This form is used only to create a partner. Had to change self.fields into an OrderDict to preserve order then 'append' to the new fields because new fields need to be first. """ self.user = kwargs.pop('user', '') super(NewPartnerForm, self).__init__(*args, **kwargs) # add location fields to form if this is a new contact if not self.instance.name: notes = self.fields.pop('notes') self.fields.update(LocationForm().fields) self.fields['city'].required = False self.fields['state'].required = False # move notes field to the end self.fields['notes'] = notes for field in self.fields.itervalues(): # primary contact information isn't required to create a partner field.required = False model_fields = SortedDict(self.fields) new_fields = { 'partnername': forms.CharField(label="Partner Organization", max_length=255, required=True, help_text="Name of the Organization", widget=forms.TextInput( attrs={ 'placeholder': 'Partner Organization', 'id': 'id_partner-partnername' })), 'partnersource': forms.CharField( label="Source", max_length=255, required=False, help_text= "Website, event, or other source where you found the partner", widget=forms.TextInput(attrs={ 'placeholder': 'Source', 'id': 'id_partner-partnersource' })), 'partnerurl': forms.URLField( label="URL", max_length=255, required=False, help_text="Full url. ie http://partnerorganization.org", widget=forms.TextInput(attrs={ 'placeholder': 'URL', 'id': 'id_partner-partnerurl' })), 'partner-tags': forms.CharField( label='Tags', max_length=255, required=False, help_text= "ie 'Disability', 'veteran-outreach', etc. Separate tags with a comma.", widget=forms.TextInput(attrs={ 'id': 'p-tags', 'placeholder': 'Tags' })) } ordered_fields = SortedDict(new_fields) ordered_fields.update(model_fields) self.fields = ordered_fields autofocus_input(self, 'partnername')
def generate_model(model_description, mapping, db_key=''): """Uses instrospection to generate a Django model from a database table. """ connection = db.connections[db_key] cursor = connection.cursor() table_name = model_description.name table2model = lambda table_name: table_name.title().replace( '_', '').replace(' ', '').replace('-', '') strip_prefix = lambda s: s[1:] if s.startswith("u'") else s try: relations = connection.introspection.get_relations(cursor, table_name) except NotImplementedError: relations = {} try: indexes = connection.introspection.get_indexes(cursor, table_name) except NotImplementedError: indexes = {} used_column_names = [] # Holds column names used in the table so far for i, row in enumerate( connection.introspection.get_table_description(cursor, table_name)): comment_notes = [ ] # Holds Field notes, to be displayed in a Python comment. extra_params = SortedDict( ) # Holds Field parameters such as 'db_column'. column_name = row[0] is_relation = i in relations att_name, params, notes = normalize_col_name(column_name, used_column_names, is_relation) extra_params.update(params) comment_notes.extend(notes) used_column_names.append(att_name) # Add primary_key and unique, if necessary. if column_name in indexes: if indexes[column_name]['primary_key']: extra_params['primary_key'] = True elif indexes[column_name]['unique']: extra_params['unique'] = True # Calling `get_field_type` to get the field type string and any # additional parameters and notes field_type, field_params, field_notes = get_field_type( connection, table_name, row) extra_params.update(field_params) comment_notes.extend(field_notes) GEOM_FIELDS = { 'GEOMETRYCOLLECTION': 'GeometryCollectionField', 'POINT': 'PointField', 'MULTIPOINT': 'MultiPointField', 'LINESTRING': 'LineStringField', 'MULTILINESTRING': 'MultiLineStringField', 'POLYGON': 'PolygonField', 'MULTIPOLYGON': 'MultiPolygonField', 'GEOMETRY': 'GeometryField', } geom_type = mapping['geom'] # Use the geom_type to override the geometry field. if field_type == 'GeometryField': if geom_type in GEOM_FIELDS: field_type = GEOM_FIELDS[geom_type] # Change the type of id to AutoField to get auto generated ids. if att_name == 'id' and extra_params == {'primary_key': True}: field_type = 'AutoField' # Add 'null' and 'blank', if the 'null_ok' flag was present in the # table description. if row[6]: # If it's NULL... if field_type == 'BooleanField': field_type = 'NullBooleanField' else: extra_params['blank'] = True if not field_type in ('TextField', 'CharField'): extra_params['null'] = True field_desc = (att_name, field_type, extra_params) if any(field_type) and column_name != 'id': field, __ = Field.objects.get_or_create(model=model_description, name=att_name) field.type = field_type field.original_name = mapping[column_name] field.save() for name, value in extra_params.items(): if any(name): Setting.objects.get_or_create(field=field, name=name, value=value)
class VideoBackend(object): """ Base class used as parental class for backends. .. code-block:: python class MyBackend(VideoBackend): ... """ re_code = None """ Compiled regex (:py:func:`re.compile`) to search code in URL. Example: ``re.compile(r'myvideo\.com/\?code=(?P<code>\w+)')`` """ re_detect = None """ Compilede regec (:py:func:`re.compile`) to detect, if input URL is valid for current backend. Example: ``re.compile(r'^http://myvideo\.com/.*')`` """ pattern_url = None """ Pattern in which the code is inserted. Example: ``http://myvideo.com?code=%s`` """ pattern_thumbnail_url = None """ Pattern in which the code is inserted to get thumbnail url. Example: ``http://static.myvideo.com/thumbs/%s`` """ allow_https = True """ Sets if HTTPS version allowed for specific backend. """ template_name = 'embed_video/embed_code.html' """ Name of embed code template used by :py:meth:`get_embed_code`. Passed template variables: ``{{ backend }}`` (instance of VideoBackend), ``{{ width }}``, ``{{ height }}`` """ def __init__(self, url, is_secure=False, query=None): """ First it tries to load data from cache and if it don't succeed, run :py:meth:`init` and then save it to cache. """ self.is_secure = is_secure self.backend = self.__class__.__name__ self._url = url self.update_query(query) def update_query(self, query=None): self._query = SortedDict(self.get_default_query()) if query is not None: self._query.update(query) @cached_property def code(self): return self.get_code() @cached_property def url(self): return self.get_url() @cached_property def protocol(self): return 'https' if self.allow_https and self.is_secure else 'http' @cached_property def thumbnail(self): return self.get_thumbnail_url() @cached_property def info(self): return self.get_info() @classmethod def is_valid(cls, url): """ Class method to control if passed url is valid for current backend. By default it is done by :py:data:`re_detect` regex. """ return True if cls.re_detect.match(url) else False def get_code(self): """ Returns video code matched from given url by :py:data:`re_code`. """ match = self.re_code.search(self._url) if match: return match.group('code') def get_url(self): """ Returns URL folded from :py:data:`pattern_url` and parsed code. """ url = self.pattern_url.format(code=self.code, protocol=self.protocol) if self._query: url += '?' + urlencode(self._query, doseq=True) return mark_safe(url) def get_thumbnail_url(self): """ Returns thumbnail URL folded from :py:data:`pattern_thumbnail_url` and parsed code. """ return self.pattern_thumbnail_url.format(code=self.code, protocol=self.protocol) def get_embed_code(self, width, height): """ Returns embed code rendered from template :py:data:`template_name`. """ return render_to_string(self.template_name, { 'backend': self, 'width': width, 'height': height, }) def get_info(self): raise NotImplementedError def get_default_query(self): # Derive backend name from class name backend_name = self.__class__.__name__[:-7].upper() default = getattr(self, 'default_query', {}) settings_key = 'EMBED_VIDEO_{0}_QUERY'.format(backend_name) return getattr(settings, settings_key, default).copy()