def joinData(cls, comments): single = False if type(comments) != list: single = True comments = [comments] authorIds = [comment["shiftAuthor"] for comment in comments] shiftIds = [comment["shiftId"] for comment in comments] userIds = [comment["createdBy"] for comment in comments] users = core.fetch(keys=userIds) authors = core.fetch(keys=authorIds) shifts = core.fetch(core.connect("shiftspace/shared"), keys=shiftIds) commentCounts = core.fetch(core.connect("shiftspace/shared"), view=Comment.count_by_shift, keys=shiftIds) for i in range(len(comments)): if (authors[i]): comments[i]["authorName"] = authors[i]["userName"] comments[i]["userName"] = users[i]["userName"] comments[i]["gravatar"] = users[i]["gravatar"] comments[i]["createdStr"] = utils.pretty_date(utils.futcstr(comments[i]["created"])) comments[i]["commentCount"] = commentCounts[i] comments[i]["space"] = shifts[i]["space"] comments[i]["href"] = shifts[i]["href"] comments[i]["domain"] = shifts[i]["domain"] if single: return comments[0] else: return comments
def read(cls, id, userId=None): db = core.connect("shiftspace/shared") theMessage = Message.load(db, id) if theMessage == None and userId: db = core.connect(SSUser.messagesDb(userId)) theMessage = Message.load(db, id) return Message.joinData(theMessage, userId)
def delete(self): from server.models.ssuser import SSUser db = core.connect(SSUser.privateDb(self.createdBy)) if db.get(self.id): del db[self.id] else: db = core.connect(SSUser.publicDb(self.createdBy)) if db.get(self.id): del db[self.id] core.replicate(db.name, "shiftspace/shared")
def delete(self): from server.models.permission import Permission server = core.sharedServer() # delete the metadata db = core.connect() del db[self.id] # delete the group database del server[Group.db(self.id)] # delete all permissions [perm.delete() for perm in core.objects(Permission.by_group(core.connect(), key=self.id))]
def create(cls, userId, shiftId, text, subscribe=False): from server.models.ssuser import SSUser from server.models.shift import Shift from server.models.message import Message # first try the public feed theShift = Shift.load(core.connect("shiftspace/shared"), shiftId) shiftAuthor = SSUser.load(core.connect(), theShift.createdBy) theUser = SSUser.load(core.connect(), userId) server = core.sharedServer() # create the comment db if necessary dbexists = True if not theShift.hasThread(): server.create(Comment.db(shiftId)) dbexists = False # get the db db = core.connect(Comment.db(shiftId)) # if db just created, sync the views and subscribe shift author if not dbexists: Comment.by_created.sync(db) Comment.all_subscribed.sync(db) shiftAuthor.subscribe(theShift) # subscribe the user making the comment if not theUser.isSubscribed(theShift) and subscribe: theUser.subscribe(theShift) # create comment and comment stub json = { "createdBy": userId, "shiftId": shiftId, "shiftAuthor": theShift.createdBy, "text": text, } newComment = Comment(**utils.clean(json)) newComment.store(db) subscribers = theShift.subscribers() # make a private copy # TODO: need to think about the implications of a private copy here - David newComment.copyTo(SSUser.privateDb(theUser.id)) # send each subscriber a message if len(subscribers) > 0: # TODO: needs to be optimized with a fast join - David for subscriber in subscribers: if subscriber != userId: astr = ((subscriber == theShift.createdBy) and "your") or ("%s's" % shiftAuthor.userName) json = { "fromId": userId, "toId": subscriber, "title": "%s just commented on %s shift!" % (theUser.userName, astr), "text": "%s just commented on %s shift!" % (theUser.userName, astr), "meta": "comment" } Message.create(**utils.clean(json)) # TODO: don't replicate if peer - David 11/21/09 core.replicate(Comment.db(shiftId), "shiftspace/shared") return newComment
def create(cls, userId, shiftId, text, subscribe=False): from server.models.ssuser import SSUser from server.models.shift import Shift from server.models.message import Message # first try the public feed theShift = Shift.load(core.connect("shiftspace/shared"), shiftId) shiftAuthor = SSUser.load(core.connect(), theShift.createdBy) theUser = SSUser.load(core.connect(), userId) server = core.server() # create the comment db if necessary dbexists = True if not theShift.hasThread(): server.create(Comment.db(shiftId)) dbexists = False # get the db db = core.connect(Comment.db(shiftId)) # if db just created, sync the views and subscribe shift author if not dbexists: Comment.by_created.sync(db) Comment.all_subscribed.sync(db) shiftAuthor.subscribe(theShift) # subscribe the user making the comment if not theUser.isSubscribed(theShift) and subscribe: theUser.subscribe(theShift) # create comment and comment stub json = { "createdBy": userId, "shiftId": shiftId, "shiftAuthor": theShift.createdBy, "text": text, } newComment = Comment(**utils.clean(json)) newComment.store(db) subscribers = theShift.subscribers() # make a private copy # TODO: need to think about the implications of a private copy here - David newComment.copyTo(SSUser.privateDb(theUser.id)) # send each subscriber a message if len(subscribers) > 0: # TODO: needs to be optimized with a fast join - David for subscriber in subscribers: if subscriber != userId: astr = ((subscriber == theShift.createdBy) and "your") or ("%s's" % shiftAuthor.userName) json = { "fromId": userId, "toId": subscriber, "title": "%s just commented on %s shift!" % (theUser.userName, astr), "text": "%s just commented on %s shift!" % (theUser.userName, astr), "meta": "comment" } Message.create(**utils.clean(json)) # TODO: don't replicate if peer - David 11/21/09 core.replicate(Comment.db(shiftId), "shiftspace/shared") return newComment
def info(self): from server.models.follow import Follow from server.models.shift import Shift # TODO: use the bulk API - David 12/10/09 result = {} db = core.connect() shared = core.connect("shiftspace/shared") result["followerCount"] = core.value(Follow.followers_count(db, key=self.id)) or 0 result["followingCount"] = core.value(Follow.following_count(db, key=self.id)) or 0 result["publishedShiftCount"] = core.value(Shift.count_by_user_and_published(shared, key=self.id)) or 0 return result
def read(cls, id, userId=None): from server.models.ssuser import SSUser if userId != None: # then try the user public db = core.connect(SSUser.publicDb(userId)) theShift = Shift.load(db, id) if not theShift: # then user private db = core.connect(SSUser.privateDb(userId)) theShift = Shift.load(db, id) else: theShift = Shift.load(core.connect("shiftspace/shared"), id) if theShift: return Shift.joinData(theShift, theShift.createdBy)
def info(self): from server.models.follow import Follow from server.models.shift import Shift # TODO: use the bulk API - David 12/10/09 result = {} db = core.connect() shared = core.connect("shiftspace/shared") result["followerCount"] = core.value( Follow.followers_count(db, key=self.id)) or 0 result["followingCount"] = core.value( Follow.following_count(db, key=self.id)) or 0 result["publishedShiftCount"] = core.value( Shift.count_by_user_and_published(shared, key=self.id)) or 0 return result
def network_connect(iface, ssid, lat, lng, passkey=None): """ connect to a network :param iface: network interface :param ssid: network name :param lat: latitude :param lng: longitude :param passkey: authentication passphrase :return: JSON response """ core.connect(iface, ssid, passkey, _get_db(), float(lat), float(lng)) return jsonify(message='connected {}:{}'.format(iface, ssid), code=200)
def delete(userName): """ Delete a user. """ db = core.connect() id = idForName(userName) # delete all the user's events userEvents = utils.ids(event.eventsForUser(id)) for eventId in userEvents: del db[eventId] # delete the user's public and message streams userStreams = utils.ids(stream.streamsForObjectRef(ref(id))) for streamId in userStreams: del db[streamId] # delete all of the remaining user's streams which have no events streamIds = utils.ids(stream.streamsByCreator(id)) [stream.delete(streamId) for streamId in streamIds if len(event.eventsForStream(streamId)) == 0] # delete all the user's shifts deleteShifts(id) # delete all the user's permission userPerms = utils.ids(permission.permissionsForUser(id)) for permId in userPerms: del db[permId] # delete the user del db[id]
def buttonConnect(self, widget): print "buttonConnect was clicked" try: db_name = app_gui.ServerSelector.get_active_text() result, worlds = core.connect(db_name) update.labelConnect(result) #dim connect and worldselector app_gui.buttonConnect.set_sensitive(False) app_gui.ServerSelector.set_sensitive(False) #undim disconnect and Apply app_gui.buttonDisconnect.set_sensitive(True) app_gui.buttonApply.set_sensitive(True) app_gui.buttonChat.set_sensitive(True) app_gui.buttonMM.set_sensitive(True) app_gui.buttonAddWorld.set_sensitive(True) app_gui.buttonEnaCommand.set_sensitive(True) #load worlds to selectbox update.worldSelector(worlds) self.loadChat("derp") self.loadMM("derp") except: raise update.labelConnect("FAILED to connect to MYSQL")
def setPrivilege(self, aUser, level): from server.models.permission import Permission thePermission = Permission.readByUserAndGroup(aUser.id, self.id) if thePermission and level > 0: db = core.connect() thePermission.level = level thePermission.store(db)
def join(self, aUser): from server.models.permission import Permission thePermission = Permission.readByUserAndGroup(aUser.id, self.id) if thePermission and thePermission.level == 0: db = core.connect() thePermission.level = 2 thePermission.store(db)
def create(cls, userId, groupId, otherId, level): from server.models.ssuser import SSUser from server.models.group import Group db = core.connect() if not groupId: raise MissingGroupError if not userId: raise MissingCreatorError if Permission.readByUserAndGroup(otherId, groupId): raise PermissionAlreadyExistsError adminable = [ row.value for row in Permission.by_adminable(db, key=userId).rows ] allowed = groupId in adminable if not allowed: theUser = SSUser.read(userId) allowed = theUser.isAdmin() if not allowed: theGroup = Group.read(groupId) allowed = theUser.isOwnerOf(theGroup) if not allowed: raise CreateEventPermissionError json = { "createdBy": userId, "userId": otherId, "groupId": groupId, "level": level } newPermission = Permission(**utils.clean(json)) newPermission.store(db) return newPermission
def updateForUser(cls, userId, groupId, level): db = core.connect() perm = core.value( Permission.by_user_and_group(db, key=[userId, groupId])) perm.level = level perm.store(db) return perm
def delete(self): """ Delete a single comment. """ from server.models.ssuser import SSUser db = core.connect(SSUser.privateDb(self.createdBy)) try: del db[self.id] except: pass db = core.connect(Comment.db(self.shiftId)) try: del db[self.id] except: pass core.replicate(Comment.db(self.shiftId), "shiftspace/shared")
def update(self, fields, updateDbs=True): from server.models.ssuser import SSUser if fields.get("content"): self.content = fields.get("content") if fields.get("summary"): self.summary = self.content["summary"] = fields.get("summary") if fields.get("broken"): self.broken = fields.get("broken") if fields.get("dbs"): self.dbs = list(set(self.dbs.extend(fields.get("dbs")))) self.modified = datetime.now() # update the correct user db if self.publishData.private: db = SSUser.privateDb(self.createdBy) else: db = SSUser.publicDb(self.createdBy) self.store(core.connect(db)) core.replicate(db, "shiftspace/shared") # update followers and groups if updateDbs: for db in self.publishData.dbs: dbtype, dbid = db.split("/") if dbtype == "group": from server.models.group import Group Group.read(dbid).updateShift(self) return Shift.joinData(self, self.createdBy)
def canRead(id, userId): """ Check if a user can read a shift. The user must have either: 1. Created the shift 2. The shift must be published and public 3. If the user is subscribed to a stream the shift is on. 4. If the shift is published to the user's private stream. Parameters: id - a shift id. Returns: bool. """ db = core.connect() theShift = db[id] if user.isAdmin(userId): return True if theShift["createdBy"] == userId: return True if theShift["publishData"]["draft"]: return False theUser = db[userId] if not theShift["publishData"]["private"]: return True if theUser["privateStream"] in theShift["publishData"]["streams"]: return True shiftStreams = theShift["publishData"]["streams"] readableStreams = permission.readableStreams(userId) allowed = set(shiftStreams).intersection(readableStreams) return len(allowed) > 0
def comments(self, start=None, end=None, limit=25): from server.models.comment import Comment if not self.hasThread(): return [] db = core.connect(Comment.db(self.id)) return Comment.joinData( core.objects(Comment.by_created(db, limit=limit)))
def create(data, notifData=None): """ Create an event document. Parameters: data - a dictionary of the event data. notifData - a dictionary of data to be sent in the notifications. Returns: an event document. """ db = core.connect() theTime = utils.utctime() data["created"] = theTime data["modified"] = theTime # create notification events notifications = stream.notifications(data["streamId"]) for userId in notifications: create({ "createdBy": data.get("createdBy"), "displayString": data.get("displayString"), "streamId": user.messageStream(userId), "unread": True, "content": data.get("content") }) newEvent = schema.event() newEvent.update(data) newEvent["type"] = "event" return db.create(newEvent)
def create(cls, userJson): """ Create a new user document. Also creates the three databases (user/public, user/private, user/inbox) to allow for peer-to-peer distribution. Parameters: userJson - a dictionary of fields and their values. """ from server.models.shift import Shift from server.models.message import Message server = core.server() db = core.connect() if userJson.get("passwordVerify"): del userJson["passwordVerify"] if userJson.get("password"): userJson['password'] = utils.md5hash(userJson['password']) if userJson.get("email"): hashedEmail = utils.md5hash(userJson["email"]) userJson["gravatar"] = "http://www.gravatar.com/avatar/%s?s=32" % hashedEmail userJson["gravatarLarge"] = "http://www.gravatar.com/avatar/%s?s=60" % hashedEmail newUser = SSUser(**utils.clean(userJson)) newUser.store(db) # user's public shifts, will be replicated to shiftspace and user/feed server.create(SSUser.publicDb(newUser.id)) # all of the user's shifts as well as subscribed content server.create(SSUser.privateDb(newUser.id)) # all of the user's messages server.create(SSUser.messagesDb(newUser.id)) # sync views Message.by_created.sync(server[SSUser.messagesDb(newUser.id)]) return newUser
def create(cls, follower, followee): from server.models.message import Message existing = Follow.read(follower, followee) if existing: return existing newFollow = Follow( follower=follower.id, followee=followee.id, ) newFollow.id = Follow.makeId(follower.id, followee.id) newFollow.store(core.connect()) json = { "fromId": "shiftspace", "toId": followee.id, "title": "%s has started following your shifts!" % (follower.userName), "text": "%s has started following your shifts!" % (follower.userName), "meta": "follow", "content": { "followerId": follower.id } } Message.create(**json) return newFollow
def shifts(self, start=None, end=None, limit=25, filter=False, query=None): from server.models.shift import Shift db = core.connect("shiftspace/shared") if not filter: if not start: start = [self.id] if not end: end = [self.id, {}] results = Shift.by_user_and_created(db, limit=limit) return Shift.joinData(core.objects(results[start:end])) else: lucene = core.lucene() queryString = "createdBy:%s" % self.id theFilter = core.dictToQuery(query) if theFilter: queryString = queryString + " AND " + theFilter try: print ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" print queryString rows = lucene.search(db, "shifts", q=queryString, include_docs=True, sort="\modified") except Exception, err: print err return [] shifts = [row["doc"] for row in rows] return Shift.joinData(shifts, self.id)
def joinData(cls, messages, userId=None): single = False if type(messages) != list: single = True messages = [messages] ids = [message["_id"] for message in messages] userIds = [message["fromId"] for message in messages] users = core.fetch(keys=userIds) if userId: readStatuses = core.fetch(db=core.connect("shiftspace/shared"), keys=[Message.makeReadId(id, userId) for id in ids]) for i in range(len(messages)): if (userIds[i]): if (userIds[i] == 'shiftspace'): messages[i]["gravatar"] = "images/default.png" messages[i]["userName"] = '******' else: messages[i]["gravatar"] = (users[i]["gravatar"] or "images/default.png") messages[i]["userName"] = users[i]["userName"] messages[i]["modifiedStr"] = utils.pretty_date(utils.futcstr(messages[i]["modified"])) if userId: messages[i]["read"] = (readStatuses[i] != None) if single: return messages[0] else: return messages
def joinData(cls, shifts, userId=None): from server.models.favorite import Favorite from server.models.comment import Comment if core._local: db = core.conect("user/%s" % core._local_id) else: db = core.connect("shiftspace/shared") single = False if type(shifts) != list: single = True shifts = [shifts] ids = [shift['_id'] for shift in shifts] favIds = [Favorite.makeId(userId, shiftId) for shiftId in ids] isFavorited = core.fetch(db, keys=favIds) favCounts = core.fetch(db, view=Favorite.count_by_shift, keys=ids) commentCounts = core.fetch(db, view=Comment.count_by_shift, keys=ids) userIds = [shift["createdBy"] for shift in shifts] users = core.fetch(keys=userIds) for i in range(len(shifts)): shifts[i]["favorite"] = (isFavorited[i] != None) shifts[i]["favoriteCount"] = favCounts[i] shifts[i]["commentCount"] = commentCounts[i] shifts[i]["gravatar"] = (users[i]["gravatar"] or "images/default.png") shifts[i]["userName"] = users[i]["userName"] shifts[i]["createdStr"] = utils.pretty_date(utils.futcstr(shifts[i]["created"])) shifts[i]["modifiedStr"] = utils.pretty_date(utils.futcstr(shifts[i]["modified"])) if single: return shifts[0] else: return shifts
def create(cls, userId, groupId, otherId, level): from server.models.ssuser import SSUser from server.models.group import Group db = core.connect() if not groupId: raise MissingGroupError if not userId: raise MissingCreatorError if Permission.readByUserAndGroup(otherId, groupId): raise PermissionAlreadyExistsError adminable = [row.value for row in Permission.by_adminable(db, key=userId).rows] allowed = groupId in adminable if not allowed: theUser = SSUser.read(userId) allowed = theUser.isAdmin() if not allowed: theGroup = Group.read(groupId) allowed = theUser.isOwnerOf(theGroup) if not allowed: raise CreateEventPermissionError json = { "createdBy": userId, "userId": otherId, "groupId": groupId, "level": level } newPermission = Permission(**utils.clean(json)) newPermission.store(db) return newPermission
def unreadCount(self): from server.models.message import Message db = core.connect("shiftspace/shared") syscount = core.value(Message.system_count(db, key=self.id)) or 0 tocount = core.value(Message.count_by_user(db, key=self.id)) or 0 readcount = core.value(Message.read_count_by_user(db, key=self.id)) or 0 return (syscount+tocount)-readcount
def getById(id): """ Get a user by id. Parameters: id - a user id. """ db = core.connect() return db[id]
def delete(id): """ Delete a event document. Parameters: id - an event id. """ db = core.connect() del db[id]
def adminable(cls, userId, dbname=True): from server.models.group import Group db = core.connect() ids = core.values(Permission.by_adminable(db, key=userId)) if dbname: return [Group.db(id) for id in ids] else: return ids
def delete(id): """ Delete a permission document. Parameters: id - a permission id. """ db = core.connect() del db[id]
def unreadCount(self): from server.models.message import Message db = core.connect("shiftspace/shared") syscount = core.value(Message.system_count(db, key=self.id)) or 0 tocount = core.value(Message.count_by_user(db, key=self.id)) or 0 readcount = core.value(Message.read_count_by_user(db, key=self.id)) or 0 return (syscount + tocount) - readcount
def create(cls, shiftJson): from server.models.ssuser import SSUser newShift = Shift(**utils.clean(shiftJson)) createdBy = newShift.createdBy db = core.connect(SSUser.privateDb(createdBy)) newShift.domain = utils.domain(newShift.href) newShift.store(db) core.replicate(SSUser.privateDb(createdBy), "shiftspace/shared") return Shift.joinData(newShift, newShift.createdBy)
def comments(self, start=None, end=None, limit=25): from server.models.comment import Comment db = core.connect("shiftspace/shared") if not start: start = [self.id] if not end: end = [self.id, {}] results = Comment.by_user_and_created(db, limit=limit) return core.objects(results[start:end])
def followers(self, start=None, end=None, limit=25): from server.models.follow import Follow if not start: start = [self.id] if not end: end = [self.id, {}] results = Follow.followers_by_created(core.connect(), limit=limit) userIds = core.values(results[start:end]) return core.fetch(keys=userIds)
def updateShift(self, aShift): from server.models.ssuser import SSUser author = SSUser.read(aShift.createdBy) if author.isMemberOf(self): grpdb = Group.db(self.id) aShift.updateIn(grpdb) else: db = core.connect() raise NotAMemberError("%s is not a member of %s" % (author.userName, self.longName))
def groups(cls, start=None, end=None, limit=25, userId=None): results = Group.by_visible_and_created(core.connect(), limit=25) if start and not end: return Group.joinData(core.objects(results[start:]), userId) if not start and end: return Group.joinData(core.objects(results[:end]), userId) if start and end: return Group.joinData(core.objects(results[start:end]), userId) return Group.joinData(core.objects(results), userId)
def groups(self, start=None, end=None, limit=25): from server.models.permission import Permission db = core.connect() if not start: start = [self.id] if not end: end = [self.id, {}] results = Permission.readable_by_user_and_created(db, limit=limit) return Permission.joinData(core.objects(results[start:end]))
def joinData(cls, groups, userId=None): from server.models.permission import Permission db = core.connect() keys = [[userId, group.id] for group in groups] perms = core.fetch(db, view=Permission.by_user_and_group, keys=keys) for i in range(len(groups)): if perms[i]: groups[i]["level"] = perms[i]["level"] return groups
def main(): log = dl.log_api.conf_logger(__name__) config = configparser.ConfigParser() config.read('config.ini') service = build("customsearch", "v1", developerKey=config['Custom Search Engine']['key']) today = datetime.datetime.now() yesterday = format_date(today - datetime.timedelta(1)) hates = set(pd.read_csv('exclude_urls.csv')['URL'].values) db = core.connect() cse_searches = db['cse_searches'] with open('result.html', 'w') as html: html.write('<html><body>') df = pd.read_csv('keywords.csv') df = df[df['Flag'] == 'Use'] for term in df['Term']: query = 'python {}'.format(term) res = get_response(service, query, yesterday, today, config['Custom Search Engine']['cx']) html.write('<h1>{}</h1>'.format(query)) if res.get('items') is None: continue html.write('<ol>') for i in res['items']: if i['link'] in hates: continue # Avoid CVs with buzzwords if 'resume' in i['link']: continue if cse_searches.find_one(link=i['link']): continue if core.not_english(i['htmlSnippet']): log.debug('Not English: {0}'.format(i['htmlSnippet'])) continue html.write('<li>{0} <a href="{1}">{1}</a></li>'.format( i['htmlSnippet'], i['link'])) cse_searches.insert( dict(search_date=datetime.datetime.now(), html_snippet=i['htmlSnippet'], link=i['link'])) html.write('</ol>') html.write('</body></html>')
def subscribe(self, aShift): from server.models.comment import Comment db = core.connect(Comment.db(aShift.id)) if not self.isSubscribed(aShift): db.create({ "_id": "user:%s" % self.id, "shiftId": aShift.id, "type": "subscription", "userId": self.id, })
def sendMail(mail, configData): encd = encData(mail) url = configData['url'] if args.verbose: print('[*] Connectiing to the server.') req = connect(url, encd) if args.verbose: print('[*] Sending mail') if send(req): return True
def favorites(self, start=None, end=None, limit=25): from server.models.favorite import Favorite db = core.connect("shiftspace/shared") if not start: start = [self.id] if not end: end = [self.id, {}] results = Favorite.by_user_and_created(db, limit=limit) favs = core.objects(results[start:end]) return core.fetch(db, keys=[fav.shiftId for fav in favs])
async def process_start(message: types.Message, state: FSMContext): async with state.proxy() as data: data["start_bitcart"] = core.defaults["start_bitcart"] if message.text == "default" else message.text try: await bot.send_message(message.chat.id, "Starting installation, please wait...") kwargs = {key: data[key] for key in core.texts if key in data} kwargs["print_func"] = functools.partial(print, file=open(os.devnull, "w")) kwargs.pop("onedomain_mode", None) core.connect(**kwargs) if core.verify_install_bitcart(data["start_bitcart"]): await bot.send_message(message.chat.id, "Successfully started bitcart!") else: await bot.send_message(message.chat.id, "Successfully installed bitcart!") except Exception: await bot.send_message( message.chat.id, "Error connecting to server/installing.\n" + hcode(traceback.format_exc().splitlines()[-1]), ) data.state = None