def invite(request, rawEmailIds): authinfo = request.getSession(IAuthInfo) emailIds = [] expr = re.compile(', *') for commaSeparated in rawEmailIds: emailIds.extend(expr.split(commaSeparated)) myId = authinfo.username myOrgId = authinfo.organization entities = base.EntitySet([myId, myOrgId]) yield entities.fetchData(['basic', 'domains']) myOrgDomains = set(entities[myOrgId].get('domains').keys()) myOrgIsWhite = len(myOrgDomains.intersection(whitelist)) myOrgUsers = [] otherOrgUsers = [] for emailId in emailIds: try: localpart, domainpart = emailId.split('@') if domainpart in blacklist: log.info("%s is blacklisted" % (domainpart)) pass elif domainpart in myOrgDomains: myOrgUsers.append(emailId) elif myOrgIsWhite: otherOrgUsers.append(emailId) except: pass stats = None if myOrgUsers or otherOrgUsers: stats = yield _sendInvitations(myOrgUsers, otherOrgUsers, entities[myId], myId, entities[myOrgId]) defer.returnValue(stats)
def _summary(self, url): parser = etree.HTMLParser() summary = None title = None image = None ogTitle = None ogSummary = None ogImage = None embed = None try: # First check if embedly supports it. if embedlyClient and embedlyClient._regex.match(url): kwargs = {'maxwidth': 400, 'autoplay': 1} obj = yield threads.deferToThread(embedlyClient.oembed, url, **kwargs) if obj.get('error') != True: image = obj.get('thumbnail_url') title = obj.get("title") summary = obj.get("description") embedType = obj.get("type") if embedType in ["photo", "video", "audio"]: defer.returnValue((summary, title, image, obj)) else: defer.returnValue((summary, title, image, None)) #XXX: client.getPage starts and stops HTTPClientFactory everytime # find a way to avoid this d = client.getPage(url) data = yield d domain = url.split('/', 3)[2] parser.feed(data) tree = parser.close() titleElement = tree.xpath("head/title") if titleElement: title = titleElement[0].text meta = tree.xpath("//meta") for element in meta: if element.attrib.get('property', '') == 'og:title': ogTitle = element.attrib.get('content', '') ogTitle = ogTitle.encode('utf-8') if element.attrib.get('property', '') == 'og:description': ogSummary = element.attrib.get('content', '') ogSummary = ogSummary.encode('utf-8') if element.attrib.get('property', '') == 'og:image': ogImage = element.attrib.get('content', '') if element.attrib.get('name', '') in ['description', 'Description']: summary = element.attrib.get('content', '') summary = summary.encode('utf-8') if ((ogSummary or summary) and (ogTitle or title) and (ogImage or image)): defer.returnValue((ogSummary or summary, ogTitle or title, ogImage or image, embed )) if not (ogSummary or summary): for element in tree.xpath("body//p"): if element.text and len(element.text) > 25: summary = element.text break if not (ogImage or image): for element in tree.xpath("body//img"): if 'src' in element.attrib \ and element.attrib['src'].startswith('http://') \ and domain in element.attrib['src']: image = element.attrib['src'] break defer.returnValue((ogSummary or summary, ogTitle or title, ogImage or image, embed)) except Exception as e: log.info(e) defer.returnValue((ogSummary or summary, ogTitle or title, ogImage or image, embed))
def _addUsers(self, request): authInfo = request.getSession(IAuthInfo) myId = authInfo.username orgId = authInfo.organization org = base.Entity(orgId) yield org.fetchData(['basic', 'domains']) # File upload dataFmt = utils.getRequestArg(request, 'format') data = utils.getRequestArg(request, "data", sanitize=False) # Form submit - single user addtion name = utils.getRequestArg(request, "name") emailId = utils.getRequestArg(request, "email", sanitize=False) passwd = utils.getRequestArg(request, "passwd", sanitize=False) jobTitle = utils.getRequestArg(request, "jobTitle") timezone = utils.getRequestArg(request, "timezone") existingUsers = set() fileUpload = True if (dataFmt or data) else False if fileUpload and not (dataFmt and data): raise errors.MissingParams([_("File")]) if fileUpload and dataFmt not in ('csv', 'tsv'): raise errors.InvalidRequest("New user details are invalid") if not fileUpload and not all([name, emailId, passwd, jobTitle, timezone]): raise errors.MissingParams([_("All fields are required to create the user")]) if dataFmt in ("csv", "tsv"): dialect = csv.excel_tab if dataFmt == "tsv" else csv.excel data = csv.reader(data.split("\n"), dialect=dialect) data = [row for row in data] invalidLines = self._validateData(data, orgId, org) if invalidLines: if len(invalidLines) == 1: msg = "Invalid data found in line %s." % (invalidLines[0]) elif len(invalidLines) <= 3: msg = "Invalid data found in lines %s." % (",".join(invalidLines[:3])) else: msg = "Invalid data found in lines %s and others." % (",".join(invalidLines[:3])) request.write("<script>parent.$$.alerts.error('%s');</script>" % (msg)) raise errors.InvalidRequest("New user details are invalid") if all([name, emailId, passwd, jobTitle, timezone]): data = [[name, emailId, jobTitle, timezone, passwd]] errorFields = [] try: domain = emailId.split("@")[1] if domain not in org.domains: errorFields = ['Email'] except IndexError: errorFields = ['Email'] if not validTimezone(timezone): errorFields.append('Timezone') if errorFields: raise errors.InvalidRequest("Invalid %s " % (','.join(errorFields))) for row in data: if row: displayName, email, jobTitle, timezone, passwd = row existingUser = yield utils.existingUser(email) if existingUser: log.info("%s is already a member of the network." "not adding it again" % (email)) existingUsers.add(email) continue yield utils.addUser(email, displayName, passwd, orgId, jobTitle, timezone) if not fileUpload: if existingUsers: response = """ $$.alerts.error('User is already in the network.'); $$.dialog.close('addpeople-dlg', true); """ else: response = """ $$.alerts.info('User Added'); $$.dialog.close('addpeople-dlg', true); $$.fetchUri('/admin/people?type=all'); """ else: response = """ <script> parent.$$.alerts.info('Users Added'); parent.$$.fetchUri('/admin/people?type=all'); parent.$$.dialog.close('addpeople-dlg', true); </script> """ request.write(response)