def people(request, did): if not did: return HttpResponseRedirect("/") query = DUser.objects(uid=did) people = None if query: people = query[0] else: #TODO remove url = ApiOperation.apiGenerator('USER', did) api = Api(url=url, api_type='USER', api_id=did) processor = UserProcessor(api) result = processor.getUser() if result: people = result else: return HttpResponseRedirect("/") #TODO end if people.content and people.content.find('\n') != -1: people.content = people.content.split('\n') people.image_link = people.image_link.replace('icon/u', 'icon/ul') try: follower = UserProfile.objects.get(did=did).user except: error_message = "抱歉,该用户还没有注册本网站..." return render_to_response('error.html', {'error':error_message, 'redirect_url':'/people/follow/'}, context_instance=RequestContext(request)) followed = len(FollowRelation.objects.filter(user=request.user, follower=follower)) > 0 return render_to_response('people/people.html', {'people': people, 'followed': followed}, context_instance=RequestContext(request))
def getUserBookTags(self): results = self.service.GetUserBookTags(self.api.url) if results: book_tags, count = results else: return False if not book_tags: return False user = None users = User.objects(uid=self.api.api_id) if users: user = users[0] else: return False try: user_book_related = None user_book_related, created = UserBookRelated.objects.get_or_create( uid=user.uid, auto_save=False, defaults={'uid':user.uid, 'book_tags':book_tags}) if not created: user_book_related.book_tags.extend(book_tags) try: user_book_related.save() except mongoengine.base.ValidationError, error: logger.debug("[ERROR]%s: %s when save %s at api:%s" %(error, \ error.to_dict(), user_book_related, self.api.url)) except Exception, error: logger.debug("[ERROR]%s: UserBookRelated.objects.get_or_create(uid=%s ..."\ %(error, self.api.api_id))
def getUser(self): user = self.service.GetPeople(self.api.url) if user: exist_user = User.objects(uid=user.uid) if not exist_user: try: user.save() except mongoengine.base.ValidationError, error: logger.debug("[ERROR]%s: %s at uid:%s" %(error, error.to_dict(), user.uid)) return False else: return exist_user[0]
def getUserContacts(self, recursion=False): results = self.service.GetContacts(self.api.url) if results: users, count = results else: return False new_users = [] new_users_data = [] indexs = [] for index, user in enumerate(users): exist_user = User.objects(uid=user.uid) if exist_user: users[index] = exist_user[0] else: new_users.append(user) new_user = user.__dict__.get('_data') for key, value in new_user.items(): if value is None: new_user.pop(key) new_user.pop('image') new_users_data.append(new_user) indexs.append(index) if new_users: ids = self.db.user.insert(new_users_data) for i, index in enumerate(indexs): users[index].id = ids[i] try: uid = self.api.api_id user_related = None user_related, created = UserRelated.objects.get_or_create( uid=uid, auto_save=False, defaults = {'uid':uid, 'contacts':users}) if not created: user_related.contacts.extend(users) try: user_related.save() except mongoengine.base.ValidationError, error: logger.debug("[ERROR]%s: %s when save %s at api:%s" \ %(error, error.to_dict(), user_related, self.api.url)) except Exception, error: logger.debug("[ERROR]%s: UserRelated.objects.get_or_create(uid=%s ..."\ %(error, self.api.api_id))
def getUserFriends(self, recursion=False): users = self.service.GetFriends(self.api.url) if results: users, count = results else: return False new_users = [] for index, user in enumerate(users): exist_user = User.objects(uid=user.uid) if exist_user: users[index] = exist_user[0] else: new_users.append(user) try: user.save() except mongoengine.base.ValidationError, error: logger.debug("[ERROR]%s: %s when save uid:%s at api:%s" \ %(error, error.to_dict(), user.uid, self.api.url))