def wxa_login(self, params, appid, appsecret, code): wxserver = 'https://api.weixin.qq.com/sns/jscode2session' params = { 'appid': appid, 'secret': appsecret, 'js_code': code, 'grant_type': 'authorization_code' } try: res = py3get(wxserver, params) except Exception as e: raise e res = json.loads(res) openid = res.get('openid') user = User.objects.filter(openid=openid).first() if not user: user = User.objects.create() dic2obj(params, [ 'username', 'phone', 'nickname', 'avatar', 'gender', 'country', 'province', 'city' ], user) user.openid = openid user.save() self.session_set('me', user.to_simple_json()) session = self.get_session() session.save() ids = Feed2User.objects.filter(owner_user_id=user.id).values_list( 'owner_feed_id', flat=True) feeds = Feed.objects.filter(id__in=ids) return { 'sessionid': session.session_key, 'feeds': gen_pager_array(feeds, {}) }
def saveUser(self, _param): """ 保存用户信息 @param _param: @return: """ flag_add = True user = User.objects.filter( pk=_param['id']).first() if _param.get('id', None) else None if user: flag_add = False if user and User.objects.exclude(pk=_param['id']).filter( username=_param['username']).count(): raise ex(u'该用户名已经存在!') if not user: if _param.get('username', None) and User.objects.filter( username=_param['username']): raise ex(u'该用户名已经存在!') user = User() dic2obj(user, [ 'username', 'truename', 'name', 'email', 'phone', 'department_id', 'role_id' ], _param) if _param.get('password', ''): user.password = _param['password'] user.save() self.logAtion(ActionLog.ACTION_MODIFY, User, user.id)
def saveNote(self, _param): '''保存记录''' from base.utils import dic2obj from main.models import Note note = _param.get('id') and Note.objects.filter( id=_param['id']).first() or Note() dic2obj(note, ['date', 'title', 'content', 'type', 'icon', 'image'], _param) note.save() return note
def saveUserInfo(self, _param): ''' 保存个人信息 @param _param: @return: ''' utils.verify_phone_format(_param.get('phone', '')) me = self.get_me() dic2obj(me, ['phone', 'truename', 'address', 'icon', 'style'], _param) me.save()
def register(self, params, username, password): user = User.objects.filter(username=username) if user: raise Exception('用户名已被占用') user = User.objects.create() dic2obj(params, [ 'username', 'password', 'nickname', 'phone', 'gender', 'email', 'openid', 'city', 'age' ], user) user.save() return {'username': username, 'id': user.id}
def changeUserInfo(self, _param): ''' 修改用户密码 @param _param: @return: ''' user = self.get_me() dic2obj( user, ['name', 'email', 'phone', 'address', 'icon', 'language', 'style'], _param) user.save()
def add_rss(self, params, url): feed = Feed.objects.filter(url=url).first() if feed: f2u = Feed2User.objects.filter( owner_feed=feed, owner_user_id=self.get_meid()).first() if f2u: raise Exception('订阅地址已添加') else: feed = Feed(url=url) feed_data = get_feed(url) dic2obj(feed_data, ['title', 'subtitle'], feed) feed.save() Post.create_by_entries(feed.id, feed_data.get('entries')) Feed2User.objects.create(owner_user_id=self.get_meid(), owner_feed=feed) return { 'id': feed.id, 'title': feed.title, 'subtitle': feed.subtitle, 'url': url, }
def saveModel(self, modelname, _param): ''' 保存模型数据 @param modelname: @param _param: @return: ''' is_add = False if not self.check_permission(modelname, 'is_edit'): raise ex(u'用户权限不足') model = self.getModel(modelname) # if hasattr(model, 'role_write') and not self.get_me().checkrole(model.role_write): # raise ex(u'用户权限不足:%s' % model.role_write) objid = _param.get('id') o = model.objects.filter(pk=objid).first() if o: if modelname == 'User' and _param.get('password', None) == '': _param.pop('password') else: is_add = True if modelname == 'User': print _param.get('username', None) u = User.objects.filter(username=_param.get('username', None)) if u: raise ex('该用户已经存在') o = model() emptyfields = [] fields = model.get_editfields() for f in model.get_fields(): if f.name in fields and (not f.blank and not f.null) and not _param.get( f.name, None): if hasattr(f, 'rel') and hasattr(f.rel, 'to'): fn = f.name + "_id" if not f.name.endswith( '_id') else f.name if not _param.get(fn, None): emptyfields.append(f.verbose_name) else: emptyfields.append(f.verbose_name) if modelname == 'User' and f.name == 'password' and not is_add: emptyfields.pop() if (f.__class__.__name__ == 'ForeignKey' or f.__class__.__name__ == 'OneToOneField' ) and not f.name.endswith('_id') and hasattr( f, 'rel') and f.editable and not f.primary_key and hasattr( f.rel, 'to'): fields.remove(f.name) fields.append(f.name + "_id") if _param.get(f.name + '_id') == '': _param[f.name + '_id'] = None if f.__class__.__name__ == 'ChildrenModelField' and f.name in fields: fields.remove(f.name) if (f.__class__.__name__ == 'DateField' or f.__class__.__name__ == 'IntegerField' or f.__class__.__name__ == 'FloatField') and _param.get( f.name) == '': _param[f.name] = None if emptyfields: efs = ','.join(emptyfields).decode('utf-8') raise ex(u"'%s'不能为空" % (efs)) if hasattr(o, 'modify'): o.modify(self.get_me(), fields, _param) else: dic2obj(o, fields, _param) if self.check_permission(modelname, 'is_self'): self.verify_user(None, None, _param, is_edit=True) o.save() if is_add: self.logAtion( ActionLog.ACTION_ADD, model=model, content=o.__unicode__() if hasattr(o, '__unicode__') else None, objsid=o.id, model_cn=o._meta.verbose_name) print o._meta.verbose_name else: self.logAtion( ActionLog.ACTION_EDIT, model=model, content=o.__unicode__() if hasattr(o, '__unicode__') else None, objsid=o.id, model_cn=o._meta.verbose_name) return o.get_json()