def test_cache_get_or_create(self): self.assertEqual(cache_get(self.vd1, 'name1'), None) value, is_created = cache_get_or_create(self.vd1, 'name1', None, lambda x: 'value%d' % x, 1) self.assertEqual(value, 'value1') self.assertEqual(is_created, True) value, is_created = cache_get_or_create(self.vd1, 'name1', None, lambda x: 'value%d' % x, 11) self.assertEqual(value, 'value1') self.assertEqual(is_created, False) cache_expire(self.vd1, 'name1', 1) sleep(1.001) value, is_created = cache_get_or_create(self.vd1, 'name1', None, lambda x: 'value%d' % x, 11) self.assertEqual(value, 'value11') self.assertEqual(is_created, True) cache_get_or_create(self.vd1, 'name2', None, lambda x: 'value%d' % x, 2) value, is_created = cache_get_or_create(self.vd1, 'name1', None, lambda x: 'value%d' % x, 11) self.assertEqual(value, 'value11') self.assertEqual(is_created, False) cache_get_or_create(self.vd2, 'name1', None, lambda x: 'asdf%d' % x, 1) value, is_created = cache_get_or_create(self.vd1, 'name1', None, lambda x: 'value%d' % x, 11) self.assertEqual(value, 'value11') self.assertEqual(is_created, False)
def userPost(self): def helper(self): pb = self.base_post for pp in self.total_pps: if pp.vd and pp.vd.is_private and pp.vd.id not in self.vd.realOwner_vd_ids and pp.vd.parent.id not in self.vd.realOwner_vd_ids: continue if pp.is_bounded: continue if pp.is_drop: # TODO : 이 부분이 테스트되는 테스트 추가 if pp.vd.id in self.vd.realOwner_vd_ids: pb = self.base_post else: # TODO : 친구는 이 장소를 drop 했으므로... 이를 어떠한 형태로든 userPost 에 반영하기 pass pass else: pb.update(pp.pb, pp.is_add) pb.uplace_uuid = self.uuid pb.place_id = self.place_id pb.normalize() return pb if not self._cache_userPost: self._cache_userPost, is_created = cache_get_or_create(self.vd, self.post_cache_name, None, helper, self) #self._cache_userPost = helper(self) return self._cache_userPost
def get_proper_uplaces_qs(vd, qs=None): def helper(vd, qs=None): qs = get_valid_uplaces_qs(qs) qs = qs.filter(vd_id__in=vd.realOwner_vd_ids) qs = qs.exclude(id__in=vd.realOwner_duplicated_uplace_ids) return qs result, is_created = cache_get_or_create(vd, 'uplaces', None, helper, vd, qs) return result
def realOwner_place_ids(self): def helper(vd_ids): from place.models import Place return [ place.id for place in Place.objects.filter(uplaces__vd_id__in=vd_ids) ] result, is_created = cache_get_or_create(self, 'realOwner_place_ids', None, helper, self.realOwner_vd_ids) return result
def placePost(self): def helper(self): pb = PostBase() for pp in self.pps.all().filter(uplace=None).order_by('id'): if pp.is_bounded: continue pb_new = pp.pb pb.update(pb_new, pp.is_add) pb.place_id = self.id pb.normalize() return pb if not self._cache_placePost: self._cache_placePost, is_created = cache_get_or_create(None, 'placePost_%s' % self.id, None, helper, self) return self._cache_placePost
def realOwner_publisher_ids(self): def helper(vd_ids): from importer.models import Importer importers = Importer.objects.filter(subscriber_id__in=vd_ids) if importers: return sum( [importer.publisher.vd_ids for importer in importers], []) else: return [] result, is_created = cache_get_or_create(self, 'realOwner_publisher_ids', None, helper, self.realOwner_vd_ids) return result
def compute_regions(vd, uplaces=None): def helper(vd, uplaces=None): if not uplaces: if not vd: raise NotImplementedError uplaces = list(get_proper_uplaces_qs(vd).exclude(lonLat=None)) for uplace in uplaces: uplace.value = uplace.lonLat uplace.timestamp = uplace.modified if len(uplaces) == 0: return None group0 = Group() group0.members = uplaces m_value = group0.lonLat from place.models import RADIUS_LOCAL_RANGE cluster = RegionClustering(group0, RADIUS_LOCAL_RANGE, distance_geography, m_value, True) cluster.run() elements = cluster.result for e in elements: e.type = 'lonLat' for i in [6, 5, 4]: group0 = Group() group0.members = elements m_value = group0.lonLat cluster = RegionClustering(group0, i*1000, distance_geography, m_value, True) cluster.run() cluster.result.sort(key=lambda g: g.count, reverse=True) for r in cluster.result: uplace = UserPlace(lonLat=r.lonLat) uplace.value = uplace.lonLat uplace.timestamp = 0 g = Group([uplace]) g.type = 'lonLat' elements.append(g) result = [member for member in cluster.result if member.count > 0] result.sort(key=lambda g: g.count, reverse=True) for g in result: g.distance = distance_geography return result result, is_created = cache_get_or_create(vd, 'regions', None, helper, vd) return result
def realOwner_duplicated_iplace_ids(self): def helper(vd_ids): from importer.models import ImportedPlace base = ImportedPlace.objects.all().exclude(place_id=None).filter( vd_id__in=vd_ids) group_by = base.values('place_id').annotate(cnt=Count(1)).filter( cnt__gte=2) result = list() before = None for iplace in base.filter( place_id__in=[row['place_id'] for row in group_by]).order_by( 'place_id', '-id'): if iplace.place_id == (before and before.place_id): result.append(iplace.id) before = iplace return result result, is_created = cache_get_or_create( self, 'realOwner_duplicated_iplace_ids', None, helper, self.realOwner_publisher_ids) return result
def realOwner_duplicated_uplace_ids(self): def helper(vd_ids): from place.libs import get_valid_uplaces_qs base = get_valid_uplaces_qs().exclude(place_id=None).filter( vd_id__in=vd_ids) group_by = base.values('place_id').annotate(cnt=Count(1)).filter( cnt__gte=2) result = list() before = None for uplace in base.filter( place_id__in=[row['place_id'] for row in group_by]).order_by( 'place_id', '-id'): if uplace.place_id == (before and before.place_id): result.append(uplace.id) before = uplace return result result, is_created = cache_get_or_create( self, 'realOwner_duplicated_uplace_ids', None, helper, self.realOwner_vd_ids) return result
def realOwner_vd_ids(self): if self.realOwner: result, is_created = cache_get_or_create( self, 'realOwner_vd_ids', None, lambda: self.realOwner.vd_ids) return result return [self.id]