def __new__(cls, clsname, bases, attrs): fields_present = set() fieldsets = SortedDict() fieldset_help = {} fieldset_sets = [] if attrs.has_key('fieldsets'): fieldset_sets.append(attrs['fieldsets']) fieldset_sets += [clazz.fieldsets for clazz in bases if hasattr(clazz, 'fieldsets')] for fieldset in fieldset_sets: for values in fieldset: if len(values) == 3: name, help_text, fields = values fieldset_help[name] = fieldset_help.get(name, help_text) else: name, fields = values fields = [f for f in fields if f not in fields_present] fields_present.update(fields) if not fields: continue if fieldsets.has_key(name): fieldsets[name] = fieldsets[name] + fields else: fieldsets[name] = fields if fieldsets.has_key(None): del fieldsets[None] attrs['fieldsets'] = fieldsets.items() attrs['fieldset_help'] = fieldset_help return super(FieldsetMetaclass, cls).__new__(cls, clsname, bases, attrs)
def prepare_idTransaction_dict(model_instance): fields = SortedDict() for field in model_instance._meta.fields: try: if getattr(model_instance, field.name) not in (None, ''): newfieldname ="%s" % (field.name) value = getattr(model_instance, field.name) #if a datetime sting, then turn into a datetime try: value = time.strptime(value, "%Y-%m-%d %H:%M:%S") except: pass try: value = time.strptime(value, "%Y-%m-%d") except: pass try: value = time.strptime(value, "%H:%M:%S") except: pass fields[newfieldname] = str(value) except: pass fields.insert(0,'_id', model_instance.transaction_id) if fields.has_key('extra_fields'): ef =json.loads(fields['extra_fields']) fields.update(ef) del fields['extra_fields'] if fields.has_key('tags'): fields['tags'] = json.loads(fields['tags']) #print "The tx type is ", fields['transaction_type'] if fields['transaction_type']=="omhe": if fields['text'] != "": p = parseomhe() d = p.parse(fields['text']) del d['transaction_id'], d['transaction_datetime'] fields.update(d) #for k in fields.keys(): # print k,"= ",fields[k] return fields
def history(request): """ Tally total expenses and income for each month """ history = SortedDict() entries = Entry.objects.all().order_by('-date') for e in entries: # Create dict key this_month = datetime.date(e.date.year, e.date.month, 1) if not history.has_key(this_month): history[this_month] = {'income':0, 'expense':0} #sum values for month if e.category.type in ['EXP', 'COGS']: history[this_month]['expense'] += e.amount elif e.category.type == 'INC': history[this_month]['income'] += e.amount for date, value_dict in history.items(): value_dict['net'] = value_dict['income'] - value_dict['expense'] return simple.direct_to_template(request, template='beancounter/history.html', extra_context = { 'history': history })
def group_districts_by_type(self): districts = self.options['districts'] by_type = SortedDict() for district in districts: if not by_type.has_key(district.type.title): by_type[district.type.title] = [] by_type[district.type.title].append(district) return by_type
def humanized_content_dict_for_model_instance(instance): """Returns a dictionary for the given Django model instance with normalized data.""" model = instance.__class__ opts = model._meta data_map = SortedDict() suppress_object_id = False for f in opts.fields: k = f.name.replace('_', ' ').title() v = getattr(instance, f.name) if hasattr(instance, 'get_%s_display' % f.name): m = getattr(instance, 'get_%s_display' % f.name) v = m() if type(f) == ForeignKey: try: to = f.related.parent_model if to == User: if v is not None: name = v.get_full_name() email = unicode(v.email) else: name = '' email = '' data_map[k] = unicode(name) data_map['%s Email' % k] = unicode(email) continue elif to == ContentType and f.name == 'content_type': if hasattr(instance, 'object_id'): try: data_map['Content Object'] = '%s: %s' % (v, v.get_object_for_this_type(id=getattr(instance, 'object_id'))) except: data_map['Content Object'] = '' pass suppress_object_id = True continue else: if v is not None: klass = ContentType.objects.get_for_model(v) data_map[k] = '%s: %s' % (klass, v) continue except Exception, e: print e pass if v == None: v = '' elif type(v) == datetime.date: v = v.strftime(smart_str(settings.DATE_FORMAT)) elif type(v) == datetime.datetime: v = v.strftime(smart_str(settings.DATETIME_FORMAT)) data_map[k] = v if suppress_object_id and data_map.has_key('Object Id'): del data_map['Object Id']
def _html_output(self, form_as, normal_row, help_text_html, sections_re, row_re): formsets = SortedDict() for bf in self.formsets: if bf.label: label = conditional_escape(force_unicode(bf.label)) # Only add the suffix if the label does not end in # punctuation. if self.form.label_suffix: if label[-1] not in ":?.!": label += self.form.label_suffix label = label or "" else: label = "" if bf.field.help_text: help_text = help_text_html % force_unicode(bf.field.help_text) else: help_text = u"" formsets[bf.name] = {"label": force_unicode(label), "field": unicode(bf), "help_text": help_text} try: output = [] data = form_as() section_search = sections_re.search(data) if formsets: hidden = u"".join(hidden_re.findall(data)) last_formset_name, last_formset = formsets.items()[-1] last_formset["field"] = last_formset["field"] + hidden formsets[last_formset_name] = normal_row % last_formset for name, formset in formsets.items()[:-1]: formsets[name] = normal_row % formset if not section_search: output.append(data) else: section_groups = section_search.groups() for row, head, item in row_re.findall(section_groups[1]): head_search = label_re.search(head) if head_search: id = head_search.groups()[1] if formsets.has_key(id): row = formsets[id] del formsets[id] output.append(row) for name, row in formsets.items(): if name in self.form.fields.keyOrder: output.append(row) return mark_safe(u"\n".join(output)) except Exception, e: import traceback return traceback.format_exc()
def get_data(self): zones = [] image_dict = {} instances = [] try: instances = api.proxy.server_list(self.request) except: servers = [] exceptions.handle(self.request, _('Unable to retrieve instances.'), ignore=True) try: for zone in api.proxy.availability_zone_list(self.request, True): zones.append(zone) try: images = SortedDict([(image.imageid, image) for image in image_utils.get_available_images(self.request, zone.id)]) except: images = {} image_dict.update(images) except: zones = [] exceptions.handle(request, _('Unable to retrieve zones.'), ignore=True) zone_dict = SortedDict([(zone.id, zone.name) for zone in zones]) gateway_dict = SortedDict([(g.hostname, g.vext_ip) for g in api.proxy.gateway_list(self.request)]) for instance in instances: if zone_dict.has_key(instance.zone_id): instance.zone_id = zone_dict[instance.zone_id] image = image_dict.get(instance.image_id) if image: instance.image_name = image.name if image.container_format == 'docker': instance.image_name += '(Container)' else: instance.image_name += '(VM)' else: instance.image_name = instance.image_id firewall = [(gateway_dict.get(f.hostname), f.gateway_port) for f in api.proxy.firewall_get(self.request, instance.id)] if firewall: instance.gateway = '%s:%s' % firewall[0] else: instance.gateway= '' return instances
def get_admin_method_values(self, qs, admin): values = SortedDict() other = 0 for obj in qs: val = getattr(admin, self.field_name)(obj) if (not val): other += 1 elif (values.has_key(val)): values[val] += 1 else: values[val] = 1 if other != 0: values[self.other_title] = other return values
def get_admin_method_values(self, qs, admin): values = SortedDict() for obj in qs: val = getattr(admin, self.field_name)(obj).isoweekday() if val == 7: val = 0 day_name = force_unicode(self.days[val]) if (values.has_key(day_name)): values[day_name] += 1 else: values[day_name] = 1 return values
def get_field_values(self, qs): values = SortedDict() other = 0 field = qs.model._meta.get_field(self.field_name) if (field.choices): count = 0 for choice in field.choices: num = qs.filter(**{field.name:choice[0]}).count() if (choice[0] != 'other'): values[choice[1]] = num count += num if (qs.count() != count): other = qs.count() - count elif (field.get_internal_type() == "ManyToManyField"): for obj in qs: for m2mobj in getattr(obj, field.name).all(): if (values.has_key(m2mobj.__unicode__())): values[m2mobj.__unicode__()] += 1 else: values[m2mobj.__unicode__()] = 1 elif (field.get_internal_type() == "BooleanField"): values[force_unicode(_('Yes'))] = qs.filter(**{field.name: True}).count() values[force_unicode(_('No'))] = qs.filter(**{field.name: False}).count() elif (field.get_internal_type() == "OrderedForeignKey" or field.get_internal_type() == "ForeignKey" or field.get_internal_type() == "CharField"): for obj in qs: val = getattr(obj, field.name) if (not val): other += 1 elif (values.has_key(val)): values[val] += 1 else: values[val] = 1 if other != 0: values[self.other_title] = other return values
def submissions(request): latest_strikes = Strike.objects.filter(start_date__gte=datetime.today().date()).order_by('start_date').filter(approved=False).exclude(submitter=1) companies = Company.objects.all() regions = Region.objects.all() strikes = SortedDict() hoje = datetime.today().strftime("%d") amanha = datetime.today().date() + timedelta(days=1) amanha = amanha.strftime("%d") for strike in latest_strikes: y = strike.start_date.strftime("%Y") m = strike.start_date.strftime("%m") d = strike.start_date.strftime("%d") if not strikes.has_key(y): strikes[y] = {} if not strikes[y].has_key(m): strikes[y][m] = {"name":strike.start_date.strftime("%B"), "days":SortedDict()} if not strikes[y][m]["days"].has_key(d): strikes[y][m]["days"][d] = {'greves':{}} if not strikes[y][m]["days"][d]['greves'].has_key(strike.company): strikes[y][m]["days"][d]['greves'][strike.company] = [] strikes[y][m]["days"][d]['greves'][strike.company].append(strike) y = datetime.today().strftime("%Y") m = datetime.today().strftime("%m") if len(strikes) > 0: fix = False if strikes[y][m]["days"].has_key(amanha): strikes[y][m]["days"][amanha]["alias"] = "Amanhã" fix = True if strikes[y][m]["days"].has_key(hoje): strikes[y][m]["days"][hoje]["alias"] = "Hoje" if fix: strikes[y][m]["days"][hoje]["fix"] = "fixAmanha" #strikes['04']["days"] = sorted(strikes['04']["days"]) context = { 'strikes': strikes, 'regions': regions, 'host': request.get_host(), 'companies': companies } return render_to_response('submissions.html', context, context_instance=RequestContext(request))
def _get_servers(request): servers = [] image_dict = SortedDict([(img["id"], img)for img in get_images(request)]) zone_dict = SortedDict([(zone.id, zone.name) for zone in get_zones(request)]) gateway_dict = SortedDict([(g.hostname, g.vext_ip) for g in api.proxy.gateway_list(request)]) for server in api.proxy.server_list(request): image = image_dict.get(server.image_id, {}) if zone_dict.has_key(server.zone_id): server.zone_id = zone_dict[server.zone_id] firewall = [(gateway_dict.get(f.hostname), f.gateway_port) for f in api.proxy.firewall_get(request, server.id)] servers.append(server_format(request, server, image, firewall)) return servers
def _html_output(self, form_as, normal_row, help_text_html, sections_re, row_re): formsets = SortedDict() for bf in self.formsets: if bf.label: label = conditional_escape(force_unicode(bf.label)) # Only add the suffix if the label does not end in # punctuation. if self.form.label_suffix: if label[-1] not in ':?.!': label += self.form.label_suffix label = label or '' else: label = '' if bf.field.help_text: help_text = help_text_html % force_unicode(bf.field.help_text) else: help_text = u'' formsets[bf.name] = normal_row % {'label': force_unicode(label), 'field': unicode(bf), 'help_text': help_text} try: output = [] data = form_as() section_search = sections_re.search(data) if not section_search: output.append(data) else: section_groups = section_search.groups() for row, head, item in row_re.findall(section_groups[1]): head_search = label_re.search(head) if head_search: id = head_search.groups()[1] if formsets.has_key(id): row = formsets[id] del formsets[id] output.append(row) for name, row in formsets.items(): if name in self.form.fields.keyOrder: output.append(row) return mark_safe(u'\n'.join(output)) except Exception,e: import traceback return traceback.format_exc()
def regroup_exams(exams): exams = exams.order_by('-semester', 'exam_type', 'number', 'has_solutions') d = SortedDict() headers = [] for e in exams: k = e.describe_exam_type() if k not in headers: headers.append(k) for e in exams: k1 = (e.semester.verbose_description(), e.instructor_names) k2 = e.describe_exam_type() if not d.has_key(k1): d[k1] = get_new_dict(headers) if d[k1].has_key(k2): d[k1][k2].append(e) else: d[k1][k2] = [e] return d, headers
def group_history_and_messages(history, messages, group_by=None): grouped_history = SortedDict() for a in history: a.extra_messages = {} for m in messages: if a.id == m['alert_history']: if not a.extra_messages.has_key(m['state']): a.extra_messages[m['state']] = { 'sms': None, 'email': None, 'jabber': None, } a.extra_messages[m['state']][m['type']] = m['message'] try: key = GROUPINGS[group_by]['group_by'](a) except AttributeError: key = None if not grouped_history.has_key(key): grouped_history[key] = [] grouped_history[key].append(a) return grouped_history
class RunningAverage(object): def __init__(self, max=datetime.timedelta(minutes=30)): """ @param max: Max duration after which data will be dropped """ super(RunningAverage, self).__init__() self._items = SortedDict() self.max = max def append(self, item, dtime = None): if not dtime: dtime = self.now() self._items[dtime] = item def remove(self, *items): for key in items: if self._items.has_key(key): del self._items[key] def sum(self, items): return sum([float(item) for item in items]) def average(self, items): if not items: return None return self.sum(items)/float(len(items)) def time_average(self, delta): now = self.now() values = [] for key, value in self._items.items(): diff = now - key if diff > self.max: self.remove(key) elif diff <= delta: values.append(value) return self.average(values) def rate(self, delta): if not self._items: return None now = self.now() values = [] last_dtime = None last_value = None for key, value in self._items.items(): diff = now - key if diff > self.max: self.remove(key) elif diff <= delta: if not last_dtime: last_dtime = key last_value = value continue rate = key - last_dtime seconds = rate.seconds + rate.days*86400 if not seconds: last_value += value continue values.append(float(value - last_value) / float(seconds)) last_dtime = key last_value = value return self.average(values) def now(self): """ Ability to alter resolution of time """ return datetime.datetime.now() def averages(self, *deltas): if not deltas: deltas = [datetime.timedelta(minutes=1)] values = [] for delta in deltas: values.append(self.time_average(delta)) return tuple(values) def rates(self, *deltas): if not deltas: deltas = [datetime.timedelta(minutes=1)] values = [] for delta in deltas: values.append(self.rate(delta)) return tuple(values) def __call__(self, *deltas): return self.averages(*deltas)
def import_from_shape(upload, start_row=0, max_rows=200000, create_int_style_cols=True): """ a shapeUpload object max_rows - any more than this is ignored centroid - if it's a (multi)polygon, should we also create a geometry_centroid field """ upload.status = 2 #set this right away so it doesn't get reprocessed upload.save() ds = DataSource(upload.shapefile) layer = ds[0] fields = layer.fields num_features = len(layer) #set max # of _style features max_distinct_style_vals = max(min(num_features / 100, 50), 10) print 'there are %d features' % num_features upload.total_rows = num_features if not num_features: print 'no rows, returning' upload.status = 6 upload.save() return rows = [] #get field types field_map = { 'OFTString': 'STRING', 'OFTReal': 'NUMBER', 'OFTInteger': 'NUMBER', 'OFTDate': 'DATETIME' } field_types = [field_map[f.__name__] for f in layer.field_types] field_layers = layer.fields #insert geometry layers first field_layers.insert(0, 'geometry') field_types.insert(0, 'LOCATION') field_layers.insert(1, 'geometry_vertex_count') field_types.insert(1, 'NUMBER') if upload.create_simplify: field_layers.insert(0, 'geometry_simplified') field_types.insert(0, 'LOCATION') field_layers.insert(1, 'geometry_simplified_vertex_count') field_types.insert(1, 'NUMBER') #use sorted dict so we can ensure table has geom columns upfront field_dict = SortedDict(zip(field_layers, field_types)) #set up extra fields if creating int/style cols if create_int_style_cols: int_style_dict = {} for field, field_type in field_dict.items(): if field_type == 'STRING': field_dict[field + '_ft_style'] = 'NUMBER' int_style_dict[field] = {} print field_dict #add some custom import fields field_dict['import_notes'] = 'STRING' print 'FIELD DICT', field_dict print 'starting to process' for i, feat in enumerate(layer): if i > max_rows: continue if start_row and i < start_row: continue upload.rows_processed = i + 1 if not i % ((num_features / 50) or 5): print upload.rows_processed, 'rp' upload.save() upload.save() rd = {} #geom = fromstr(feat.geom.wkt,srid=srid) if layer.srs: try: geom = OGRGeometry(feat.geom.wkt, layer.srs.proj4) geom.transform(4326) except Exception, e: print 'FAIL GEOM' print e, geom = None else: geom = OGRGeometry(feat.geom.wkt) if geom: geom = fromstr(geom.wkt) #create optional centroid for polys if upload.create_centroid and 'oly' in geom.geom_type: field_dict['geometry_pos'] = 'LOCATION' rd['geometry_pos'] = geom.point_on_surface.kml if upload.create_centroid_poly and 'oly' in geom.geom_type: field_dict['geometry_pos_poly_2'] = 'LOCATION' field_dict['geometry_pos_poly_3'] = 'LOCATION' rd['geometry_pos_poly_2'] = geom.point_on_surface.buffer( .0001, 10).kml rd['geometry_pos_poly_3'] = geom.point_on_surface.buffer( .0005, 10).kml #if it's > 1M characters, we need to simplify it for FT simplify_tolerance = .0001 while len(geom.kml) > 1000000: geom = geom.simplify(simplify_tolerance) print 'simplified to %f' % simplify_tolerance rd['import_notes'] = 'simplified to %d DD' % simplify_tolerance simplify_tolerance = simplify_tolerance * 1.5 if not geom.valid: rd['import_notes'] = '<br>Geometry not valid' kml = geom.kml rd['geometry'] = kml rd['geometry_vertex_count'] = geom.num_coords if upload.create_simplify and not 'oint' in geom.geom_type: amt = .002 if 'oly' in geom.geom_type: buffer_geom = geom.buffer(amt) buffer_geom = buffer_geom.buffer(amt * -1) simple_geom = buffer_geom.simplify(amt) else: simple_geom = geom.simplify(amt) rd['geometry_simplified'] = simple_geom.kml rd['geometry_simplified_vertex_count'] = simple_geom.num_coords for f in fields: val = feat.get(f) #make sure we have proper null type for diff fields if val == '<Null>': continue if not val: continue if field_dict[f] == 'DATETIME': val = val.isoformat().split('T')[0] if field_dict[f] == 'STRING' \ and create_int_style_cols \ and field_dict.has_key(f + '_ft_style'): #check to see if we have a number for this yet try: rd[f + '_ft_style'] = int_style_dict[f][val] except: int_style_dict[f][val] = len(int_style_dict[f]) rd[f + '_ft_style'] = int_style_dict[f][val] #however if we have too many distinct vals, let's just not do this anymore if len(int_style_dict[f]) > max_distinct_style_vals: print 'DELETING FD %s' % f del field_dict[f + '_ft_style'] del rd[f + '_ft_style'] #sucks, but now we should just remove all these fields from previous rows for srow in rows: try: del srow[f + '_ft_style'] except: pass #probably this was a null value? rd[f] = val rows.append(rd) #let's process 10k rows at a time.. not keep everything in memory if len(rows) > 10000: uploadRows(upload, field_dict, rows) rows = []
class FormWizard(object): """ The basic FormWizard. This class needs a storage backend when creating an instance. """ def __init__(self, storage, form_list, initial_list={}, instance_list={}, condition_list={}): """ Creates a form wizard instance. `storage` is the storage backend, the place where step data and current state of the form gets saved. `form_list` is a list of forms. The list entries can be form classes of tuples of (`step_name`, `form_class`). `initial_list` contains a dictionary of initial data dictionaries. The key should be equal to the `step_name` in the `form_list`. `instance_list` contains a dictionary of instance objects. This list is only used when `ModelForms` are used. The key should be equal to the `step_name` in the `form_list`. """ self.form_list = SortedDict() self.storage_name = storage assert len(form_list) > 0, 'at least one form is needed' for i in range(len(form_list)): form = form_list[i] if isinstance(form, tuple): self.form_list[unicode(form[0])] = form[1] else: self.form_list[unicode(i)] = form for form in self.form_list.values(): if issubclass(form, formsets.BaseFormSet): form = form.form if [True for f in form.base_fields.values() if issubclass(f.__class__, forms.FileField)] and \ not hasattr(self, 'file_storage'): raise NoFileStorageException self.initial_list = initial_list self.instance_list = instance_list self.condition_list = condition_list def get_form_list(self, request, storage): form_list = SortedDict() for form_key, form_class in self.form_list.items(): condition = self.condition_list.get(form_key, True) if callable(condition): condition = condition(self, request, storage) if condition: form_list[form_key] = form_class return form_list def __repr__(self): return '%s: form_list: %s, initial_list: %s' % ( self.get_wizard_name(), self.form_list, self.initial_list) def __call__(self, request, *args, **kwargs): """ This method gets called by the routing engine. The first argument is `request` which contains a `HttpRequest` instance. The request is stored in `request` for later use. After processing the request using the `process_request` method, the response gets updated by the storage engine (for example add cookies). """ storage = get_storage(self.storage_name, self.get_wizard_name(), request, getattr(self, 'file_storage', None)) response = self.process_request(request, storage, *args, **kwargs) storage.update_response(response) if kwargs.get('testmode', False): return response, storage else: return response def process_request(self, request, *args, **kwargs): """ Returns a response generated by either `process_get_request` or `process_post_request` (depends on `request.method`). """ if request.method == 'GET': return self.process_get_request(request, *args, **kwargs) else: return self.process_post_request(request, *args, **kwargs) def process_get_request(self, request, storage, *args, **kwargs): """ If the wizard gets a GET request, it assumes that the user just starts at the first step or wants to restart the process. The wizard will be resetted before rendering the first step. """ self.reset_wizard(request, storage) if 'extra_context' in kwargs: self.update_extra_context(request, storage, kwargs['extra_context']) storage.set_current_step(self.get_first_step(request, storage)) return self.render(request, storage, self.get_form(request, storage)) def process_post_request(self, request, storage, *args, **kwargs): """ Generates a HttpResponse which contains either the current step (if form validation wasn't successful), the next step (if the current step was stored successful) or the done view (if no more steps are available) """ if 'extra_context' in kwargs: self.update_extra_context(request, storage, kwargs['extra_context']) if request.POST.has_key('form_prev_step') and \ self.get_form_list(request, storage).has_key( request.POST['form_prev_step']): storage.set_current_step(request.POST['form_prev_step']) form = self.get_form( request, storage, data=storage.get_step_data( self.determine_step(request, storage)), files=storage.get_step_files( self.determine_step(request, storage)), ) else: # Check if form was refreshed current_step = self.determine_step(request, storage) prev_step = self.get_prev_step(request, storage, step=current_step) for value in request.POST: if prev_step and not value.startswith( current_step) and value.startswith(prev_step): # form refreshed, change current step storage.set_current_step(prev_step) break form = self.get_form(request, storage, data=request.POST, files=request.FILES) if form.is_valid(): storage.set_step_data( self.determine_step(request, storage), self.process_step(request, storage, form)) storage.set_step_files( self.determine_step(request, storage), self.process_step_files(request, storage, form)) current_step = self.determine_step(request, storage) last_step = self.get_last_step(request, storage) if current_step == last_step: return self.render_done(request, storage, form, **kwargs) else: return self.render_next_step(request, storage, form) return self.render(request, storage, form) def render_next_step(self, request, storage, form, **kwargs): """ Gets called when the next step/form should be rendered. `form` contains the last/current form. """ next_step = self.get_next_step(request, storage) new_form = self.get_form(request, storage, next_step, data=storage.get_step_data(next_step), files=storage.get_step_files(next_step)) storage.set_current_step(next_step) return self.render(request, storage, new_form, **kwargs) def render_done(self, request, storage, form, **kwargs): """ Gets called when all forms passed. The method should also re-validate all steps to prevent manipulation. If any form don't validate, `render_revalidation_failure` should get called. If everything is fine call `done`. """ final_form_list = [] for form_key in self.get_form_list(request, storage).keys(): form_obj = self.get_form(request, storage, step=form_key, data=storage.get_step_data(form_key), files=storage.get_step_files(form_key)) if not form_obj.is_valid(): return self.render_revalidation_failure( request, storage, form_key, form_obj, **kwargs) final_form_list.append(form_obj) done_response = self.done(request, storage, final_form_list, **kwargs) self.reset_wizard(request, storage) return done_response def get_form_prefix(self, request, storage, step=None, form=None): """ Returns the prefix which will be used when calling the actual form for the given step. `step` contains the step-name, `form` the form which will be called with the returned prefix. If no step is given, the form_prefix will determine the current step automatically. """ if step is None: step = self.determine_step(request, storage) return str(step) def get_form_initial(self, request, storage, step): """ Returns a dictionary which will be passed to the form for `step` as `initial`. If no initial data was provied while initializing the form wizard, a empty dictionary will be returned. """ return self.initial_list.get(step, {}) def get_form_instance(self, request, storage, step): """ Returns a object which will be passed to the form for `step` as `instance`. If no instance object was provied while initializing the form wizard, None be returned. """ return self.instance_list.get(step, None) def get_form(self, request, storage, step=None, data=None, files=None): """ Constructs the form for a given `step`. If no `step` is defined, the current step will be determined automatically. The form will be initialized using the `data` argument to prefill the new form. """ if step is None: step = self.determine_step(request, storage) kwargs = { 'data': data, 'files': files, 'prefix': self.get_form_prefix(request, storage, step, self.form_list[step]), 'initial': self.get_form_initial(request, storage, step), } if issubclass(self.form_list[step], forms.ModelForm): kwargs.update( {'instance': self.get_form_instance(request, storage, step)}) elif issubclass(self.form_list[step], forms.models.BaseModelFormSet): kwargs.update( {'queryset': self.get_form_instance(request, storage, step)}) return self.form_list[step](**kwargs) def process_step(self, request, storage, form): """ This method is used to postprocess the form data. For example, this could be used to conditionally skip steps if a special field is checked. By default, it returns the raw `form.data` dictionary. """ return self.get_form_step_data(request, storage, form) def process_step_files(self, request, storage, form): return self.get_form_step_files(request, storage, form) def render_revalidation_failure(self, request, storage, step, form, **kwargs): """ Gets called when a form doesn't validate before rendering the done view. By default, it resets the current step to the first failing form and renders the form. """ storage.set_current_step(step) return self.render(request, storage, form, **kwargs) def get_form_step_data(self, request, storage, form): """ Is used to return the raw form data. You may use this method to manipulate the data. """ return form.data def get_form_step_files(self, request, storage, form): """ Is used to return the raw form files. You may use this method to manipulate the data. """ return form.files def get_all_cleaned_data(self, request, storage): """ Returns a merged dictionary of all step' cleaned_data dictionaries. If a step contains a `FormSet`, the key will be prefixed with formset and contain a list of the formset' cleaned_data dictionaries. """ cleaned_dict = {} for form_key in self.get_form_list(request, storage).keys(): form_obj = self.get_form(request, storage, step=form_key, data=storage.get_step_data(form_key), files=storage.get_step_files(form_key)) if form_obj.is_valid(): if isinstance(form_obj.cleaned_data, list): cleaned_dict.update( {'formset-%s' % form_key: form_obj.cleaned_data}) else: cleaned_dict.update(form_obj.cleaned_data) return cleaned_dict def get_cleaned_data_for_step(self, request, storage, step): """ Returns the cleaned data for a given `step`. Before returning the cleaned data, the stored values are being revalidated through the form. If the data doesn't validate, None will be returned. """ if self.form_list.has_key(step): form_obj = self.get_form(request, storage, step=step, data=storage.get_step_data(step), files=storage.get_step_files(step)) if form_obj.is_valid(): return form_obj.cleaned_data return None def determine_step(self, request, storage): """ Returns the current step. If no current step is stored in the storage backend, the first step will be returned. """ return storage.get_current_step() or \ self.get_first_step(request, storage) def get_first_step(self, request, storage): """ Returns the name of the first step. """ return self.get_form_list(request, storage).keys()[0] def get_last_step(self, request, storage): """ Returns the name of the last step. """ return self.get_form_list(request, storage).keys()[-1] def get_next_step(self, request, storage, step=None): """ Returns the next step after the given `step`. If no more steps are available, None will be returned. If the `step` argument is None, the current step will be determined automatically. """ form_list = self.get_form_list(request, storage) if step is None: step = self.determine_step(request, storage) key = form_list.keyOrder.index(step) + 1 if len(form_list.keyOrder) > key: return form_list.keyOrder[key] else: return None def get_prev_step(self, request, storage, step=None): """ Returns the previous step before the given `step`. If there are no steps available, None will be returned. If the `step` argument is None, the current step will be determined automatically. """ form_list = self.get_form_list(request, storage) if step is None: step = self.determine_step(request, storage) key = form_list.keyOrder.index(step) - 1 if key < 0: return None else: return form_list.keyOrder[key] def get_step_index(self, request, storage, step=None): """ Returns the index for the given `step` name. If no step is given, the current step will be used to get the index. """ if step is None: step = self.determine_step(request, storage) return self.get_form_list(request, storage).keyOrder.index(step) def get_num_steps(self, request, storage): """ Returns the total number of steps/forms in this the wizard. """ return len(self.get_form_list(request, storage)) def get_wizard_name(self): """ Returns the name of the wizard. By default the class name is used. This name will be used in storage backends to prevent from colliding with other form wizards. """ return self.__class__.__name__ def reset_wizard(self, request, storage): """ Resets the user-state of the wizard. """ storage.reset() def get_template(self, request, storage): """ Returns the templates to be used for rendering the wizard steps. This method can return a list of templates or a single string. """ return 'formwizard/wizard.html' def get_template_context(self, request, storage, form): """ Returns the template context for a step. You can overwrite this method to add more data for all or some steps. Example: class MyWizard(FormWizard): def get_template_context(self, request, storage, form): context = super(MyWizard, self).get_template_context( request, storage, form) if storage.get_current_step() == 'my_step_name': context.update({'another_var': True}) return context """ return { 'extra_context': self.get_extra_context(request, storage), 'form_step': self.determine_step(request, storage), 'form_first_step': self.get_first_step(request, storage), 'form_last_step': self.get_last_step(request, storage), 'form_prev_step': self.get_prev_step(request, storage), 'form_next_step': self.get_next_step(request, storage), 'form_step0': int(self.get_step_index(request, storage)), 'form_step1': int(self.get_step_index(request, storage)) + 1, 'form_step_count': self.get_num_steps(request, storage), 'form': form, } def get_extra_context(self, request, storage): """ Returns the extra data currently stored in the storage backend. """ return storage.get_extra_context_data() def update_extra_context(self, request, storage, new_context): """ Updates the currently stored extra context data. Already stored extra context will be kept! """ context = self.get_extra_context(request, storage) context.update(new_context) return storage.set_extra_context_data(context) def render(self, request, storage, form, **kwargs): """ Renders the acutal `form`. This method can be used to pre-process data or conditionally skip steps. """ return self.render_template(request, storage, form) def render_template(self, request, storage, form=None): """ Returns a `HttpResponse` containing the rendered form step. Available template context variables are: * `extra_context` - current extra context data * `form_step` - name of the current step * `form_first_step` - name of the first step * `form_last_step` - name of the last step * `form_prev_step`- name of the previous step * `form_next_step` - name of the next step * `form_step0` - index of the current step * `form_step1` - index of the current step as a 1-index * `form_step_count` - total number of steps * `form` - form instance of the current step """ form = form or self.get_form(request, storage) return render_to_response(self.get_template(request, storage), self.get_template_context( request, storage, form), context_instance=RequestContext(request)) def done(self, request, storage, form_list, **kwargs): """ This method muss be overrided by a subclass to process to form data after processing all steps. """ raise NotImplementedError("Your %s class has not defined a done() \ method, which is required." % self.__class__.__name__)
def index(request, highlight='-1'): latest_strikes = Strike.objects.filter(end_date__gte=datetime.today().date()).order_by('start_date').exclude(approved=False)[:30] companies = Company.objects.all() regions = Region.objects.all() strikes = SortedDict() todaysDay = datetime.today().strftime("%d") todayMonth = datetime.today().strftime("%m") tomorrow = datetime.today().date() + timedelta(days=1) tomorrowsDay = tomorrow.strftime("%d") for strike in latest_strikes: sd = strike.start_date y = strike.start_date.strftime("%Y") m = strike.start_date.strftime("%m") # if m < todayMonth: # m = todayMonth d = strike.start_date.strftime("%d") if strike.start_date < datetime.today(): sd = datetime.today() d = todaysDay if not strikes.has_key(y): strikes[y] = SortedDict() if not strikes[y].has_key(m): mName = calendar.month_name[int(m)] if len(mName) >= 7: #shrink months that don't fit mName = mName[0:3]+"." strikes[y][m] = {"name":mName, "days":SortedDict()} if not strikes[y][m]["days"].has_key(d): strikes[y][m]["days"][d] = {'strikes':{}, "date":sd.strftime("%A, %e de %B de %Y")} if not strikes[y][m]["days"][d]['strikes'].has_key(strike.company): strikes[y][m]["days"][d]['strikes'][strike.company] = [] strikes[y][m]["days"][d]['strikes'][strike.company].append(strike) y = datetime.today().strftime("%Y") m = datetime.today().strftime("%m") if len(strikes) > 0 and (strikes.has_key(y) and len(strikes[y]) >0 and strikes[y].has_key(m)): fix = False if strikes[y][m]["days"].has_key(tomorrowsDay): strikes[y][m]["days"][tomorrowsDay]["alias"] = "Amanhã" fix = True if strikes[y][m]["days"].has_key(todaysDay): strikes[y][m]["days"][todaysDay]["alias"] = "Hoje" if fix: strikes[y][m]["days"][todaysDay]["fix"] = "fixTomorrow" for c in strikes[y][m]["days"][todaysDay]["strikes"]: cc = strikes[y][m]["days"][todaysDay]["strikes"][c] if len(cc) > 1: strikes[y][m]["days"][todaysDay]["strikes"][c] = sorted(cc, key=MYattrgetter('start_date.day'), reverse=True) #strikes['04']["days"] = sorted(strikes['04']["days"]) context = { 'strikes': strikes, 'regions': regions, 'host': request.get_host(), 'companies': companies, 'highlights': [int(highlight)] } return render_to_response('index.html', context, context_instance=RequestContext(request))
class FormWizard(object): """ The basic FormWizard. This class needs a storage backend when creating an instance. """ def __init__(self, storage, form_list, initial_list={}, instance_list={}): """ Creates a form wizard instance. `storage` is the storage backend, the place where step data and current state of the form gets saved. `form_list` is a list of forms. The list entries can be form classes of tuples of (`step_name`, `form_class`). `initial_list` contains a dictionary of initial data dictionaries. The key should be equal to the `step_name` in the `form_list`. `instance_list` contains a dictionary of instance objects. This list is only used when `ModelForms` are used. The key should be equal to the `step_name` in the `form_list`. """ self.form_list = SortedDict() self.storage_name = storage assert len(form_list) > 0, 'at least one form is needed' for i in range(len(form_list)): form = form_list[i] if isinstance(form, tuple): self.form_list[unicode(form[0])] = form[1] else: self.form_list[unicode(i)] = form self.initial_list = initial_list self.instance_list = instance_list def __repr__(self): return 'step: %s, form_list: %s, initial_list: %s' % ( self.determine_step(), self.form_list, self.initial_list) def __call__(self, request, *args, **kwargs): """ This method gets called by the routing engine. The first argument is `request` which contains a `HttpRequest` instance. The request is stored in `self.request` for later use. After processing the request using the `process_request` method, the response gets updated by the storage engine (for example add cookies). """ self.request = request self.storage = get_storage(self.storage_name, self.get_wizard_name(), self.request) response = self.process_request(*args, **kwargs) response = self.storage.update_response(response) return response def process_request(self, *args, **kwargs): """ Returns a response generated by either `process_get_request` or `process_post_request` (depends on `request.method`). """ if self.request.method == 'GET': return self.process_get_request(*args, **kwargs) else: return self.process_post_request(*args, **kwargs) def process_get_request(self, *args, **kwargs): """ If the wizard gets a GET request, it assumes that the user just starts at the first step or wants to restart the process. The wizard will be resetted before rendering the first step. """ self.reset_wizard() if 'extra_context' in kwargs: self.update_extra_context(kwargs['extra_context']) self.storage.set_current_step(self.get_first_step()) return self.render(self.get_form()) def process_post_request(self, *args, **kwargs): """ Generates a HttpResponse which contains either the current step (if form validation wasn't successful), the next step (if the current step was stored successful) or the done view (if no more steps are available) """ if 'extra_context' in kwargs: self.update_extra_context(kwargs['extra_context']) if self.request.POST.has_key('form_prev_step') and self.form_list.has_key(self.request.POST['form_prev_step']): self.storage.set_current_step(self.request.POST['form_prev_step']) form = self.get_form(data=self.storage.get_step_data(self.determine_step())) else: form = self.get_form(data=self.request.POST) if form.is_valid(): self.storage.set_step_data(self.determine_step(), self.process_step(form)) if self.determine_step() == self.get_last_step(): return self.render_done(form, *args, **kwargs) else: return self.render_next_step(form, *args, **kwargs) return self.render(form) def render_next_step(self, form, *args, **kwargs): """ Gets called when the next step/form should be rendered. `form` contains the last/current form. """ next_step = self.get_next_step() new_form = self.get_form(next_step, data=self.storage.get_step_data(next_step)) self.storage.set_current_step(next_step) return self.render(new_form) def render_done(self, form, *args, **kwargs): """ Gets called when all forms passed. The method should also re-validate all steps to prevent manipulation. If any form don't validate, `render_revalidation_failure` should get called. If everything is fine call `done`. """ final_form_list = [] for form_key in self.form_list.keys(): form_obj = self.get_form(step=form_key, data=self.storage.get_step_data(form_key)) if not form_obj.is_valid(): return self.render_revalidation_failure(form_key, form_obj) final_form_list.append(form_obj) return self.done(self.request, final_form_list) def get_form_prefix(self, step=None, form=None): """ Returns the prefix which will be used when calling the actual form for the given step. `step` contains the step-name, `form` the form which will be called with the returned prefix. If no step is given, the form_prefix will determine the current step automatically. """ if step is None: step = self.determine_step() return str(step) def get_form_initial(self, step): """ Returns a dictionary which will be passed to the form for `step` as `initial`. If no initial data was provied while initializing the form wizard, a empty dictionary will be returned. """ return self.initial_list.get(step, {}) def get_form_instance(self, step): """ Returns a object which will be passed to the form for `step` as `instance`. If no instance object was provied while initializing the form wizard, None be returned. """ return self.instance_list.get(step, None) def get_form(self, step=None, data=None): """ Constructs the form for a given `step`. If no `step` is defined, the current step will be determined automatically. The form will be initialized using the `data` argument to prefill the new form. """ if step is None: step = self.determine_step() kwargs = { 'data': data, 'prefix': self.get_form_prefix(step, self.form_list[step]), 'initial': self.get_form_initial(step), } if issubclass(self.form_list[step], forms.ModelForm): kwargs.update({'instance': self.get_form_instance(step)}) return self.form_list[step](**kwargs) def process_step(self, form): """ This method is used to postprocess the form data. For example, this could be used to conditionally skip steps if a special field is checked. By default, it returns the raw `form.data` dictionary. """ return self.get_form_step_data(form) def render_revalidation_failure(self, step, form): """ Gets called when a form doesn't validate before rendering the done view. By default, it resets the current step to the first failing form and renders the form. """ self.storage.set_current_step(step) return self.render(form) def get_form_step_data(self, form): """ Is used to return the raw form data. You may use this method to manipulate the data. """ return form.data def get_all_cleaned_data(self): """ Returns a merged dictionary of all step' cleaned_data dictionaries. If a step contains a `FormSet`, the key will be prefixed with formset and contain a list of the formset' cleaned_data dictionaries. """ cleaned_dict = {} for form_key in self.form_list.keys(): form_obj = self.get_form(step=form_key, data=self.storage.get_step_data(form_key)) if form_obj.is_valid(): if isinstance(form_obj.cleaned_data, list): cleaned_dict.update({'formset-%s' % form_key: form_obj.cleaned_data}) else: cleaned_dict.update(form_obj.cleaned_data) return cleaned_dict def get_cleaned_data_for_step(self, step): """ Returns the cleaned data for a given `step`. Before returning the cleaned data, the stored values are being revalidated through the form. If the data doesn't validate, None will be returned. """ if self.form_list.has_key(step): form_obj = self.get_form(step=step, data=self.storage.get_step_data(step)) if form_obj.is_valid(): return form_obj.cleaned_data return None def determine_step(self): """ Returns the current step. If no current step is stored in the storage backend, the first step will be returned. """ return self.storage.get_current_step() or self.get_first_step() def get_first_step(self): """ Returns the name of the first step. """ return self.form_list.keys()[0] def get_last_step(self): """ Returns the name of the last step. """ return self.form_list.keys()[-1] def get_next_step(self, step=None): """ Returns the next step after the given `step`. If no more steps are available, None will be returned. If the `step` argument is None, the current step will be determined automatically. """ if step is None: step = self.determine_step() key = self.form_list.keyOrder.index(step) + 1 if len(self.form_list.keyOrder) > key: return self.form_list.keyOrder[key] else: return None def get_prev_step(self, step=None): """ Returns the previous step before the given `step`. If there are no steps available, None will be returned. If the `step` argument is None, the current step will be determined automatically. """ if step is None: step = self.determine_step() key = self.form_list.keyOrder.index(step) - 1 if key < 0: return None else: return self.form_list.keyOrder[key] def get_step_index(self, step=None): """ Returns the index for the given `step` name. If no step is given, the current step will be used to get the index. """ if step is None: step = self.determine_step() return self.form_list.keyOrder.index(step) @property def num_steps(self): """ Returns the total number of steps/forms in this the wizard. """ return len(self.form_list) def get_wizard_name(self): """ Returns the name of the wizard. By default the class name is used. This name will be used in storage backends to prevent from colliding with other form wizards. """ return self.__class__.__name__ def reset_wizard(self): """ Resets the user-state of the wizard. """ self.storage.reset() def get_template(self): """ Returns the templates to be used for rendering the wizard steps. This method can return a list of templates or a single string. """ return 'formwizard/wizard.html' def get_extra_context(self): """ Returns the extra data currently stored in the storage backend. """ return self.storage.get_extra_context_data() def update_extra_context(self, new_context): """ Updates the currently stored extra context data. Already stored extra context will be kept! """ context = self.get_extra_context() context.update(new_context) return self.storage.set_extra_context_data(context) def render(self, form): """ Renders the acutal `form`. This method can be used to pre-process data or conditionally skip steps. """ return self.render_template(form) def render_template(self, form=None): """ Returns a `HttpResponse` containing the rendered form step. Available template context variables are: * `extra_context` - current extra context data * `form_step` - name of the current step * `form_first_step` - name of the first step * `form_last_step` - name of the last step * `form_prev_step`- name of the previous step * `form_next_step` - name of the next step * `form_step0` - index of the current step * `form_step1` - index of the current step as a 1-index * `form_step_count` - total number of steps * `form` - form instance of the current step """ form = form or self.get_form() return render_to_response(self.get_template(), { 'extra_context': self.get_extra_context(), 'form_step': self.determine_step(), 'form_first_step': self.get_first_step(), 'form_last_step': self.get_last_step(), 'form_prev_step': self.get_prev_step(), 'form_next_step': self.get_next_step(), 'form_step0': int(self.get_step_index()), 'form_step1': int(self.get_step_index()) + 1, 'form_step_count': self.num_steps, 'form': form, }, context_instance=RequestContext(self.request)) def done(self, request, form_list): """ This method muss be overrided by a subclass to process to form data after processing all steps. """ raise NotImplementedError("Your %s class has not defined a done() method, which is required." % self.__class__.__name__)
def get_data(self, id): supporter = Supporter.objects.get(id = id) data = SortedDict() data['id'] = supporter.id data['first_name'] = supporter.first_name data['middle_name'] = supporter.middle_name data['last_name'] = supporter.last_name data['gender'] = supporter.gender data['gender_label'] = supporter.get_gender() data['birthdate'] = supporter.birthdate data['age'] = supporter.get_age() data['bio'] = supporter.bio cp = supporter.contact_profile if cp: if cp.primary_phone: data['phones'] = [] phone = SortedDict() phone['number'] = cp.primary_phone.number phone['type'] = cp.primary_phone.type.title phone['active'] = cp.primary_phone.active phone['sms_ok'] = cp.primary_phone.sms_ok phone['do_not_call'] = cp.primary_phone.do_not_call phone['primary'] = 1 data['phones'].append(phone) other_phones = cp.other_phones.all() if other_phones: if not data.has_key('phones'): data['phones'] = [] for other_phone in other_phones: phone = SortedDict() phone['number'] = other_phone.number phone['type'] = other_phone.type.title phone['active'] = other_phone.active phone['sms_ok'] = other_phone.sms_ok phone['do_not_call'] = other_phone.do_not_call phone['primary'] = 0 data['phones'].append(phone) if cp.primary_email: data['emails'] = [] email = SortedDict() email['email'] = cp.primary_email.email email['type'] = cp.primary_email.type.title email['active'] = cp.primary_email.active email['do_not_send'] = cp.primary_email.do_not_send email['primary'] = 1 data['emails'].append(email) other_emails = cp.other_emails.all() if other_emails: if not data.has_key('emails'): data['emails'] = [] for other_email in other_emails: email = SortedDict() email['email'] = other_email.email email['type'] = other_email.type.title email['active'] = other_email.active email['do_not_send'] = other_email.do_not_send email['primary'] = 0 data['emails'].append(email) if cp.primary_address: data['addresses'] = [] address = SortedDict() address['address1'] = cp.primary_address.address1 address['address2'] = cp.primary_address.address2 address['address3'] = cp.primary_address.address3 address['city'] = cp.primary_address.city address['state'] = cp.primary_address.state address['zip'] = cp.primary_address.zip address['type'] = cp.primary_address.address_type.title address['primary'] = 1 data['addresses'].append(address) other_addresses = cp.other_addresses.all() if other_addresses: if not data.has_key('addresses'): data['addresses'] = [] for address in other_addresses: address = SortedDict() address['address1'] = address.address1 address['address2'] = address.address2 address['address3'] = address.address3 address['city'] = address.city address['state'] = address.state address['zip'] = address.zip address['type'] = address.address_type.title address['primary'] = 0 data['addresses'].append(address) if cp.primary_language: data['languages'] = [] language = SortedDict() language['title'] = cp.primary_language.title language['primary'] = 1 data['languages'].append(language) other_languages = cp.other_languages.all() if other_languages: if not data.has_key('languages'): data['languages'] = [] for language_obj in languages: language = SortedDict() language['title'] = language_obj.title language['primary'] = 0 data['languages'].append(language) organizations = cp.organizations.all() if organizations: data['organizations'] = [] for organization_obj in organizations: organization = SortedDict() organization['id'] = organization_obj.id organization['name'] = organization_obj.name organization['type'] = organization_obj.type.title data['organizations'].append(organization) websites = cp.websites.all() if websites: data['websites'] = [] for website_obj in websites: website = SortedDict() website['title'] = website_obj.title website['url'] = website_obj.url website['active'] = website_obj.active website['type'] = website_obj.type.title data['websites'].append(website) feeds = cp.feeds.all() if feeds: data['feeds'] = [] for feed_obj in feeds: feed = SortedDict() feed['title'] = feed_obj.title feed['url'] = feed_obj.url feed['active'] = feed_obj.active feed['type'] = feed_obj.type.title data['feeds'].append(feed) return data
def detail_pkg(request, serial, manifest_name): machine = None manifest = None install_items = None if serial: try: machine = Machine.objects.get(serial_number=serial) except Machine.DoesNotExist: raise Http404 else: raise Http404 report_plist = {} if machine: try: report = MunkiReport.objects.get(machine=machine) report_plist = report.get_report() except MunkiReport.DoesNotExist: pass # get autocomplete data install_items = Manifest.getInstallItemNames(manifest_name) valid_install_items = install_items["suggested"] + install_items["updates"] + install_items["with_version"] suggested_install_items = install_items["suggested"] valid_catalogs = Catalog.list() valid_manifest_names = Manifest.list() autocomplete_data = json.dumps( {"items": install_items["suggested"], "catalogs": valid_catalogs, "manifests": valid_manifest_names} ) # loop trought includet manifests as long as it have one includetManifests = dict() def get_addition_manifests(manifest): detailManifest = Manifest.read(manifest) includetManifests.update({manifest: detailManifest}) if "included_manifests" in detailManifest: for includetManifest in detailManifest.included_manifests: get_addition_manifests(includetManifest) manifest = Manifest.read(manifest_name) sorted_Manifests = SortedDict() sorted_Manifests[manifest_name] = manifest if "included_manifests" in manifest: for includetManifest in manifest.included_manifests: get_addition_manifests(includetManifest) sort_list = manifest.included_manifests for key in sort_list: sorted_Manifests[key] = includetManifests[key] key_list = includetManifests.keys() key_list.sort() for key in key_list: if key not in sorted_Manifests: sorted_Manifests[key] = includetManifests[key] # item_details -> list with software and details # true_items for check if a software is in catalog or not item_details = {} true_items = list() if "catalogs" in manifest: for catalog in manifest.catalogs: catalog_detail = Catalog.detail(catalog) if catalog_detail: for detail in reversed(catalog_detail): if not detail.name in item_details: item_details[detail.name] = detail true_items.append(detail.name) if "icon_name" in item_details[detail.name]: icon = Catalog.get_icon(item_details[detail.name].icon_name) else: icon = Catalog.get_icon(detail.name) item_details[detail.name].icon_name = icon ManagedInstallsDetail = SortedDict() if report_plist.has_key("ManagedInstalls"): for item in report_plist.ManagedInstalls: ManagedInstallsDetail[item.name] = item # installs installs = SortedDict() listed = list() installsTypes = ["managed_installs", "managed_uninstalls", "optional_installs"] for installsType in installsTypes: installs[installsType] = SortedDict() for number, manifests in enumerate(sorted_Manifests): installs[installsType][manifests] = SortedDict() if manifest: for index, item in enumerate(sorted_Manifests[manifests][installsType]): listed.append(item) if ManagedInstallsDetail.has_key(item): installs[installsType][manifests][item] = ManagedInstallsDetail[item] if item in true_items: if installs[installsType].get(manifests, {}).has_key(item): installs[installsType][manifests][item].update(item_details[item]) else: installs[installsType][manifests][item] = item_details[item] installs[installsType][manifests][item].update({"incatalog": "True"}) else: if installs[installsType].get(manifests, {}).has_key(item): installs[installsType][manifests][item].update({"incatalog": "False"}) else: installs[installsType][manifests][item] = {"name": item, "incatalog": "False"} required = SortedDict() for item in sorted(ManagedInstallsDetail.items(), key=lambda x: x[1]["display_name"]): if not item[0] in listed: if item_details.has_key(item[0]): ManagedInstallsDetail[item[0]].icon_name = item_details[item[0]].icon_name else: ManagedInstallsDetail[item[0]].icon_name = "/static/img/PackageIcon.png" required[item[0]] = ManagedInstallsDetail[item[0]] # handle items that were installed during the most recent run install_results = {} for result in report_plist.get("InstallResults", []): nameAndVers = result["name"] + "-" + result["version"] if result["status"] == 0: install_results[nameAndVers] = "installed" else: install_results[nameAndVers] = "error" if install_results: for item in report_plist.get("ItemsToInstall", []): name = item.get("display_name", item["name"]) nameAndVers = "%s-%s" % (name, item["version_to_install"]) item["install_result"] = install_results.get(nameAndVers, "pending") for item in report_plist.get("ManagedInstalls", []): if "version_to_install" in item: name = item.get("display_name", item["name"]) nameAndVers = "%s-%s" % (name, item["version_to_install"]) if install_results.get(nameAndVers) == "installed": item["installed"] = True # handle items that were removed during the most recent run # this is crappy. We should fix it in Munki. removal_results = {} # for result in report_plist.get('RemovalResults', []): # m = re.search('^Removal of (.+): (.+)$', result) # if m: # try: # if m.group(2) == 'SUCCESSFUL': # removal_results[m.group(1)] = 'removed' # else: # removal_results[m.group(1)] = m.group(2) # except IndexError: # pass if removal_results: for item in report_plist.get("ItemsToRemove", []): name = item.get("display_name", item["name"]) item["install_result"] = removal_results.get(name, "pending") if item["install_result"] == "removed": if not "RemovedItems" in report_plist: report_plist["RemovedItems"] = [item["name"]] elif not name in report_plist["RemovedItems"]: report_plist["RemovedItems"].append(item["name"]) c = RequestContext( request, { "manifest_name": manifest_name, "manifest": manifest, "report": report_plist, "installs": installs, "autocomplete_data": autocomplete_data, "required": required, "valid_manifest_names": valid_manifest_names, "valid_catalogs": valid_catalogs, }, ) c.update(csrf(request)) return render_to_response("reports/detail_pkg.html", c)
def import_from_shape(upload, start_row=0, max_rows=200000, create_int_style_cols=True): """ a shapeUpload object max_rows - any more than this is ignored centroid - if it's a (multi)polygon, should we also create a geometry_centroid field """ upload.status = 2 #set this right away so it doesn't get reprocessed upload.save() ds = DataSource(upload.shapefile) layer = ds[0] fields = layer.fields num_features = len(layer) #set max # of _style features max_distinct_style_vals = max(min(num_features / 100, 50),10) print 'there are %d features' % num_features upload.total_rows = num_features if not num_features: print 'no rows, returning' upload.status = 6 upload.save() return rows = [] #get field types field_map = { 'OFTString':'STRING', 'OFTReal':'NUMBER', 'OFTInteger':'NUMBER', 'OFTDate':'DATETIME' } field_types = [field_map[f.__name__] for f in layer.field_types] field_layers = layer.fields #insert geometry layers first field_layers.insert(0,'geometry') field_types.insert(0,'LOCATION') field_layers.insert(1,'geometry_vertex_count') field_types.insert(1,'NUMBER') if upload.create_simplify: field_layers.insert(0,'geometry_simplified') field_types.insert(0,'LOCATION') field_layers.insert(1,'geometry_simplified_vertex_count') field_types.insert(1,'NUMBER') #use sorted dict so we can ensure table has geom columns upfront field_dict = SortedDict(zip(field_layers, field_types)) #set up extra fields if creating int/style cols if create_int_style_cols: int_style_dict = {} for field,field_type in field_dict.items(): if field_type == 'STRING': field_dict[field + '_ft_style'] = 'NUMBER' int_style_dict[field] = {} print field_dict #add some custom import fields field_dict['import_notes'] = 'STRING' print 'FIELD DICT', field_dict print 'starting to process' for i, feat in enumerate(layer): if i > max_rows: continue if start_row and i < start_row: continue upload.rows_processed = i + 1 if not i % ((num_features / 50) or 5): print upload.rows_processed,'rp' upload.save() upload.save() rd = {} #geom = fromstr(feat.geom.wkt,srid=srid) if layer.srs: try: geom = OGRGeometry(feat.geom.wkt, layer.srs.proj4) geom.transform(4326) except Exception, e: print 'FAIL GEOM' print e, geom = None else: geom = OGRGeometry(feat.geom.wkt) if geom: geom = fromstr(geom.wkt) #create optional centroid for polys if upload.create_centroid and 'oly' in geom.geom_type: field_dict['geometry_pos'] = 'LOCATION' rd['geometry_pos'] = geom.point_on_surface.kml if upload.create_centroid_poly and 'oly' in geom.geom_type: field_dict['geometry_pos_poly_2'] = 'LOCATION' field_dict['geometry_pos_poly_3'] = 'LOCATION' rd['geometry_pos_poly_2'] = geom.point_on_surface.buffer(.0001,10).kml rd['geometry_pos_poly_3'] = geom.point_on_surface.buffer(.0005,10).kml #if it's > 1M characters, we need to simplify it for FT simplify_tolerance = .0001 while len(geom.kml) > 1000000: geom = geom.simplify(simplify_tolerance) print 'simplified to %f' % simplify_tolerance rd['import_notes'] = 'simplified to %d DD' % simplify_tolerance simplify_tolerance = simplify_tolerance * 1.5 if not geom.valid: rd['import_notes'] = '<br>Geometry not valid' kml = geom.kml rd['geometry'] = kml rd['geometry_vertex_count'] = geom.num_coords if upload.create_simplify and not 'oint' in geom.geom_type: amt = .002 if 'oly' in geom.geom_type: buffer_geom = geom.buffer(amt) buffer_geom = buffer_geom.buffer(amt * -1) simple_geom = buffer_geom.simplify(amt) else: simple_geom = geom.simplify(amt) rd['geometry_simplified'] = simple_geom.kml rd['geometry_simplified_vertex_count'] = simple_geom.num_coords for f in fields: val = feat.get(f) #make sure we have proper null type for diff fields if val == '<Null>': continue if not val: continue if field_dict[f] == 'DATETIME': val = val.isoformat().split('T')[0] if field_dict[f] == 'STRING' \ and create_int_style_cols \ and field_dict.has_key(f + '_ft_style'): #check to see if we have a number for this yet try: rd[f + '_ft_style'] = int_style_dict[f][val] except: int_style_dict[f][val] = len(int_style_dict[f]) rd[f + '_ft_style'] = int_style_dict[f][val] #however if we have too many distinct vals, let's just not do this anymore if len(int_style_dict[f]) > max_distinct_style_vals: print 'DELETING FD %s' % f del field_dict[f + '_ft_style'] del rd[f + '_ft_style'] #sucks, but now we should just remove all these fields from previous rows for srow in rows: try:del srow[f + '_ft_style'] except: pass #probably this was a null value? rd[f] = val rows.append(rd) #let's process 10k rows at a time.. not keep everything in memory if len(rows) > 10000: uploadRows(upload, field_dict, rows) rows = []