def authenticate(self, request): email = request.user_email validate_email(email) team_key = request.team_key user = User.find_user_by_email(email, team_key) if team_key: team_secret = request.team_secret validate_team_secret(team_secret) display_name = request.display_name image_url = request.user_image_url if not user: user = User.insert_user(email=email, username=display_name, account_type=team_key, image_url=image_url) community = Community.getCommunityFromTeamKey(team_key) UserRole.insert(user, community) elif (display_name != user.display_name) or (image_url != user.image_url): User.update_user(user=user, email=email, username=display_name, account_type=team_key, image_url=image_url) if not Community.authenticate(team_key, md5(team_secret)): raise endpoints.UnauthorizedException("Authentication failed. Team key and secret are not matched.") elif user.auth_source == AuthSourceType.ANNO: password = request.password validate_password(password) if not user: raise endpoints.NotFoundException("Authentication failed. User account " + email + " doesn't exist.") if not User.authenticate(email, md5(password)): raise endpoints.UnauthorizedException("Authentication failed. Email and password are not matched.") else: raise endpoints.ForbiddenException("Account for '%s' is Google or Facebook OAuth account." % email) return UserMessage(id=user.key.id(), display_name=user.display_name)
def getCommunityApps(community_id=None, team_key=None, app_count=None): if community_id: community = Community.get_by_id(community_id) elif team_key: community = Community.query(Community.team_key == team_key).get() return community.apps[0:app_count] if app_count else community.apps
def community_teamkey_update(self, request): if not is_auth_user_admin(headers=self.request_state.headers): return ResponseMessage(success=False) Community.update_teamkey(request) new_user_team_token = update_user_team_token(headers=self.request_state.headers, team_key=request.new_team_key) return ResponseMessage(success=True, msg=json.dumps(new_user_team_token))
def getAppAndCommunity(message, user): if message.team_key: community = Community.getCommunityFromTeamKey(team_key=message.team_key) appinfo = getAppInfo(community.key.id()) elif message.app_name: appinfo = AppInfo.get(name=message.app_name, platform=message.platform_type) community = None if appinfo is None: appInfoMessage = AppInfoMessage(name=message.app_name, version=message.app_version, platform=message.platform_type) appinfo = AppInfo.insert(appInfoMessage) else: app_community = getCommunityForApp(id=appinfo.key.id()) if app_community and isMember(app_community, user): community = app_community elif message.community_name: community_id = Community.getCommunity(community_name=message.community_name).id community = Community.get_by_id(community_id) appinfo = getAppInfo(community_id) else: raise endpoints.BadRequestException("Please specify a community or app") return appinfo, community
def create_teams(): team_key = "" app_name = "" community_name = "" admin_user_email = "" other_users_email = [] app = AppInfo.query().filter(AppInfo.lc_name == app_name.lower()).get() if not app: appinfo_message = AppInfoMessage() appinfo_message.name = app_name app = AppInfo.insert(appinfo_message) community = Community.getCommunityFromTeamKey(team_key=team_key) if not community: community_message = CommunityMessage(name=community_name, team_key=team_key, team_secret=md5(community_name.lower())) community_message.user = UserMessage(user_email=admin_user_email) Community.insert(community_message) community = Community.getCommunityFromTeamKey(team_key=team_key) if community and app: if not app.key in community.apps: community.apps.append(app.key) community.put() for user_email in other_users_email: user = User.find_user_by_email(email=user_email, team_key=team_key) if not user: user = User.insert_user(user_email, account_type=team_key, image_url="") userrole = UserRole.insert(user, community)
def insert_user(self, request): if not is_auth_user_admin(headers=self.request_state.headers): return ResponseMessage(success=False) action_user = auth_user(self.request_state.headers) user = get_user_from_request(user_id=request.user_id, user_email=request.user_email, team_key=request.team_key) if not user: user = User.insert_user(request.user_email, username=request.user_display_name, account_type=request.team_key, auth_source=AuthSourceType.PLUGIN, password=md5(request.user_password), image_url=request.user_image_url or "") community = Community.getCommunityFromTeamKey(request.team_key) if request.team_key else Community.get_by_id(request.community_id) role = request.role if request.role else UserRoleType.MEMBER resp = None if user and community: circle = 0 for circle_value, circle_name in community.circles.iteritems(): if circle_name == request.circle: circle = int(circle_value) resp = UserRole.insert(user, community, role, circle) send_added_user_email(community.name, user.display_name, "added", action_user.display_name, community.team_hash) return ResponseMessage(success=True if resp else False)
def set_communities(self): communities = [] for community in Community.get_list(): if community.get_county_id() == self.get_id( ) and community.get_province_id() == self.get_province_id(): communities.append(community) return communities
def update_user(self, request): action_auth_user = auth_user(self.request_state.headers) if not (action_auth_user.user_email == request.user_email): if not is_auth_user_admin(action_auth_user=action_auth_user): return ResponseMessage(success=False) user = get_user_from_request(user_id=request.user_id, user_email=request.user_email, team_key=request.team_key) if user: user.display_name = request.user_display_name or user.display_name user.password = md5(request.user_password) if request.user_password else user.password user.image_url = request.user_image_url or user.image_url or "" user.put() community = Community.getCommunityFromTeamKey(request.team_key) if request.team_key else Community.get_by_id(request.community_id) resp = None if user and community: circle = 0 for circle_value, circle_name in community.circles.iteritems(): if circle_name == request.circle: circle = int(circle_value) resp = UserRole.edit(user, community, request.role, circle) return ResponseMessage(success=True if resp else False)
def fetch_list_pages(db, city_index): logging.info('抓取区域列表') areas_obj = db.query(Area).filter(Area.city_index == city_index, Area.insert == 0).all() for area in areas_obj: page = 1 while True: logging.info(f'正在抓取第{page}页') api_url = f'http://m.58.com/xiaoquweb/getXiaoquList/?city={area.area_index}&page={page}' res = requests.get(api_url, headers=config.config.headers, timeout=5) res.raise_for_status() res_json_obj = json.loads(res.content) if res_json_obj['code'] == "0": page_dto = res_json_obj['data']['pageDTO'] info_list = res_json_obj['data']['infoList'] if page_dto['currentPageNo'] > page_dto['totalPage']: break for community in info_list: add_community = Community(city_index, area.area_index, community) db.add(add_community) logging.info('新增1条小区信息') page += 1 area.insert = 1 db.commit() logging.info('抓取区域列表数据完毕') db.close()
def reset_team_secret(self, request): if not is_auth_user_admin(headers=self.request_state.headers): return CommunityValueMessage(secret=None) secret = Community.reset_team_secret(request.team_key) new_user_team_token = update_user_team_token(headers=self.request_state.headers, team_secret=secret) return CommunityValueMessage(secret=secret, user_team_token=json.dumps(new_user_team_token))
def community_users(self, request): user = auth_user(self.request_state.headers) community_userroles = [] if user: community = Community.getCommunityFromTeamKey(request.account_type) if community and (UserRole.getCircleLevel(user, community) > 0): community_userroles = UserRole.query().filter(ndb.AND(UserRole.community == community.key, UserRole.circle_level > 0) ).fetch(projection=[UserRole.user]) users = [] for community_userrole in community_userroles: current_user = community_userrole.user.get() users.append(UserMessage(id=current_user.key.id(), user_email=current_user.user_email, display_name=current_user.display_name, image_url=current_user.image_url)) # removing auth_user [ users.remove(user_info) for user_info in users if user_info.user_email == user.user_email ] # sorting users alphabetically users = sorted(users, key=lambda user_info: user_info.display_name.lower()) return UserListMessage(user_list=users)
def dashboard_authenticate(self, request): email = request.user_email password = request.password team_key = request.team_key accounts = [] if email and password: users = User.get_all_user_by_email(email, md5(password), team_key=team_key) for user in users: if user: team_key = user.account_type team = Community.getCommunityFromTeamKey(team_key) if team: userTeamToken = get_user_team_token(email, password, team_key, team.team_secret, user.display_name, user.image_url) account = AccountAuthenticateMessage(display_name=user.display_name, image_url=user.image_url, team_name=team.name, team_key=team_key, team_hash=team.team_hash, user_team_token=json.dumps(userTeamToken), role=UserRole.getRole(user, team)) accounts.append(account) return AccountAuthenticateListMessage(authenticated=True if len(accounts) else False, account_info=accounts)
def get_circle_users(self, request): roles = [UserRoleType.MEMBER, UserRoleType.ADMIN] community = Community.getCommunityFromTeamKey(request.team_key) circle_list_message = [] for circle_value, circle_name in community.circles.items(): circle_message = CommunityCircleMembersMessage() circle_message.circle_name = circle_name circle_message.users = [] for userrole in UserRole.getUsersByCircle(community.key, int(circle_value)): user = userrole.user.get() if user and (user.account_type == community.team_key): if user.user_email.split("@")[1] == "devnull.usersource.io": break user_message = UserAdminMasterMessage() user_message.display_name = user.display_name user_message.user_email = user.user_email user_message.password_present = True if user.password else False user_message.role = userrole.role user_message.image_url = user.image_url circle_message.users.append(user_message) circle_list_message.append(circle_message) return CommunityCircleMembersListMessage(circle_list=circle_list_message, roles=roles)
def to_response_message(self, user, list_message=True): """ Convert anno model to AnnoResponseMessage. """ user_message = None if self.creator is not None: user_info = self.creator.get() user_message = UserMessage(display_name=user_info.display_name, image_url=user_info.image_url) app = self.app.get() if self.app else None app_name = app.name if app else self.app_name app_icon_url = app.icon_url if app else None app_version = app.version if app else self.app_version if list_message: anno_read_status = False if user: from model.userannostate import UserAnnoState anno_read_status = UserAnnoState.is_read(user, self) last_activity_user = UserAnnoState.last_activity_user(self) circle_level_value = None if (self.community and self.circle_level > 0): circle_level_value = Community.getCircleLevelValue(self.community, self.circle_level) anno_message = AnnoResponseMessage(id=self.key.id(), anno_text=self.anno_text, anno_type=self.anno_type, app_name=app_name, app_icon_url=app_icon_url, created=self.created, creator=user_message, last_update_time=self.last_update_time, last_activity=self.last_activity, last_update_type=self.last_update_type, anno_read_status=anno_read_status, last_activity_user=last_activity_user, circle_level_value=circle_level_value ) else: anno_message = AnnoResponseMessage(id=self.key.id(), anno_text=self.anno_text, anno_type=self.anno_type, level=self.level, device_model=self.device_model, app_name=app_name, app_version=app_version, app_icon_url=app_icon_url, os_name=self.os_name, os_version=self.os_version, created=self.created, creator=user_message, draw_elements=self.draw_elements, screenshot_is_anonymized=self.screenshot_is_anonymized, vote_count=self.vote_count, flag_count=self.flag_count, followup_count=self.followup_count, last_update_time=self.last_update_time, last_activity=self.last_activity, last_update_type=self.last_update_type) return anno_message
def create_community_circle(): communities = Community.query().filter(Community.team_key != None).fetch() for community in communities: community.circles = { CircleValue.CONTRIBUTOR : CircleType.CONTRIBUTOR, CircleValue.BETA_TESTER : CircleType.BETA_TESTER, CircleValue.ALPHA_TESTER : CircleType.ALPHA_TESTER, CircleValue.DEVELOPER : CircleType.DEVELOPER } community.plan = PlanType.BASIC community.put()
def edit_welcome_msg(self, request): community = Community.get_by_id(request.id) if community: community.welcome_msg = request.welcome_msg community.put() return ResponseMessage(success=True) else: return ResponseMessage(success=False)
def list_community(self, request): community_value_list = [] if request.team_hash == "us3rs0urc3": community_list = Community.query().filter(Community.team_key != None).fetch() for community in community_list: community_value_list.append(CommunityValueMessage(name=community.name, key=community.team_key, secret=community.team_secret)) return CommunityValueListMessage(teams=community_value_list)
def is_auth_user_admin(headers=None, action_auth_user=None): is_admin = False user = auth_user(headers) if (not action_auth_user and headers) else action_auth_user if user: community = Community.getCommunityFromTeamKey(user.account_type) if community: role = UserRole.getRole(user, community) is_admin = role == UserRoleType.ADMIN return is_admin
def auth_user(headers): current_user = get_endpoints_current_user(raise_unauthorized=False) user = None if current_user is None: credential_pair = get_credential(headers) signinMethod = SignInMethod.ANNO team_key = None team_secret = None display_name = None image_url = None if len(credential_pair) == 2: email, password = credential_pair elif len(credential_pair) == 5: signinMethod, email, password, team_key, team_secret = credential_pair else: signinMethod, email, password, team_key, team_secret, display_name, image_url = credential_pair validate_email(email) user = User.find_user_by_email(email, team_key) if signinMethod == SignInMethod.ANNO: User.authenticate(email, md5(password)) elif signinMethod == SignInMethod.PLUGIN: if not Community.authenticate(team_key, team_secret): raise endpoints.UnauthorizedException("Incorrect team key or secret") display_name = unicode(display_name, "utf-8", "ignore") if not user: user = User.insert_user(email=email, username=display_name, account_type=team_key, image_url=image_url) community = Community.getCommunityFromTeamKey(team_key) UserRole.insert(user, community) elif (display_name and display_name != user.display_name) or (image_url and image_url != user.image_url): User.update_user(user=user, email=email, username=display_name, account_type=team_key, image_url=image_url) else: user = User.find_user_by_email(current_user.email()) if user is None: raise endpoints.UnauthorizedException("Oops, something went wrong. Please try later.") return user
def migrate_photo_time_annos(cursor=None): team_key = 'us.orbe.Reko-Album' phototime_app = AppInfo.query().filter(AppInfo.lc_name == 'phototime').get() phototime_community = Community.getCommunityFromTeamKey(team_key=team_key) anno_list = Anno.query().filter(Anno.app == phototime_app.key).fetch() for anno in anno_list: anno.community = phototime_community.key user_email = anno.creator.get().user_email anno.creator = User.find_user_by_email(email=user_email, team_key=team_key).key anno.put()
def community_user_list(cls, community_id=None, community_key=None, only_managers=False): if (not community_key) and community_id: community_key = Community.get_by_id(community_id).key users = [] if community_key: query = cls.query().filter(cls.community == community_key) if only_managers: query = query.filter(cls.role == UserRoleType.MANAGER) users = query.fetch() return users
def edit_user_role(self, request): user = get_user_from_request(user_id=request.user_id, user_email=request.user_email) community = Community.get_by_id(request.community_id) resp = None if community: if user: resp = UserRole.edit(user, community, request.role) elif request.include_invite: resp = Invite.change_user_role(request.user_email, community, request.role) return ResponseMessage(success=True if resp else False)
def delete_user(self, request): user = get_user_from_request(user_id=request.user_id, user_email=request.user_email) community = Community.get_by_id(request.community_id) success = False if community: if user: UserRole.delete(user, community) success = True elif request.include_invite: Invite.delete(request.user_email, community) success = True return ResponseMessage(success=success)
def get_all_teams_by_email(cls, email): from model.community import Community users = cls.query().filter(cls.user_email == email).fetch() accounts = [] for user in users: if user: team_key = user.account_type team = Community.getCommunityFromTeamKey(team_key) if team: account = AccountAuthenticateMessage(team_name=team.name, team_key=team_key) accounts.append(account) return accounts
def add_teamhash(cursor=None): community_list, cursor, more = Community.query().fetch_page(BATCH_SIZE, start_cursor=cursor) community_update_list = [] for community in community_list: if (not community.team_hash) and community.team_key: community.team_hash = md5(community.team_key)[-8:] community_update_list.append(community) if len(community_update_list): ndb.put_multi(community_update_list) if more: add_teamhash(cursor=cursor)
def getCommunityForApp(id=None, app_name=None): if id: app = AppInfo.get_by_id(id) elif app_name: app = AppInfo.get(name=app_name) app_community = None if app: communities = Community.query().fetch() for community in communities: if app.key in community.apps: app_community = community break return app_community
def add_community(self): ''' Create list_of_community built of Community objects. Except Vallue Error when data isn't integer. ''' first_index_community = 0 for row in Territory.list_of_territory: try: index_community = int(row[2]) if index_community != first_index_community and self.number == int( row[1]): community = Community(row[4], row[5], index_community) self.list_of_community.append(community) first_index_community = index_community except ValueError: continue
def get_admin_master_data(self, request): communities_message = [] query = Community.query().filter(Community.team_secret != None) if request.team_key: query = query.filter(Community.team_key == request.team_key) communities = query.fetch() for community in communities: community_message = CommunityAdminMasterMessage() community_message.community_name = community.name community_message.team_key = community.team_key community_message.team_secret = community.team_secret community_message.team_hash = community.team_hash community_message.plan = community.plan app = community.apps[0].get() if app: community_message.app_name = app.name community_message.app_icon = app.icon_url community_message.users = [] if request.get_user_list: for userrole in UserRole.community_user_list(community_key=community.key): user = userrole.user.get() if user and (user.account_type == community.team_key): if user.user_email.split("@")[1] == "devnull.usersource.io": break user_message = UserAdminMasterMessage() user_message.display_name = user.display_name user_message.user_email = user.user_email user_message.password_present = True if user.password else False user_message.role = userrole.role user_message.image_url = user.image_url if community.circles: user_message.circle = community.circles.get(str(userrole.circle_level)) community_message.users.append(user_message) communities_message.append(community_message) return CommunityAdminMasterListMessage(communities=communities_message)
def anno_list(self, request): """ Exposes an API endpoint to retrieve a list of anno. """ user = auth_user(self.request_state.headers) limit = 10 if request.limit is not None: limit = request.limit is_plugin = request.is_plugin or False curs = None if request.cursor is not None: try: curs = Cursor(urlsafe=request.cursor) except BadValueError: raise endpoints.BadRequestException('Invalid cursor %s.' % request.cursor) select_projection = None if request.select is not None: select_projection = request.select.split(',') if request.query_type == AnnoQueryType.CREATED: return Anno.query_by_app_by_created(request.app, limit, select_projection, curs, user) elif request.query_type == AnnoQueryType.VOTE_COUNT: return Anno.query_by_vote_count(request.app, user) elif request.query_type == AnnoQueryType.FLAG_COUNT: return Anno.query_by_flag_count(request.app, user) elif request.query_type == AnnoQueryType.ACTIVITY_COUNT: return Anno.query_by_activity_count(request.app, user) elif request.query_type == AnnoQueryType.LAST_ACTIVITY: return Anno.query_by_last_activity(request.app, user) elif request.query_type == AnnoQueryType.COUNTRY: return Anno.query_by_country(request.app, user) elif request.query_type == AnnoQueryType.COMMUNITY: community = Community.get_by_id(request.community) return Anno.query_by_community(community, limit, select_projection, curs, user) elif request.query_type == AnnoQueryType.APP: app = AppInfo.get(request.app) return Anno.query_by_app(app, limit, select_projection, curs, user) else: return Anno.query_by_page(limit, select_projection, curs, user, is_plugin)
def load_file(): if not os.path.isfile('static/malopolska.csv'): raise FileNotFoundError else: with open('static/malopolska.csv', 'r') as csvfile: for row in csvfile: row = row.strip() row = row.split('\t') if row[0].isdigit(): data = (row[4], row[5]) if not row[1]: voivodeship = Voivodeship(row[0], *data) elif not row[2]: county = County(row[1], *data) voivodeship.add_county(county) elif row[3]: community = Community(row[2], row[3], *data) county.add_community(community)
def create(cls, message, creator): community_name = "" community = Community.get_by_id(message.community) invitation_created = cls.query(ndb.AND(cls.email == message.email, cls.community == community.key) ).count() if community and not invitation_created: if message.role is None: message.role = UserRoleType.MEMBER if message.invite_msg is None: message.invite_msg = community.welcome_msg invite_hash = str(uuid.uuid4()) entity = cls(name=message.name, email=message.email, role=message.role, invite_msg=message.invite_msg, invite_hash=invite_hash, community=community.key, creator=creator.key) entity.put() if entity: community_name = community.name return community_name
def community_insert(self, request): resp = Community.insert(request) return ResponseMessage(success=True, msg=resp)
def community_update(self, request): if not is_auth_user_admin(headers=self.request_state.headers): return ResponseMessage(success=False) Community.update(request) return ResponseMessage(success=True)
def list_by_community(cls, community_id): return cls.query(cls.community == Community.get_by_id(community_id).key).fetch()
def all_units_list(self): data = County.get_list() + Community.get_list() + Town.get_list() + \ RuralArea.get_list() + Delegacy.get_list() return data