def ajax_get_popular(request): '''only show sources which have more than 2...''' res = {} if 'product_id' in request.POST: product_id = request.POST['product_id'] purches = Purchase.objects.filter(product__id=product_id) elif 'source_id' in request.POST: source_id = request.POST['source_id'] purches = Purchase.objects.filter(source__id=source_id) else: import ipdb;ipdb.set_trace() prices = {} products = {} sources = {} who_with= {} hours= {} for p in purches: cost = round(p.get_cost(), 1) prices[cost] = prices.get(cost, 0) + 1 sources[p.source] = sources.get(p.source, 0) + 1 products[p.product] = products.get(p.product, 0) + 1 for who in p.who_with.all(): who_with[who] = who_with.get(who, 0) + 1 hours[p.hour] = hours.get(p.hour, 0) + 1 res['prices'] = [k for k in sorted(prices.items(), key=lambda x:-1*x[1]) if k[1] > 1] res['sources'] = [((k[0].name, k[0].id), k[1]) for k in sorted(sources.items(), key=lambda x:-1*x[1]) if k[1] > 1] res['products'] =[((k[0].name, k[0].id), k[1]) for k in sorted(products.items(), key=lambda x:-1*x[1]) if k[1] > 1] res['who_with'] = [((str(k[0]), k[0].id), k[1]) for k in sorted(who_with.items(), key=lambda x:-1*x[1])][:50] res['sources'] = [k for k in res['sources']][:15] res['prices'] = sorted([k for k in res['prices']], key=lambda x:x[0]) try: res['hours'] = [((hour2name[k[0]], k[0]), k[1]) for k in sorted(hours.items(), key=lambda x:-1*x[1]) if k[0] is not None] except Exception,e: from utils import ipdb;ipdb()
def changelist_view(self, request, extra_context=None): '''rewriting this to sometimes kill the "ID" filter when you click on another one.''' if request.GET.has_key('id'): #delete id parameter if there are other filters! yes! real_keys = [k for k in request.GET.keys() if k not in getattr(self, 'not_count_filters', [])] if len(real_keys) != 1: q = request.GET.copy() del q['id'] request.GET = q request.META['QUERY_STRING'] = request.GET.urlencode() if request.method == 'GET': '''re-create a changelist, get the filter specs, and put them into the request somewhere to be picked up by a future edit to the change list template, which would allow them to be displayed for all normal changelist_view pages''' #this is so that I can display filters on the top of the page for easy cancelling them. if request.GET.has_key('_changelist_filters'): qq = request.GET.copy() del qq['_changelist_filters'] log.info('killed extraneous weird filter thingie which would have caused a 500 error.') request.GET = qq ChangeList = self.get_changelist(request) list_display = self.get_list_display(request) list_display_links = self.get_list_display_links(request, list_display) from django.contrib.admin import options list_filter = self.get_list_filter(request) cl = ChangeList(request, self.model, list_display, list_display_links, list_filter, self.date_hierarchy, self.search_fields, self.list_select_related, self.list_per_page, self.list_max_show_all, self.list_editable, self) used_filters = [xx for xx in cl.filter_specs if xx.used_parameters] filter_descriptions = [] from django.contrib.admin.filters import BooleanFieldListFilter if 'id' in request.GET: desc = ('id', request.GET['id'], make_untoggle_link(request, 'id')) filter_descriptions.append(desc) for key in request.GET.keys(): if key.endswith('__id'): desc = ('%s id' % key.split('__')[0], request.GET[key], make_untoggle_link(request, key)) filter_descriptions.append(desc) for uf in used_filters: if type(uf) == BooleanFieldListFilter: current_val = bool(int(uf.used_parameters.values()[0])) if current_val: desc = (uf.title, current_val, make_untoggle_link(request, uf.used_parameters.items()[0][0])) else: desc = (uf.title, current_val, make_untoggle_link(request, uf.used_parameters.items()[0][0])) filter_descriptions.append(desc) else: try: current_val = uf.used_parameters.values()[0] choice = None if getattr(uf, 'lookup_choices', False): got = False #looking up the "descriptive" way to describe the value. for choice in uf.lookup_choices: if choice[0] == current_val: choice = choice[1] break try: int(current_val) if choice[0] == int(current_val): choice = choice[1] break except ValueError: pass try: float(current_val) if choice[0] == float(current_val): choice = choice[1] break except ValueError: pass if not choice: from utils import ipdb;ipdb() else: choice = uf.used_parameters.keys()[0] choice = current_val desc = (uf.title, choice, make_untoggle_link(request, uf.used_parameters.items()[0][0])) filter_descriptions.append(desc) except Exception, e: from utils import ipdb;ipdb() if request.GET and 'q' in request.GET: desc = ('Searching for', "\"%s\"" % request.GET['q'], make_untoggle_link(request, 'q')) filter_descriptions.append(desc) request.filter_descriptions = filter_descriptions
def ajax_get_data(request): try: data=request.POST.dict() kind=data['kind'] action=data['action'] #kind2klass={'note':Note, } if kind=='note': if action=='new': day=Day.objects.get(id=data['day_id']) guy=Note(day=day) guy.save() html=guy.as_html() res={'success':True,'html':html,'message':None} elif action=='delete': guy=Note.objects.get(id=data['id']) guy.deleted=True guy.save() res={'success':True,'message':'deleted','note_id':data['id']} elif action=='get': guy=Note.objects.get(id=data['id']) html=guy.as_html() res={'success':True,'html':html,'message':None} elif kind=='person': if action=='new': exiperson=Person.objects.filter(first_name=data['first_name'],last_name=data['last_name']) if exiperson.exists(): person=exiperson[0] res={'success':True,'person_id':person.id,'message':'person already existed'} else: if 'gender' not in data or not data['gender']: res={'success':False,'message':'needs gender'} return r2j(res) gender=gender2id(data['gender']) if not 'met_through' in data or not data['met_through']: res={'success':False,'message':'needs met through'} return r2j(res) person=Person(first_name=data['first_name'],last_name=data['last_name'],gender=gender) person.save() through=Person.objects.get(id=data['met_through']) person.met_through.add(through) #only need to send new data, other stuff is wrapped. res={'success':True,'person_id':person.id,'message':'created %s'%person} elif kind=='source': if action=='new': if 'name' not in data or not data['name']: res={'success':False,'message':'needs name'} return r2j(res) exisource=Source.objects.filter(name=data['name']) if exisource.exists(): source=exisource[0] res={'success':True,'source_id':source.id,'message':'source already existed'} else: region=Region.objects.get(id=data['region_id']) source=Source(name=data['name'],region=region) source.save() res={'success':True,'source_id':source.id,'message':'created %s'%source} return r2j(res) except Exception,e: from utils import ipdb;ipdb() tb=traceback.format_exc(e) res={'success':False,'message':'error: %s<div class="pre">%s</div>%s'%(e,tb,data)} return r2j(res)