def api_upload_attachment(): i = ctx.request.input(name='', description='', link='') name = i.name.strip() description = i.description.strip() f = i.file ref_type = 'attachment' ref_id = db.next_str() fcontent = f.file.read() filename = f.filename fext = os.path.splitext(filename)[1] if not name: name = os.path.splitext(os.path.split(filename)[1])[0] preview = None w = h = 0 res = store.upload_file(ref_type, ref_id, filename, fcontent) if res.mime.startswith('image/'): try: logging.info( 'it seems an image was uploaded, so try to get size...') im = thumbnail.as_image(fcontent) w, h = im.size[0], im.size[1] logging.info('size got: %d x %d' % (w, h)) if w > 160 or h > 120: logging.info( 'creating thumbnail for uploaded image (size %d x %d)...' % (w, h)) tn = thumbnail.create_thumbnail(im, 160, 120) pw, ph, pcontent = tn['width'], tn['height'], tn['data'] logging.info( 'thumbnail was created successfully with size %d x %d.' % (w, h)) preview = store.upload_file(ref_type, ref_id, filename, fcontent) else: logging.info('No need to create thumbnail.') preview = res except: logging.exception('error when creating thumbnail.') current = time.time() attr = Dict( \ id = ref_id, \ website_id = ctx.website.id, \ user_id = ctx.user.id, \ resource_id = res.id, \ preview_resource_id = preview and preview.id or '', \ name = name, \ description = description, \ width = w, \ height = h, \ size = res.size, \ mime = res.mime, \ creation_time = current, \ modified_time = current, \ version = 0) db.insert('attachments', **attr) if i.link == u't': attr.filelink = '/api/resources/url?id=%s' % attr.resource_id return attr
def upload_resource(ref_type, ref_id, fname, fp): ' upload resource and return resource object ' uname, uprovider = get_enabled_upload() if uname is None: return dict(error=_('No uploader selected')) filename = os.path.split(fname)[1] if len(filename) > 50: filename = filename[-50:] ext = os.path.splitext(filename)[1].lower() mime = mimetypes.types_map.get(ext, 'application/octet-stream') current = time.time() fcontent = fp if isinstance(fp, str) else fp.read() m = dict( \ id = db.next_str(), \ ref_id = ref_id, \ ref_type = ref_type, \ deleted = False, \ size = len(fcontent), \ filename = filename, \ mime = mime, \ uploader = uname, \ ref = '', \ url = '', \ creation_time = current, \ modified_time = current, \ version = 0 \ ) r = create_upload_provider(uname).upload(ref_type, ext, fcontent) for k in r: if k in m: m[k] = r[k] db.insert('resources', **m) return m
def do_register(): i = ctx.request.input(name='', email='', passwd='') name = i.name.strip() if not name: raise APIError('value', '', 'Invalid name.') email = i.email.strip().lower() check_email(email) passwd = i.passwd check_md5_passwd(passwd) us = db.select('select * from users where email=?', email) if us: raise APIError('register', '', 'Email already registered.') uid = db.next_str() db.insert('users', id=uid, name=name, email=email, passwd=passwd, version=0) make_session_cookie(uid, passwd) return {'id': uid}
def create_website(email, name, domain): # generate password: L = [] for i in range(10): n = int(random.random() * 62) if n < 10: L.append(chr(n + 48)) elif n < 36: L.append(chr(n + 55)) else: L.append(chr(n + 61)) passwd = ''.join(L) md5passwd = hashlib.md5(passwd).hexdigest() current = time.time() website = dict( id=db.next_str(), disabled=False, domain=domain, name=name, creation_time=current, modified_time=current, version=0) with db.transaction(): db.insert('websites', **website) create_user(website['id'], email, md5passwd, name, ROLE_ADMINISTRATORS, locked=True) return passwd
def upload_file(ref_type, ref_id, filename, fcontent): fileext = os.path.splitext(filename)[1].lower() filesize = len(fcontent) dt = datetime.now() fpath = os.path.join(ctx.website.id, str(ref_type), str(dt.year), str(dt.month), str(dt.day), '%s%s' % (uuid.uuid4().hex, fileext)) sname = get_enabled_store_name() url, the_ref = get_store_instance(sname).upload(fpath, fcontent) logging.info('uploaded file: %s' % url) ref = '%s:%s' % (sname, the_ref) r = Dict( \ id = db.next_str(), \ website_id = ctx.website.id, \ ref_id = ref_id, \ ref_type = ref_type, \ deleted = False, \ size = filesize, \ filename = filename, \ mime = mimetypes.types_map.get(fileext, 'application/octet-stream'), \ ref = ref, \ url = url, \ creation_time = time.time(), \ version = 0) db.insert('resources', **r) return r
def callback(): i = ctx.request.input(code='') code = i.code client = _create_client() r = client.request_access_token(code) logging.info('access token: %s' % json.dumps(r)) access_token, expires_in, uid = r.access_token, r.expires_in, r.uid client.set_access_token(access_token, expires_in) u = client.users.show.get(uid=uid) logging.info('got user: %s' % uid) users = db.select('select * from users where id=?', uid) user = dict(name=u.screen_name, \ image_url=u.avatar_large or u.profile_image_url, \ statuses_count=u.statuses_count, \ friends_count=u.friends_count, \ followers_count=u.followers_count, \ verified=u.verified, \ verified_type=u.verified_type, \ auth_token=access_token, \ expired_time=expires_in) if users: db.update_kw('users', 'id=?', uid, **user) else: user['id'] = uid db.insert('users', **user) _make_cookie(uid, access_token, expires_in) raise seeother('/')
def api_create_article(): i = ctx.request.input(category_id='', name='', tags='', content='', draft='') name = i.name.strip() content = i.content.strip() category_id = i.category_id if not name: raise APIValueError('name', 'name cannot be empty.') if not content: raise APIValueError('content', 'content cannot be empty.') if not category_id: raise APIValueError('category_id', 'category_id cannot be empty.') cat = _get_category(category_id) draft = True if ctx.user.role_id < ROLE_CONTRIBUTORS: draft = True if i.draft else False current = time.time() html_content, summary = html.parse_md(content, 800) article = Dict( \ id=db.next_str(), \ website_id=ctx.website.id, \ user_id=ctx.user.id, \ user_name=ctx.user.name, \ category_id=category_id, \ draft=draft, \ name=name, \ tags=_format_tags(i.tags), \ read_count=0, \ summary=summary, \ content=content, \ creation_time=current, \ modified_time=current, \ version=0) db.insert('articles', **article) return article
def get_default_cv(uid): cvs = None while not cvs: cvs = db.select('select * from resumes where user_id=?', uid) if not cvs: cv_id = db.next_str() db.insert('resumes', id=cv_id, user_id=uid, title='My Resume', version=0) db.insert('sections', id=db.next_str(), user_id=uid, resume_id=cv_id, display_order=0, kind='about', title='About', description='', version=0) cv = cvs[0] cv.sections = db.select( 'select * from sections where resume_id=? order by display_order', cv.id) for section in cv.sections: section.style = _SECTIONS_STYLE[section.kind] section.entries = db.select( 'select * from entries where section_id=? order by display_order', section.id) return cv
def update_timeline(): i = ctx.request.input() client = _create_client() data = client.parse_signed_request(i.signed_request) if data is None: raise StandardError('Error!') user_id = data.get('uid', '') auth_token = data.get('oauth_token', '') if not user_id or not auth_token: return dict(error='bad_signature') expires = data.expires client.set_access_token(auth_token, expires) u = db.select('select since_id from users where id=?', user_id)[0] kw = dict(uid=user_id, count=100, trim_user=1) since_id = u.since_id if since_id: kw['since_id'] = since_id timeline = client.statuses.user_timeline.get(**kw) statuses = timeline.statuses count = 0 if statuses: since_id = str(statuses[0].id) for st in statuses: info = record.parse(st.text) if info: t, ymd = _parse_datetime(st.created_at) r = dict(id=st.id, user_id=user_id, text=st.text, created_at=t, rdistance=info[0], rtime=info[1], rdate=ymd) if not db.select('select id from records where id=?', st.id): db.insert('records', **r) count = count + 1 db.update_kw('users', 'id=?', user_id, since_id = since_id) return dict(count=count, since_id=since_id)
def callback(): i = ctx.request.input(code="") code = i.code client = _create_client() r = client.request_access_token(code) logging.info("access token: %s" % json.dumps(r)) access_token, expires_in, uid = r.access_token, r.expires_in, r.uid client.set_access_token(access_token, expires_in) u = client.users.show.get(uid=uid) logging.info("got user: %s" % uid) users = db.select("select * from users where id=?", uid) user = dict( name=u.screen_name, image_url=u.avatar_large or u.profile_image_url, statuses_count=u.statuses_count, friends_count=u.friends_count, followers_count=u.followers_count, verified=u.verified, verified_type=u.verified_type, auth_token=access_token, expired_time=expires_in, ) if users: db.update_kw("users", "id=?", uid, **user) else: user["id"] = uid db.insert("users", **user) _make_cookie(uid, access_token, expires_in) raise seeother("/")
def add_entry(): _check_user() i = ctx.request.input(id='', title='', subtitle='', description='') title = i.title.strip() subtitle = i.subtitle.strip() description = i.description.strip() if not title: raise APIError('value', 'title', 'Title is empty') cv = get_default_cv(ctx.user.id) for s in cv.sections: if s.id == i.id: next_id = db.next_str() logging.info('NEXT-ID: ' + next_id) db.insert('entries', id=next_id, user_id=ctx.user.id, resume_id=cv.id, section_id=s.id, display_order=len(s.entries), title=title, subtitle=subtitle, description=description, picture='', version=0) return dict(id=next_id, title=title, subtitle=subtitle, description=description) raise APIError('value', 'id', 'Invalid section id.')
def add_section(): _check_user() i = ctx.request.input(kind='', title='', description='') kind = i.kind if not kind in _SECTIONS_SET: raise APIError('value', 'kind', 'Invalid kind.') title = i.title.strip() if not title: title = _SECTIONS_DICT.get(kind) description = i.description.strip() cv = get_default_cv(ctx.user.id) for s in cv.sections: if s.kind == kind: raise APIError('value', '', 'Section exist.') next_id = db.next_str() db.insert('sections', id=next_id, user_id=ctx.user.id, resume_id=cv.id, display_order=len(cv.sections), kind=kind, title=title, description=description, version=0) return dict(id=next_id, kind=kind, title=title, description=description)
def insert(self): self.pre_insert and self.pre_insert() params = {} for k, v in self.__mappings__.iteritems(): if v.insertable: if not hasattr(self, k): setattr(self, k, v.default) params[v.name] = getattr(self, k) db.insert('%s' % self.__table__, **params) return self
def get_menus(): ''' Get navigation menus as list, each element is a Dict object. ''' menus = db.select('select * from menus order by display_order, name') if menus: return menus current = time.time() menu = Dict(id=db.next_str(), name=u'Home', description=u'', type='latest_articles', display_order=0, ref='', url='/latest', creation_time=current, modified_time=current, version=0) db.insert('menus', **menu) return [menu]
def api_upload_attachment(): i = ctx.request.input(name='', description='', link='') name = i.name.strip() description = i.description.strip() f = i.file ref_type = 'attachment' ref_id = db.next_str() fcontent = f.file.read() filename = f.filename fext = os.path.splitext(filename)[1] if not name: name = os.path.splitext(os.path.split(filename)[1])[0] preview = None w = h = 0 res = store.upload_file(ref_type, ref_id, filename, fcontent) if res.mime.startswith('image/'): try: logging.info('it seems an image was uploaded, so try to get size...') im = thumbnail.as_image(fcontent) w, h = im.size[0], im.size[1] logging.info('size got: %d x %d' % (w, h)) if w > 160 or h > 120: logging.info('creating thumbnail for uploaded image (size %d x %d)...' % (w, h)) tn = thumbnail.create_thumbnail(im, 160, 120) pw, ph, pcontent = tn['width'], tn['height'], tn['data'] logging.info('thumbnail was created successfully with size %d x %d.' % (w, h)) preview = store.upload_file(ref_type, ref_id, filename, fcontent) else: logging.info('No need to create thumbnail.') preview = res except: logging.exception('error when creating thumbnail.') current = time.time() attr = Dict( \ id = ref_id, \ website_id = ctx.website.id, \ user_id = ctx.user.id, \ resource_id = res.id, \ preview_resource_id = preview and preview.id or '', \ name = name, \ description = description, \ width = w, \ height = h, \ size = res.size, \ mime = res.mime, \ creation_time = current, \ modified_time = current, \ version = 0) db.insert('attachments', **attr) if i.link==u't': attr.filelink = '/api/resources/url?id=%s' % attr.resource_id return attr
def insert(self): self.pre_insert and self.pre_insert() params = {} for k, v in self.__mappings__.iteritems(): if v.insertable: if not hasattr(self, k): print k setattr(self, k, v.default) params[v.name] = getattr(self, k) db.insert('%s' % self.__table__, **params) return self
def set_text(name, value): ''' Set text by name and value. ''' pos = name.find('_') if pos<=0: raise ValueError('bad setting name: %s must be xxx_xxx' % name) kind = name[:pos] current = time.time() if 0==db.update('update texts set value=?, modified_time=?, version=version+1 where name=?', value, current, name): st = dict(id=db.next_str(), kind=kind, name=name, value=value, creation_time=current, modified_time=current, version=0) db.insert('texts', **st)
def _create_wiki_page(wiki_id, parent_id, display_order, name, content): current = time.time() p = dict(id=db.next_str(), \ website_id=ctx.website.id, \ wiki_id=wiki_id, \ parent_id=parent_id, \ display_order=display_order, \ name=name, \ content=content, \ creation_time=current, \ modified_time=current, \ version=0) db.insert('wiki_pages', **p) return p
def _get_categories(): cats = db.select('select * from categories where website_id=? order by display_order, name', ctx.website.id) if not cats: logging.info('create default uncategorized...') current = time.time() uncategorized = Dict(id=db.next_str(), \ website_id=ctx.website.id, \ name='Uncategorized', description='', \ locked=True, display_order=0, \ creation_time=current, modified_time=current, \ version=0) db.insert('categories', **uncategorized) cats = [uncategorized] return cats
def insert(self): ''' 通过db对象的insert接口执行sql SQL :insert into ‘user’ ('password') ''' self.pre_insert and self.pre_insert() params = {} for k, v in self.__mappings__.iteritems(): if v.insertable: if not hasattr(self, k): setattr(self, k, v.default) params[v.name] = getattr(self, k) db.insert('%s' % self.__table__, **params) return self
def get_default_cv(uid): cvs = None while not cvs: cvs = db.select('select * from resumes where user_id=?', uid) if not cvs: cv_id = db.next_str() db.insert('resumes', id=cv_id, user_id=uid, title='My Resume', version=0) db.insert('sections', id=db.next_str(), user_id=uid, resume_id=cv_id, display_order=0, kind='about', title='About', description='', version=0) cv = cvs[0] cv.sections = db.select('select * from sections where resume_id=? order by display_order', cv.id) for section in cv.sections: section.style = _SECTIONS_STYLE[section.kind] section.entries = db.select('select * from entries where section_id=? order by display_order', section.id) return cv
def add_entry(): _check_user() i = ctx.request.input(id='', title='', subtitle='', description='') title = i.title.strip() subtitle = i.subtitle.strip() description = i.description.strip() if not title: raise APIError('value', 'title', 'Title is empty') cv = get_default_cv(ctx.user.id) for s in cv.sections: if s.id==i.id: next_id = db.next_str() logging.info('NEXT-ID: ' + next_id) db.insert('entries', id=next_id, user_id=ctx.user.id, resume_id=cv.id, section_id=s.id, display_order=len(s.entries), title=title, subtitle=subtitle, description=description, picture='', version=0) return dict(id=next_id, title=title, subtitle=subtitle, description=description) raise APIError('value', 'id', 'Invalid section id.')
def _create_wiki_page(wiki_id, parent_id, display_order, name, content): current = time.time() p = dict( id=db.next_str(), website_id=ctx.website.id, wiki_id=wiki_id, parent_id=parent_id, display_order=display_order, name=name, content=content, creation_time=current, modified_time=current, version=0, ) db.insert("wiki_pages", **p) return p
def make_comment(ref_type, ref_id, user, content): ''' Make a comment. Args: ref_type: the ref type, e.g. 'article'. ref_id: the ref id, e.g., article id. user: current user. content: comment content. Returns: the comment object as dict. ''' cid = db.next_str() kw = dict(id=cid, ref_type=ref_type, ref_id=ref_id, user_id=user.id, image_url=user.image_url, name=user.name, content=content, creation_time=time.time(), version=0) db.insert('comments', **kw) return kw
def _get_categories(): cats = db.select( 'select * from categories where website_id=? order by display_order, name', ctx.website.id) if not cats: logging.info('create default uncategorized...') current = time.time() uncategorized = Dict(id=db.next_str(), \ website_id=ctx.website.id, \ name='Uncategorized', description='', \ locked=True, display_order=0, \ creation_time=current, modified_time=current, \ version=0) db.insert('categories', **uncategorized) cats = [uncategorized] return cats
def create_user(website_id, email, passwd, name, role_id, locked=False): current = time.time() user = dict( id=db.next_str(), website_id=website_id, locked=locked, name=name, role_id=role_id, email=email, verified=False, passwd=passwd, image_url='http://www.gravatar.com/avatar/%s' % hashlib.md5(str(email)).hexdigest(), creation_time=current, modified_time=current, version=0) db.insert('users', **user) return user
def add_section(): _check_user() i = ctx.request.input(kind='', title='', description='') kind = i.kind if not kind in _SECTIONS_SET: raise APIError('value', 'kind', 'Invalid kind.') title = i.title.strip() if not title: title = _SECTIONS_DICT.get(kind) description = i.description.strip() cv = get_default_cv(ctx.user.id) for s in cv.sections: if s.kind==kind: raise APIError('value', '', 'Section exist.') next_id = db.next_str() db.insert('sections', id=next_id, user_id=ctx.user.id, resume_id=cv.id, display_order=len(cv.sections), kind=kind, title=title, description=description, version=0) return dict(id=next_id, kind=kind, title=title, description=description)
def insert(self): ''' Insert this object into database. ''' self.pre_insert and self.pre_insert() args = {} for k, v in self.__mappings__.iteritems(): if v.insertable: # if hasattr(self, k): # arg = getattr(self, k) # else: # arg = v.default() # setattr(self, k, arg) if not hasattr(self, k): setattr(self, k, v.default) args[k] = getattr(self, k) db.insert(self.__table__, **args) return self
def auth_callback(): ''' Callback from sina, then redirect to previous url. ''' code = ctx.request.input(code='').code if not code: raise seeother('/s/auth_failed') client = APIClient(app_key=APP_KEY, app_secret=APP_SECRET, redirect_uri=CALLBACK) r = client.request_access_token(code) access_token = r.access_token expires = r.expires_in uid = r.uid # get user info: client.set_access_token(access_token, expires) account = client.users.show.get(uid=uid) image = account.get(u'profile_image_url', u'about:blank') logging.info('got account: %s' % str(account)) name = account.get('screen_name', u'') or account.get('name', u'') id = u'weibo_%s' % uid user = auth.fn_load_user(id) if user: # update user if necessary: db.update('update user set name=?, oauth_image=?, oauth_access_token=?, oauth_expires=? where id=?', \ name, image, access_token, expires, id) else: db.insert('user', \ id = id, \ name = name, \ oauth_access_token = access_token, \ oauth_expires = expires, \ oauth_url = u'http://weibo.com/u/%s' % uid, \ oauth_image = image, \ admin = False) # make a signin cookie: cookie_str = auth.make_session_cookie(id, access_token, expires) logging.info('will set cookie: %s' % cookie_str) redirect = ctx.request.cookie(COOKIE_REDIRECT, '/') ctx.response.set_cookie(auth.COOKIE_AUTH, cookie_str, expires=expires) ctx.response.delete_cookie(COOKIE_REDIRECT) raise seeother(redirect)
def api_create_category(): i = ctx.request.input(name='', description='') name = i.name.strip() description = i.description.strip() if not name: raise APIValueError('name', 'name cannot be empty') num = len(_get_categories()) if num >= 100: raise APIError('operation:failed', 'category', 'cannot create new category for the maximum number of categories was reached.') logging.info('create new category...') current = time.time() cat = Dict(id=db.next_str(), \ website_id=ctx.user.website_id, \ name=name, description=description, \ locked=False, display_order=num, \ creation_time=current, modified_time=current, \ version=0) db.insert('categories', **cat) return cat
def _set_setting(website_id, kind, key, value): ''' Set setting by kind, key and value. ''' if len(kind) == 0 or len(kind) > 50 or len(key) == 0 or len(key) > 50: raise ValueError('invalid setting name.') if not isinstance(value, (str, unicode)): value = str(value) name = '%s:%s' % (kind, key) settings = dict( \ id = db.next_str(), \ website_id = website_id, \ kind = kind, \ name = name, \ value = value, \ creation_time = time.time(), \ version = 0) db.update('delete from settings where name=? and website_id=?', name, website_id) db.insert('settings', **settings)
def index(): i = ctx.request.input() client = _create_client() data = client.parse_signed_request(i.signed_request) if data is None: raise StandardError('Error!') user_id = data.get('uid', '') auth_token = data.get('oauth_token', '') if not user_id or not auth_token: return Template('/static/auth.html', client_id=APP_ID) expires = data.expires client.set_access_token(auth_token, expires) # check database if user exist: user = None users = db.select('select * from users where id=?', user_id) if users: # user exist, update if token changed: user = users[0] if auth_token != user.auth_token: uu = _from_weibo_user(client.users.show.get(uid=user_id)) uu['auth_token'] = auth_token uu['expired_time'] = expires user.update(uu) db.update_kw('users', 'id=?', user_id, **uu) else: u = client.users.show.get(uid=user_id) user = _from_weibo_user(u) user['id'] = user_id user['level'] = 0 user['weight'] = 55 if user['gender'] == u'f' else 75 user['since_id'] = '' user['auth_token'] = auth_token user['expired_time'] = expires db.insert('users', **user) img = user['avatar_large'] or user['profile_image_url'] or user['image_url'] return Template('/static/index.html', user=user, user_img=img, signed_request=i.signed_request)
def set_text(kind, key, value): ''' Set text by kind, key and value. ''' if len(kind) == 0 or len(kind) > 50 or len(key) == 0 or len(key) > 50: raise ValueError('invalid setting name.') if not isinstance(value, (str, unicode)): value = str(value) name = '%s:%s' % (kind, key) text = dict( \ id = db.next_str(), \ website_id = ctx.website.id, \ kind = kind, \ name = name, \ value = value, \ creation_time = time.time(), \ version = 0) db.update('delete from texts where name=? and website_id=?', name, ctx.website.id) db.insert('texts', **text) cache.client.delete('TEXT:%s:%s:%s' % (ctx.website.id, kind, key))
def api_create_wiki(): ' create a new wiki. ' i = ctx.request.input(name='', description='', content='') name = i.name.strip() if not name: raise APIValueError('name', 'name cannot be empty') content = i.content.strip() if not content: raise APIValueError('content', 'content cannot be empty') current = time.time() wiki = Dict( \ id=db.next_str(), \ website_id=ctx.website.id, \ name=name, \ description=i.description.strip(), \ content=content, \ creation_time=current, \ modified_time=current, \ version=0) db.insert('wikis', **wiki) return wiki
def update_timeline(): i = ctx.request.input() client = _create_client() data = client.parse_signed_request(i.signed_request) if data is None: raise StandardError('Error!') user_id = data.get('uid', '') auth_token = data.get('oauth_token', '') if not user_id or not auth_token: return dict(error='bad_signature') expires = data.expires client.set_access_token(auth_token, expires) u = db.select('select since_id from users where id=?', user_id)[0] kw = dict(uid=user_id, count=100, trim_user=1) since_id = u.since_id if since_id: kw['since_id'] = since_id timeline = client.statuses.user_timeline.get(**kw) statuses = timeline.statuses count = 0 if statuses: since_id = str(statuses[0].id) for st in statuses: info = record.parse(st.text) if info: t, ymd = _parse_datetime(st.created_at) r = dict(id=st.id, user_id=user_id, text=st.text, created_at=t, rdistance=info[0], rtime=info[1], rdate=ymd) if not db.select('select id from records where id=?', st.id): db.insert('records', **r) count = count + 1 db.update_kw('users', 'id=?', user_id, since_id=since_id) return dict(count=count, since_id=since_id)
def api_create_wiki(): " create a new wiki. " i = ctx.request.input(name="", description="", content="") name = i.name.strip() if not name: raise APIValueError("name", "name cannot be empty") content = i.content.strip() if not content: raise APIValueError("content", "content cannot be empty") current = time.time() wiki = Dict( id=db.next_str(), website_id=ctx.website.id, name=name, description=i.description.strip(), content=content, creation_time=current, modified_time=current, version=0, ) db.insert("wikis", **wiki) return wiki
def api_create_category(): i = ctx.request.input(name='', description='') name = i.name.strip() description = i.description.strip() if not name: raise APIValueError('name', 'name cannot be empty') num = len(_get_categories()) if num >= 100: raise APIError( 'operation:failed', 'category', 'cannot create new category for the maximum number of categories was reached.' ) logging.info('create new category...') current = time.time() cat = Dict(id=db.next_str(), \ website_id=ctx.user.website_id, \ name=name, description=description, \ locked=False, display_order=num, \ creation_time=current, modified_time=current, \ version=0) db.insert('categories', **cat) return cat
def index(): i = ctx.request.input() client = _create_client() data = client.parse_signed_request(i.signed_request) if data is None: raise StandardError('Error!') user_id = data.get('uid', '') auth_token = data.get('oauth_token', '') if not user_id or not auth_token: return Template('/static/auth.html', client_id=APP_ID) expires = data.expires client.set_access_token(auth_token, expires) # check database if user exist: user = None users = db.select('select * from users where id=?', user_id) if users: # user exist, update if token changed: user = users[0] if auth_token != user.auth_token: uu = _from_weibo_user(client.users.show.get(uid=user_id)) uu['auth_token'] = auth_token uu['expired_time'] = expires user.update(uu) db.update_kw('users', 'id=?', user_id, **uu) else: u = client.users.show.get(uid=user_id) user = _from_weibo_user(u) user['id'] = user_id user['level'] = 0 user['weight'] = 55 if user['gender']==u'f' else 75 user['since_id'] = '' user['auth_token'] = auth_token user['expired_time'] = expires db.insert('users', **user) img = user['avatar_large'] or user['profile_image_url'] or user['image_url'] return Template('/static/index.html', user=user, user_img=img, signed_request=i.signed_request)
def api_create_page(): i = ctx.request.input(name='', tags='', content='', draft='false') name = i.name.strip() content = i.content.strip() if not name: raise APIValueError('name', 'name cannot be empty.') if not content: raise APIValueError('content', 'content cannot be empty.') draft = boolean(i.draft) current = time.time() page = Dict( \ id=db.next_str(), \ website_id=ctx.website.id, \ draft=draft, \ name=name, \ tags=_format_tags(i.tags), \ read_count=0, \ content=content, \ creation_time=current, \ modified_time=current, \ version=0) db.insert('pages', **page) return page
def _Downloud_All(screen_name): u = _check_cookie() if u is None: return dict(error='failed', redirect='/signin') client = _create_client() client.set_access_token(u.auth_token, u.expired_time) # 目标id #uid_tar = 1005051640494672#5439666129test #screen_name = u"经典语录" url_name = screen_name.encode('utf-8') urllib.quote(url_name) result = [] friendship_url = 'https://api.weibo.com/2/friendships/create.json?' #data = ('%s%s%s%s' % ( 'uid=',str(uid_tar),'&access_token=',client.access_token)) data = ('%s%s%s%s' % ( 'screen_name=',url_name,'&access_token=',client.access_token)) # 关注目标 #r = urllib2.Request(url= friendship_url, data=data) #f = urllib2.urlopen(r) length = 1 index = 1 count = 0 while length > 0 and count < 1000: statuses = client.statuses__friends_timeline(page=index)['statuses'] length = len(statuses) #输出了部分信息 for i in range(0,length): #print index, i, length, count #if (statuses[i]['user']['id'] == uid_tar): count = count + 1 sigresult = { 'text': statuses[i]['text'], 'picurl': statuses[i]['pic_urls'],'time': statuses[i]['created_at']} #print sigresult['text'] result.append(sigresult) #print result[count-1]['text'] index = index + 1 #取消关注 #friendship_des_url = 'https://api.weibo.com/2/friendships/destroy.json?' #r = urllib2.Request(url= friendship_des_url, data=data) #f = urllib2.urlopen(r) length = len(result) print length filename = 'positive_word.rtf' f=open(filename,'r') lines=f.readlines() count=0 query={} query1=[] for line in lines: #print isinstance(line, unicode) line=line.split() line=line[0] query[count]=line sigquery = { 'query': line , 'weight': 0, 'picurl': [] } query1.append(sigquery) count=count+1 #print count f.close() for i in range(0,count): for j in range(0,length): rs = result[j]['text'].encode('utf-8') if rs.find(query[i]) >= 0: query1[i]['weight'] = query1[i]['weight'] + 1 if len(result[j]['picurl'])>0: query1[i]['picurl'].append({'picurl':result[j]['picurl'][0],'time':result[j]['time']}) # 新浪返回的token,类似abc123xyz456 #screen_name = 'testName' db.update('delete from pictures where username=?', screen_name) # insert labels for i in range(0, count): l = query1[i]['query']#.decode('utf-8') w = query1[i]['weight'] pics = query1[i]['picurl'] label = dict( username=screen_name, \ label=l, \ weight=w ) lb = db.select('select * from labels where label=? and username=?', l, screen_name) if lb: db.update_kw('labels', 'label=? and username=?', l, screen_name, **label) else: db.insert('labels', **label) li = db.select('select id from labels where label=? and username=?', l, screen_name) #print li for j in range(0, len(pics)): picture = dict( picUrl=pics[j]['picurl'], \ upTime=pics[j]['time'], \ labelID=li[0]['id'], \ username=screen_name ) db.insert('pictures', **picture) return query1
# ] # 如果要执行 insert update deletecaozuo ,执行 update() 方法,返回受影响的行数 # n=db.update('update users set name = "Lily" where id in (?,?',4,5) # update() 函数签名为 update(slq,*args) # 统一用?作为占位符,并传入可变参数来绑定,从根本上避免 SQL 注入共计 # 每个 select() 和 update() 调用,都隐含地自动打开并关闭了数据库连接,这样上层调用者完全不必关心数据库地层连接 # 但是,如果要在一个数据库连接里执行多个 SQL 语句怎么办?我们用一个 with 语句实现: with db.connection(): db.select('...') db.select('...') db.select('...') # 如果要在一个数据库失误中执行多个SQL 语句怎么办?我们还是用一个 with 语句实现: with db.transaction(): db.select('...') db.update('...') db.insert('...') #实现 DB 模块 #由于模块是全局对象,模块变量是全局唯一变量,有两个重要的变量: #代码在 operational_training 中编写 # -*- coding: UTF-8 -*- import threading # 数据库引擎对象 class _Engine(object): def __init__(self, connect): self._connect = connect def connect(self): return self._connect()