Exemple #1
0
def location(request, pk):
  location = get_object_or_404(models.Locations, id=pk)
  favorite = get_is_favorite(request, location)
  category_tree = []
  fav_pk = pk
  related = models.Locations.objects.filter(
              map_add=True,
              admin_check=True,
              category__in=[location.category]).exclude(id=pk)
  if location.category:
    category_tree = location.category.get_ancestors(include_self=True)
  if favorite and request.user.is_authenticated():
     fav_pk = location.locatfavoritedescrip_set.filter(user=request.user).last().pk
  if request.is_ajax():
    return render(
       request,
       'favorites/inner/favorite_tag_location.html',
       {'location': location,
        'favorite': favorite,
        'fav_pk': fav_pk })
  return render(
       request,
       'location_item/index.html',
       {'location': location,
        'category_tree': category_tree,
        'favorite': favorite,
        'fav_pk': fav_pk,
        'related': related })
Exemple #2
0
def catalog(request, slug=None, model=None, filter=None):
  filter_kwargs = dict(map_add=True, admin_check=True)
  template_dict = {}
  if not request.is_ajax():
    condition = models.LocatCondition.objects.all()
    interior = models.LocatInterior.objects.all()
    room = models.LocatRoom.objects.all()
    floor = models.LocatFloor.objects.all()
    categories = models.LocatCategory.objects.all()
    districts_level0 = models.LocatDistrict.objects.filter(level=0)
    districts_level0.middle = float(len(districts_level0))/2
    template = 'catalog/index.html'
    template_dict.update({'condition':condition, 'interior':interior, 'room':room, 'floor':floor, 'categories':categories, 'districts':districts_level0})
  else:
    template = 'catalog/location_list_result.html'
  # Theme START
  category_list_slug = None
  if slug and model:
    obj = get_object_or_404(model, slug=slug)
    filter_kwargs['themes__in'] = [obj.id]
    template_dict.update({'theme':obj})
  location_list = models.Locations.objects.filter(**filter_kwargs)
  # Theme END
  # Filter START
  category_selection = {}
  district_selection = []
  if request.GET.get('category'):
    filt = request.GET.get('category')
    result = get_locations_by_category_complex(filt)
    location_list = result['locations']
    category_selection = result['user_selection']
    category_list_slug = result['filter_category']
    # test = result['test']
    # template_dict.update({'test':test })
  if request.GET.get('area'):
    filt = request.GET.get('area')
    filter_area = [c.strip() for c in filt.split(',')]
    if location_list and request.GET.get('category') or model == models.LocatTheme:
      location_id = []
      for obj in location_list:
        location_id.append(obj.id)
      location_list = models.Locations.objects.filter(id__in=location_id, area__in=filter_area)
    else:
      location_list = models.Locations.objects.filter(map_add=True, admin_check=True, area__in=filter_area)
    district_selection = models.LocatDistrict.objects.filter(id__in=filter_area)
  elif not request.GET.get('category') and not request.GET.get('area') and model != models.LocatTheme:
    location_list = models.Locations.objects.filter(map_add=True, admin_check=True)
  template_dict.update({'category_selection':category_selection, 'district_selection':district_selection})
  if not request.is_ajax() and category_list_slug:
    result = get_open_root_check(category_list_slug)
    root_id = result['root_id']
    check = result['check']
    template_dict.update({'check':check, 'open_root_id':root_id})
  # Filter END
  # Sorting START
  if request.GET.get('sorting'):
    sorting = request.GET.get('sorting')
    location_list = location_list.order_by(sorting)
    template_dict.update({'sorting':sorting})
  # Sorting END
  # View START
  if request.GET.get('view'):
    view = request.GET.get('view')
    template_dict.update({'view': view })
  # View END
  # Pagination START
  try:
    page = int(request.GET.get('page', '1'))
    per_page = int(request.GET.get('per_page', '36'))
  except ValueError:
    page, per_page = (1, 36)
  paginator = Paginator(location_list, per_page)
  try:
    locations = paginator.page(page)
  except (EmptyPage, InvalidPage):
    # If page is out of range (e.g. 9999), deliver last page of results.
    locations = paginator.page(paginator.num_pages)
  # Favorite flag START
  for location in locations:
    location.fav_flag = get_is_favorite(request, location)
  # Favorite flag END
  template_dict.update({'locations':locations})
  # Pagination END
  return render(request, template, template_dict)