Ejemplo n.º 1
0
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))
Ejemplo n.º 2
0
def downloadBook(bid, old=None):
    url = ApiOperation.apiGenerator('BOOK', bid)
    api = douban.Api(url=url, api_type='BOOK')
    processor = BookProcessor(api)
    if old:
        return processor.getBook(old)
    else:
        return processor.getBook()
Ejemplo n.º 3
0
def downloadBookTags(bid):
    url = ApiOperation.apiGenerator('BOOK_TAGS', bid)
    api = douban.Api(url=url, api_type='BOOK_TAGS')
    processor = BookProcessor(api)
    tags = processor.getBookTags()
    if tags:
        return tags
    else:
        return []
Ejemplo n.º 4
0
def welcome_douban(request):
    api_operation = ApiOperation(key='default')
    results = None
    error_message = ''
    key = request.GET.get('oauth_token', '')
    if key:
        request_token = getRequestToken(key)
        if request_token:
            results = api_operation.getServiceFromRequest(request_token)
    if results:
        service, uid = results
        profile = UserData(uid).getProfile()
        if profile:
            try:
                new_user = User.objects.create_user(username = profile['uid'],
                                                    password = profile['uid'])
                new_user.backend="%s.%s" %('django.contrib.auth.backends','ModelBackend')
                #以新用户登陆
                auth.login(request, new_user)
                info = UserProfile(user=new_user)
                info.nickname = profile['nickname']
                info.image_link = profile['image_link']
                info.sign = profile['signature']
                info.website = profile['website']
                info.introduction = profile['introduction']
                info.did = profile['uid']
                try:
                    info.save()
                except Exception, error:
                    logger.debug(str(error))
                
                #启动新用户的后台线程 完成推荐数据的初始化
                InitData.initUserData(uid)
                
                #设置密码, 默认为用户名
                user_form = UserChangeForm(instance=new_user)
                return render_to_response("accounts/settings.html", {
                        'msg':"请设置登录密码, 现在默认和用户名相同",
                        'user_form':user_form
                        },context_instance=RequestContext(request))
            except Exception, error:
                error_message = _("Sorry, we have a error when create new user: %s."%(error))
Ejemplo n.º 5
0
Archivo: book.py Proyecto: klb3713/BYSJ
class BookProcessor():
    """
        methods about book data processing 
    """

    def __init__(self, api, key='book'):
        self.api = api
        self.api_operation = ApiOperation(key, api)
        self.service = self.api_operation.getService()
        self.db = MongoDB.getConnection()
    
    def apiProcess(self):
        api_map = {
            'BOOK' : lambda: self.getBook(),
            'BOOK_COLLECTION' : lambda: self.getBookCollections(),
            'BOOK_REVIEWS' : lambda: self.getBookReviews(),
            'BOOK_TAGS' : lambda: self.getBookTags(),
        }
        return api_map[self.api.api_type]()
        
    def getBook(self, oldbook=None):
        book = self.service.GetBook(self.api.url)
        if book:
            if oldbook:
                oldbook.summary = book.summary
                oldbook.common_tags = book.common_tags
                oldbook.rating = book.rating
                try:
                    oldbook.save()
                    return oldbook
                except mongoengine.base.ValidationError, error:
                    logger.debug("[ERROR]%s: %s at bid:%s" %(error, error.to_dict(), oldbook.bid))
            
            try:
                book.save()
            except mongoengine.base.ValidationError, error:
                logger.debug("[ERROR]%s: %s at bid:%s" %(error, error.to_dict(), book.bid))
            
            book_reviews_api = self.api_operation.apiGenerator('BOOK_REVIEWS', book.bid)
            api = ReviewApi(url=book_reviews_api, api_id=book.bid, api_type='BOOK_REVIEWS')
            api.save()
            return book
Ejemplo n.º 6
0
Archivo: user.py Proyecto: klb3713/BYSJ
class UserProcessor(object):
    """
        methods about user data processing 
    """

    def __init__(self, api, key='default'):
        self.api = api
        self.api_operation = ApiOperation(key, api)
        self.service = self.api_operation.getService()
        self.db = MongoDB.getConnection()
        
    def __del__(self):
        pass
        
    def apiProcess(self):
        api_map = {
            'USER' : lambda: self.getUser(),
            'USER_CONTACTS' : lambda: self.getUserContacts(),
            'USER_FRIENDS' : lambda: self.getUserFriends(),
            'USER_NOTES' : lambda: self.getUserNotes(),
            'USER_RECOMMENDATIONS' : lambda: self.getUserRecommendations(),
            'USER_BOOK_REVIEWS' : lambda: self.getUserBookReviews(),
            'USER_BOOK_TAGS' : lambda: self.getUserBookTags(),
        }
        return api_map[self.api.api_type]()
    
    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]
        else:
Ejemplo n.º 7
0
Archivo: book.py Proyecto: klb3713/BYSJ
 def __init__(self, api, key='book'):
     self.api = api
     self.api_operation = ApiOperation(key, api)
     self.service = self.api_operation.getService()
     self.db = MongoDB.getConnection()
Ejemplo n.º 8
0
def authDouban(request):
    api_operation = ApiOperation(key='default')
    auth_url = api_operation.getOAuthUrl(next='/accounts/welcome?type=douban')
    return HttpResponseRedirect(auth_url)