def _uploadDone(self, request): SKey = config.get('CloudFiles', 'SecretKey') AKey = config.get('CloudFiles', 'AccessKey') creds = AWSCredentials(AKey, SKey) client = s3Client.S3Client(creds) bucket = utils.getRequestArg(request, "bucket") key = utils.getRequestArg(request, "key") file_info = yield client.head_object(bucket, key) tmp_files_info = {} name = file_info['x-amz-meta-filename'][0] size = file_info['content-length'][0] fileType = file_info['content-type'][0] fileId = file_info['x-amz-meta-fileid'][0] val = "%s:%s:%s:%s" % (fileId, name, size, fileType) filename = urlsafe_b64decode(name) tmp_files_info[fileId] = [fileId, filename, size, fileType] # XXX: We currently don't generate any thumbnails! # yield threads.deferToThread(self._enqueueMessage, bucket, key, name, fileType) yield db.insert(fileId, "tmp_files", val, "fileId") response = """ <textarea data-type="application/json"> {"ok": true, "files": %s} </textarea> """ % (json.dumps(tmp_files_info)) request.write(response)
def get_count(): secretKey = config.get('CloudFiles', 'SecretKey') accessKey = config.get('CloudFiles', 'AccessKey') thumbnailsQueue = config.get('CloudFiles', 'ThumbnailsQueue') sqsConn = boto.connect_sqs(accessKey, secretKey) queue = sqsConn.get_queue(thumbnailsQueue) print '%s -- %s' %(thumbnailsQueue, queue.count())
def sendmail(toAddr, subject, textPart, htmlPart=None, fromAddr="*****@*****.**", fromName="Flocked-in"): if textPart: textPart = sanitizer.unescape(textPart, {":": ":"}) if htmlPart: msg = MIMEMultipart("alternative") msg.preamble = "This is a multi-part message in MIME format." msgText = MIMEText(textPart, _charset="utf8") msg.attach(msgText) msgText = MIMEText(htmlPart, "html", _charset="utf8") msg.attach(msgText) else: msg = MIMEText(textPart, _charset="utf8") msg["Subject"] = sanitizer.unescape(subject, {":": ":"}) msg["From"] = "%s <%s>" % (fromName, fromAddr) msg["To"] = toAddr try: devMailId = config.get("Devel", "MailId") if devMailId: toAddr = devMailId except: pass message = msg.as_string() host = config.get("SMTP", "Host") yield smtp.sendmail(host, fromAddr, toAddr, message)
def _removeTempFile(self, request): (appchange, script, args, myId) = yield self._getBasicArgs(request) landing = not self._ajax myOrgId = args["orgId"] SKey = config.get('CloudFiles', 'SecretKey') AKey = config.get('CloudFiles', 'AccessKey') bucket = config.get('CloudFiles', 'Bucket') creds = AWSCredentials(AKey, SKey) client = s3Client.S3Client(creds) fileId = utils.getRequestArg(request, "id") key = "%s/%s/%s" % (myOrgId, myId, fileId) #Check if the file is not in the "files" CF. In other words, it is not # attached to an existing item. Also check if I am the owner of the # file. Finally clear the existing entry in the "temp_files" CF res = yield db.get_slice(fileId, "tmp_files", ["fileId"]) if len(res) == 1: try: res = yield db.get(fileId, "files", super_column="meta") except ttypes.NotFoundException: file_info = yield client.head_object(bucket, key) owner = file_info['x-amz-meta-uid'][0] if owner == myId: yield client.delete_object(bucket, key) yield db.remove(fileId, "tmp_files") else: raise errors.EntityAccessDenied("attachment", fileId) else: raise errors.InvalidRequest()
def _S3FormData(self, request): (appchange, script, args, myId) = yield self._getBasicArgs(request) landing = not self._ajax myOrgId = args["orgId"] SKey = config.get('CloudFiles', 'SecretKey') AKey = config.get('CloudFiles', 'AccessKey') domain = config.get('CloudFiles', 'Domain') bucket = config.get('CloudFiles', 'Bucket') if domain == "": calling_format = SubdomainCallingFormat() domain = "s3.amazonaws.com" else: calling_format = VHostCallingFormat() conn = S3Connection(AKey, SKey, host=domain, is_secure=True, calling_format=calling_format) filename = utils.getRequestArg(request, "name") or None #TODO:If name is None raise an exception mime = utils.getRequestArg(request, "mime") or None if mime: if not mimetypes.guess_extension(mime): mime = mimetypes.guess_type(filename)[0] else: mime = mimetypes.guess_type(filename)[0] if not mime: mime = "text/plain" filename = urlsafe_b64encode(filename) fileId = utils.getUniqueKey() key = '%s/%s/%s' % (myOrgId, myId, fileId) attachment_filename = 'attachment;filename=\"%s\"' % (filename) x_conds = ['{"x-amz-meta-uid":"%s"}' % myId, '{"x-amz-meta-filename":"%s"}' % filename, '{"x-amz-meta-fileId":"%s"}' % fileId, '{"content-type":"%s"}' % mime] x_fields = [{"name":"x-amz-meta-uid", "value":"%s" % myId}, {"name":"x-amz-meta-filename", "value":"%s" % filename}, {"name":"content-type", "value":"%s" % mime}, {"name":"x-amz-meta-fileId", "value":"%s" % fileId}] max_content_length = constants.MAX_FILE_SIZE x_conds.append('["content-length-range", 0, %i]' % max_content_length) redirect_url = config.get('General', 'URL') + "/files/update" form_data = conn.build_post_form_args(bucket, key, http_method="https", fields=x_fields, conditions=x_conds, success_action_redirect=redirect_url) request.write(json.dumps([form_data])) defer.returnValue(0)
def _getAllFiles(): SKey = config.get('CloudFiles', 'SecretKey') AKey = config.get('CloudFiles', 'AccessKey') bucket = config.get('CloudFiles', 'Bucket') conn = S3Connection(AKey, SKey) bucket = conn.get_bucket(bucket) files = [] for key in bucket.list(): if not key.name.endswith('/'): files.append(key.name) return files
def _renderFile(self, request): """Allow the user to download a file attached to a conversation. Redirects the user to the donwload location on S3. Keyword arguments: filename: The name of the file. myId: userId of the person who uploaded the file. myOrgId: orgId of the currently logged in user. fileType: mimeType of the file as detected during uploading. url: filename of the file object in the Amazon S3 system. """ fileInfo = yield self._getFileInfo(request) owner, url, fileType, size, name = fileInfo authinfo = request.getSession(IAuthInfo) myOrgId = authinfo.organization filename = urlsafe_b64decode(name) try: filename.decode('ascii') except UnicodeDecodeError: filename = filename.decode('utf-8').encode('utf-8') filename = str(Header(filename, "UTF-8")).encode('string_escape') else: filename = filename.encode('string_escape') headers = {'response-content-type': fileType, 'response-content-disposition': 'attachment;filename=\"%s\"' % filename, 'response-expires': '0'} SKey = config.get('CloudFiles', 'SecretKey') AKey = config.get('CloudFiles', 'AccessKey') domain = config.get('CloudFiles', 'Domain') bucket = config.get('CloudFiles', 'Bucket') if domain == "": calling_format = SubdomainCallingFormat() domain = "s3.amazonaws.com" else: calling_format = VHostCallingFormat() conn = S3Connection(AKey, SKey, host=domain, calling_format=calling_format) Location = conn.generate_url(600, 'GET', bucket, '%s/%s/%s' % (myOrgId, owner, url), response_headers=headers) request.setResponseCode(307) request.setHeader('Location', Location)
def _enqueueMessage(self, bucket, key, filename, content_type): SKey = config.get('CloudFiles', 'SecretKey') AKey = config.get('CloudFiles', 'AccessKey') thumbnailsBucket = config.get('CloudFiles', 'ThumbnailsBucket') thumbnailsQueue = config.get('CloudFiles', 'ThumbnailsQueue') sqsConn = boto.connect_sqs(AKey, SKey) queue = sqsConn.get_queue(thumbnailsQueue) if not queue: queue = sqsConn.create_queue(thumbnailsQueue) data = {'bucket': bucket, 'filename': filename, 'key': key, 'content-type': content_type} message = queue.new_message(body=json.dumps(data)) queue.write(message)
def _sendmailResetPassword(email, token): rootUrl = config.get('General', 'URL') brandName = config.get('Branding', 'Name') body = "A request was received to reset the password "\ "for %(email)s on %(brandName)s.\nTo change the password please click the following "\ "link, or paste it in your browser.\n\n%(resetPasswdUrl)s\n\n"\ "This link is valid for 24hours only.\n"\ "If you did not request this email there is no need for further action\n" resetPasswdUrl = "%(rootUrl)s/password/resetPassword?email=%(email)s&token=%(token)s" % (locals()) args = {"brandName": brandName, "rootUrl": rootUrl, "resetPasswdUrl": resetPasswdUrl, "email": email} subject = "[%(brandName)s] Reset Password requested for %(email)s" % (locals()) htmlBody = t.getBlock("emails.mako", "forgotPasswd", **args) textBody = body % (locals()) yield utils.sendmail(email, subject, textBody, htmlBody)
def _sendSignupInvitation(emailId): if len(emailId.split('@')) != 2: raise InvalidEmailId() mailId, domain = emailId.split('@') if domain in blacklist: raise DomainBlacklisted() rootUrl = config.get('General', 'URL') brandName = config.get('Branding', 'Name') signature = "Flocked-in Team.\n\n\n\n" myOrgId = yield getOrgId(domain) if myOrgId: entities = yield db.get_slice(myOrgId, "entities", ["basic"]) myOrg = utils.supercolumnsToDict(entities) orgName = myOrg['basic']['name'] else: orgName = domain existing = yield db.get_slice(emailId, "userAuth", ["user"]) existing = utils.columnsToDict(existing) if existing and existing.get('user', ''): subject = "[%s] Account exists" % (brandName) body = "You already have an account on %(brandName)s.\n"\ "Please visit %(rootUrl)s/signin to sign-in.\n\n" textBody = (body + signature) % locals() htmlBody = t.getBlock("emails.mako", "accountExists", **locals()) else: subject = "Welcome to %s" % (brandName) body = "Please click the following link to join %(orgName)s network on %(brandName)s\n"\ "%(activationUrl)s\n\n" activationTmpl = "%(rootUrl)s/signup?email=%(emailId)s&token=%(token)s" token = utils.getRandomKey() insert_d = db.insert(domain, "invitations", emailId, token, emailId) activationUrl = activationTmpl % locals() textBody = (body + signature) % locals() htmlBody = t.getBlock("emails.mako", "signup", **locals()) yield insert_d yield utils.sendmail(emailId, subject, textBody, htmlBody)
def _reportUser(self, request, myId, targetId): entities = base.EntitySet([myId, targetId]) yield entities.fetchData() reportedBy = entities[myId].basic["name"] email = entities[targetId].basic["emailId"] rootUrl = config.get('General', 'URL') brandName = config.get('Branding', 'Name') authinfo = request.getSession(IAuthInfo) amIAdmin = authinfo.isAdmin cols = yield db.get_slice(email, "userAuth", ["reactivateToken", "isFlagged", "isAdmin"]) cols = utils.columnsToDict(cols) if cols.has_key("isAdmin") and not amIAdmin: raise errors.PermissionDenied("Only administrators can flag other \ administrators for verification") if cols.has_key("isFlagged"): token = cols.get("reactivateToken") else: token = utils.getRandomKey() yield db.insert(email, "userAuth", token, 'reactivateToken') yield db.insert(email, "userAuth", "", 'isFlagged') body = "%(reportedBy)s has flagged your account for verification."\ "You can verify your account by clicking on the link below.\n"\ "\n\n%(reactivateUrl)s\n\n" reactivateUrl = "%(rootUrl)s/password/verify?email=%(email)s&token=%(token)s"%(locals()) args = {"brandName": brandName, "rootUrl": rootUrl, "reportedBy":reportedBy, "reactivateUrl": reactivateUrl} subject = "[%(brandName)s] Your profile has been flagged for review" %(locals()) htmlBody = t.getBlock("emails.mako", "reportUser", **args) textBody = body %(locals()) yield utils.sendmail(email, subject, textBody, htmlBody) request.write('$$.alerts.info("%s");' % _('User has been flagged for verification'))
def _renderFile(self, request): fileInfo = yield self._getFileInfo(request) owner, url, fileType, size, name = fileInfo authinfo = request.getSession(IAuthInfo) myOrgId = authinfo.organization filename = urlsafe_b64decode(name) try: filename.decode('ascii') except UnicodeDecodeError: filename = filename.decode('utf-8').encode('utf-8') filename = str(Header(filename, "UTF-8")).encode('string_escape') else: filename = filename.encode('string_escape') headers = {'response-content-type': fileType, 'response-content-disposition': 'attachment;filename=\"%s\"' % filename, 'response-expires': '0'} SKey = config.get('CloudFiles', 'SecretKey') AKey = config.get('CloudFiles', 'AccessKey') domain = config.get('CloudFiles', 'Domain') bucket = config.get('CloudFiles', 'Bucket') if domain == "": calling_format = SubdomainCallingFormat() domain = "s3.amazonaws.com" else: calling_format = VHostCallingFormat() conn = S3Connection(AKey, SKey, host=domain, is_secure=True, calling_format=calling_format) Location = conn.generate_url(600, 'GET', bucket, '%s/%s/%s' % (myOrgId, owner, url), response_headers=headers) request.setResponseCode(307) request.setHeader('Location', Location)
def __init__(self, f): defaultLogLevel = 'INFO' self.logLevel = defaultLogLevel try: self.logLevel = config.get('LOGGING', 'LOGLEVEL') if self.logLevel not in LOG_LEVELS: self.logLevel = defaultLogLevel except ConfigParser.NoSectionError: pass except ConfigParser.NoOptionError: pass self.logLevel = LOG_LEVELS[self.logLevel] python.log.FileLogObserver.__init__(self, f)
def _send(self, request): name = utils.getRequestArg(request, "name") or None email = utils.getRequestArg(request, "email") or None subject = utils.getRequestArg(request, "subject") or None message = utils.getRequestArg(request, "message") or None if not name or not email or not subject or not message: raise errors.MissingParams(["Please fill-in all the fields"]) if not 0 < int(subject) < len(self._subjects): return subject = "[flocked-in contact] %s" % self._subjects[int(subject)] toAddr = config.get("General", "ContactId") yield utils.sendmail(toAddr, subject, message, fromAddr=email, fromName=name) if not self.thanksPage: self.thanksPage = static.File("public/thanks-for-contacting.html") if self.thanksPage: yield self.thanksPage.render_GET(request)
#url = compound.Pipe(validators.SocialString(sanitize=False), # validators.URL()) url = compound.All(validators=[validators.URL(), validators.SocialString(sanitize=False)]) comment = validators.TextWithSnippet(ignore_null=True) def _sanitize(text, maxlen=0): unitext = text if type(text) == unicode or not text\ else text.decode('utf-8', 'replace') if maxlen and len(unitext) > maxlen: return utils.toSnippet(unitext, maxlen) else: return unitext.encode('utf-8') embedlyKey = config.get('Embedly', 'Key') embedlyClient = None if embedlyKey: embedlyClient = embedly.client.Embedly(key=embedlyKey) embedlyClient.get_services() class Links(object): implements(IPlugin, IItemType) itemType = "link" position = 3 hasIndex = True indexFields = {'meta':set(['link_summary','link_title'])} monitoredFields = {'meta':['comment', 'link_summary', 'link_title']} displayNames = ('Link', 'Links')
import uuid import time import pickle from telephus.protocol import ManagedCassandraClientFactory from telephus.client import CassandraClient from telephus.cassandra.ttypes import ColumnPath, ColumnParent, Column, SuperColumn, KsDef, CfDef from twisted.internet import defer, reactor from twisted.python import log sys.path.append(os.getcwd()) from social import config, db, utils, search from social.base import Entity KEYSPACE = config.get("Cassandra", "Keyspace") @defer.inlineCallbacks def reindexProfileContent(): rows = yield db.get_range_slice('entities', count=1000) for row in rows: entityId = row.key log.msg(entityId) entity = Entity(entityId, utils.supercolumnsToDict(row.columns)) if entity.basic.get('type', '') == 'user': orgId = entity.basic.get('org', '') if orgId: yield search.solr.updatePeopleIndex(entityId, entity, orgId)
def getNewUserCount(startDate, endDate, count=100, column_count=100, mail_to=''): frm_to = startDate + ' ' + endDate startDate = datetime.datetime.strptime(startDate, dateFormat) endDate = datetime.datetime.strptime(endDate, dateFormat) if endDate <= startDate: log.msg("end-date should be later than start-date") raise Exception("end-date should be later than start-date") startTime = time.mktime(startDate.timetuple()) endTime = time.mktime(endDate.timetuple()) toFetchCount = count +1 toFetchColumnCount = column_count +1 new_domains = [] start = '' stats = {} data = {} while 1: domains = yield db.get_range_slice('domainOrgMap', count=toFetchCount, start=start) for row in domains[:count]: domain = row.key for col in row.columns[:count]: if domain not in data.setdefault(col.column.name, {}).setdefault("domain", []): data[col.column.name]["domain"].append((domain, col.column.timestamp/1e6)) column_timestamp = col.column.timestamp/1000000.0 if column_timestamp < endTime and column_timestamp >= startTime: if domain not in new_domains: new_domains.append(domain) if len(domains) < toFetchCount: break else: start = domains[-1].key stats = {frm_to: {"newDomains":new_domains, "newDomainCount": len(new_domains) }} start = '' new_users = {} usersOrgMap = {} totalNewUsers = 0 totalUsers ={} while 1: users = yield db.get_range_slice('orgUsers', start=start, count=toFetchCount, column_count=toFetchColumnCount) for row in users[:count]: orgId = row.key totalUsers[orgId] = 0 for col in row.columns[:column_count]: userId = col.column.name usersOrgMap[userId] = orgId if userId not in data.setdefault(orgId, {}).setdefault("users", {}): data[orgId]['users'][userId] = {"newItems":0, "items":0} column_timestamp = col.column.timestamp/1000000.0 if column_timestamp < endTime and column_timestamp >= startTime: if col.column.name not in new_users.setdefault(orgId, []): new_users[orgId].append(userId) if column_timestamp < endTime: totalUsers[orgId] +=1 if len(row.columns) == toFetchColumnCount: column_start = row.columns[-1].column.name while 1: _users = yield db.get_range_slice('orgUsers', count=1, start=orgId, column_start=column_start, column_count=toFetchColumnCount) for col in _users[0].columns[:column_count]: userId = col.column.name usersOrgMap[userId] = orgId if userId not in data.setdefault(orgId, {}).setdefault("users", {}): data[orgId]['users'][userId] = {'newItems':0, 'items':0} column_timestamp = col.column.timestamp/1000000.0 if column_timestamp < endTime and column_timestamp >= startTime: if col.column.name not in new_users[orgId]: new_users[orgId].append(userId) if column_timestamp < endTime: totalUsers[orgId] +=1 if len(_users[0].columns) == toFetchColumnCount: column_start = _users[0].columns[-1].column.name else: break totalNewUsers += len(new_users.get(orgId, [])) if len(users) < toFetchCount: break else: start = users[-1].key stats[frm_to]["signups"] = totalNewUsers start = '' while 1: rows = yield db.get_range_slice('userItems', start=start, count=toFetchCount, column_count = toFetchColumnCount) for row in rows[:count]: userId = row.key for col in row.columns[:column_count]: if userId not in usersOrgMap: data['no-org'] = {"users":{userId:{"items": 0, "newItems": 0}}} orgId = 'no-org' else: orgId = usersOrgMap[userId] if userId not in data[orgId]['users'] : data[orgId]['users'] = {'items': 0 , 'newItems': 0} column_timestamp = col.column.timestamp/1000000.0 if column_timestamp < endTime and column_timestamp >= startTime: data[orgId]['users'][userId]['newItems'] += 1 if column_timestamp < endTime: data[orgId]['users'][userId]['items'] += 1 if len(row.columns) == toFetchColumnCount: cstart = row.columns[-1].column.name while 1: userItems = yield db.get_range_slice('userItems', count=1, start=userId, column_start= cstart, column_count= toFetchColumnCount) for col in userItems[0].columns[:column_count]: column_timestamp = col.column.timestamp/1000000.0 if column_timestamp < endTime and column_timestamp >= startTime: data[orgId]['users'][userId]['newItems'] += 1 #if userId in data[orgId]['users'] : if column_timestamp < endTime: data[orgId]['users'][userId]['items'] += 1 if len(userItems[0].columns) == toFetchColumnCount: cstart = userItems[0].columns[-1].column.name else: break if len(rows) < toFetchCount: break else: start = rows[-1].key stats["domain"] = OrderedDict() sortedOrgIds = sorted(data, key=lambda x: data[x]["domain"][0][1]) for orgId in sortedOrgIds: domainName = ",".join([x[0] for x in data[orgId]['domain']]) stats["domain"][domainName] = {} stats["domain"][domainName]["newUsers"] = len(new_users.get(orgId, [])) stats["domain"][domainName]["totalUsers"] = totalUsers.get(orgId, 0) stats["domain"][domainName]["newItems"] = sum([data[orgId]['users'][x]['newItems'] for x in data[orgId].get('users', {})]) stats["domain"][domainName]["items"] = sum([data[orgId]['users'][x]['items'] for x in data[orgId].get('users', {})]) if not mail_to: print pprint.pprint(stats) subject = "Stats: %s to %s" % (startDate.strftime(dateFormat), endDate.strftime(dateFormat)) textPart = repr(stats) rootUrl = config.get('General', 'URL') brandName = config.get('Branding', 'Name') htmlPart = getBlock("emails.mako", "html_stats", **{"stats":stats, "frm_to": frm_to, 'rootUrl': rootUrl, 'brandName': brandName}) for mailId in mail_to: yield utils.sendmail(mailId, subject, textPart, htmlPart)
import cPickle as pickle import time from email.utils import formatdate from twisted.web import resource, server, static, util from twisted.internet import defer from twisted.python import components from social import db, utils, base, plugins, config from social.isocial import IAuthInfo from social.logging import log SECURE_COOKIES = False try: SECURE_COOKIES = config.get("General", "SSLOnlyCookies") except: pass COOKIE_DOMAIN = None try: COOKIE_DOMAIN = config.get('General', "CookieDomain") except: pass class RequestFactory(server.Request): cookiename = 'session' session = None session_saved = False apiAccessToken = None @defer.inlineCallbacks def _saveSessionToDB(self, ignored=None):
import httplib from urlparse import urljoin, urlparse from zope.interface import implements #from twisted.python import log from twisted.internet import defer, reactor, protocol, threads, task from twisted.web import client from twisted.web.iweb import IBodyProducer from twisted.internet import defer from twisted.web.client import Agent from twisted.web.http_headers import Headers from social import config from social.logging import log COMET_BASEURL = config.get('Cometd', 'BaseUrl') COMET_PATH = config.get('Cometd', 'Path') COMET_SECRET=config.get('Cometd', 'secret') class CometRetryException(Exception): """ """ class _StringProducer(object): implements(IBodyProducer) def __init__(self, body): self.body = body self.length = len(body) def startProducing(self, consumer):
def postFeedback(self, request): """creates a feedback item with feedback, feedback-{mood} tags. Push item to feeback, feedback-{mood} tag followers. Note: Item is owned by feedback-domain/synovel.com, not user sending the feedback. Only users of feedback-domain/synovel.com can access the item. """ comment = utils.getRequestArg(request, 'comment') mood = utils.getRequestArg(request, 'mood') if not mood or not comment: raise errors.MissingParams([_("Feedback")]) authInfo = request.getSession(IAuthInfo) myId = authInfo.username orgId = authInfo.organization tagName = 'feedback' moodTagName = 'feedback-' + mood feedbackDomain = config.get('Feedback', 'Domain') or 'synovel.com' cols = yield db.get_slice(feedbackDomain, 'domainOrgMap') if not cols: raise errors.ConfigurationError("feedbackDomain is invalid!") # Only one org exists per domain synovelOrgId = cols[0].column.name tagId, tag = yield tags.ensureTag(request, tagName, synovelOrgId) moodTagId, moodTag = yield tags.ensureTag(request, moodTagName, synovelOrgId) # Anyone in synovel can receive feedback. acl = {'accept': {'orgs': [synovelOrgId]}} acl = json.dumps(acl) synovelOrg = base.Entity(synovelOrgId) yield synovelOrg.fetchData() # createNewItem expects an entity object with has org in basic info. # organizations wont have 'org' set. synovelOrg.basic['org'] = synovelOrgId item = yield utils.createNewItem(request, 'feedback', synovelOrg, acl, subType=mood) item['meta']['org'] = synovelOrgId item['meta']['userId'] = myId item['meta']['userOrgId'] = orgId item['meta']['comment'] = comment item['tags'] = {tagId: synovelOrgId, moodTagId: synovelOrgId} itemId = utils.getUniqueKey() tagItemCount = int(tag['itemsCount']) moodTagItemCount = int(moodTag['itemsCount']) if tagItemCount % 10 == 7: tagItemCount = yield db.get_count(tagId, "tagItems") if moodTagItemCount % 10 == 7: moodTagItemCount = yield db.get_count(moodTagId, "tagItems") tagItemCount += 1 moodTagItemCount += 1 # Finally save the feedback yield db.batch_insert(itemId, "items", item) yield db.insert(tagId, "tagItems", itemId, item["meta"]["uuid"]) yield db.insert(moodTagId, "tagItems", itemId, item["meta"]["uuid"]) yield db.insert(synovelOrgId, "orgTags", str(tagItemCount), "itemsCount", tagId) yield db.insert(synovelOrgId, "orgTags", str(moodTagItemCount), "itemsCount", moodTagId) cols = yield db.multiget_slice([tagId, moodTagId], "tagFollowers") followers = utils.multiColumnsToDict(cols) followers = set(followers[tagId].keys() + followers[moodTagId].keys()) value = {"feed": {item['meta']['uuid']: itemId}} muts = dict([(x, value) for x in followers]) if muts: yield db.batch_mutate(muts)
def generate_thumbnails(dry_run=True): def _generate_thumbnail(filename, size, thumbKey, content_type, thumbnailsBucket): tmpFile = tempfile.NamedTemporaryFile() image = PythonMagick.Image(filename) image.scale(size) image.write(tmpFile.name) k1 = thumbnailsBucket.new_key(thumbKey) headers = {'Content-Type': str(content_type)} k1.set_contents_from_file(tmpFile, headers) tmpFile.close() secretKey = config.get('CloudFiles', 'SecretKey') accessKey = config.get('CloudFiles', 'AccessKey') bucketName = config.get('CloudFiles', 'Bucket') thumbnailsBucket = config.get('CloudFiles', 'ThumbnailsBucket') thumbnailsQueue = config.get('CloudFiles', 'ThumbnailsQueue') if not (secretKey and accessKey) and not dry_run: raise Exception("MISSING CONFIG") sqsConn = boto.connect_sqs(accessKey, secretKey) s3Conn = boto.connect_s3(accessKey, secretKey) thumbnailsBucket = s3Conn.get_bucket(thumbnailsBucket) queue = sqsConn.get_queue(thumbnailsQueue) sleep = 1 while 1: done_work = False for message in queue.get_messages(): meta = json.loads(message.get_body()) key = meta['key'] bucket = meta['bucket'] filename = meta['filename'] content_type = meta['content-type'] isProfilePic = meta.get('is-profile-pic', False) isLogo = meta.get('is-logo', False) isAttachment = meta.get('is-attachment', False) if content_type in MEDIA_MIME: #TODO: cache the buckets bucket = s3Conn.get_bucket(bucketName) k = bucket.get_key(key) if k is None: queue.delete_message(message) continue done_work= True fp = tempfile.NamedTemporaryFile() k.get_file(fp) large = constants.LOGO_SIZE_LARGE if isLogo else constants.AVATAR_SIZE_LARGE medium = constants.LOGO_SIZE_MEDIUM if isLogo else constants.AVATAR_SIZE_MEDIUM small = constants.LOGO_SIZE_SMALL if isLogo else constants.AVATAR_SIZE_SMALL largeThumbKey = "large/%s" %(key) mediumThumbKey = "medium/%s" %(key) smallThumbKey = "small/%s" %(key) _generate_thumbnail(fp.name, large, largeThumbKey, content_type, thumbnailsBucket) _generate_thumbnail(fp.name, medium, mediumThumbKey, content_type, thumbnailsBucket) _generate_thumbnail(fp.name, small, smallThumbKey, content_type, thumbnailsBucket) fp.close() #temp file is destroyed. queue.delete_message(message) if not done_work: sleep = 60 if sleep > 30 else (sleep*2) else: sleep = 1 time.sleep(sleep) # prevents busy loop
import json import traceback import tempfile import os from mako.template import Template from mako.lookup import TemplateLookup from social import config tmpDirName = 'social-' + str(os.geteuid()) tmpDirPath = os.path.join(tempfile.gettempdir(), tmpDirName) filesystemChecks = False try: checkForUpdates = config.get('Devel', 'CheckTemplateUpdates') if checkForUpdates and checkForUpdates.lower() == "true": filesystemChecks = True except: pass _collection = TemplateLookup(directories=['templates'], module_directory=tmpDirPath, output_encoding='utf-8', input_encoding='utf-8', filesystem_checks=filesystemChecks, default_filters=['decode.utf8'], collection_size=100) _spaceRE = re.compile(r'(\n)\s+')
from twisted.web import client, resource from twisted.web.iweb import IBodyProducer from twisted.internet import protocol, reactor, defer from twisted.web.http import PotentialDataLoss from twisted.web.http_headers import Headers from social import base, utils, config, feed from social import errors, plugins, _, db from social import template as t from social.constants import SEARCH_RESULTS_PER_PAGE from social.logging import dump_args, profile, log from social.relations import Relation URL = config.get('SOLR', 'HOST') class XMLBodyProducer(object): implements (IBodyProducer) def __init__(self, domtree): self._body = etree.tostring(domtree) self.length = len(self._body) def startProducing(self, consumer): consumer.write(self._body) return defer.succeed(None) def pauseProducing(self): pass
def _sendInvitations(myOrgUsers, otherOrgUsers, me, myId, myOrg): rootUrl = config.get('General', 'URL') brandName = config.get('Branding', 'Name') senderName = me.basic["name"] senderOrgName = myOrg.basic["name"] senderAvatarUrl = utils.userAvatar(myId, me, "medium") sentUsers = [] blockedUsers = [] existingUsers = [] myOrgSubject = "%s invited you to %s" % (senderName, brandName) myOrgBody = "Hi,\n\n"\ "%(senderName)s has invited you to %(senderOrgName)s network on %(brandName)s.\n"\ "To activate your account please visit: %(activationUrl)s.\n\n" otherOrgSubject = "%s invited you to %s" % (senderName, brandName) otherOrgBody = "Hi,\n\n"\ "%(senderName)s has invited you to try %(brandName)s.\n"\ "To activate your account please visit: %(activationUrl)s.\n\n" signature = "Flocked.in Team.\n\n\n\n"\ "--\n"\ "To block invitations from %(senderName)s visit %(blockSenderUrl)s\n"\ "To block all invitations from %(brandName)s visit %(blockAllUrl)s" blockSenderTmpl = "%(rootUrl)s/signup/blockSender?email=%(emailId)s&token=%(token)s" blockAllTmpl = "%(rootUrl)s/signup/blockAll?email=%(emailId)s&token=%(token)s" activationTmpl = "%(rootUrl)s/signup?email=%(emailId)s&token=%(token)s" # Combine all users. myOrgUsers.extend(otherOrgUsers) # Ensure that the users do not already exist and that the users are # not in the doNotSpam list (for this sender or globally) d1 = db.multiget(myOrgUsers, "userAuth", "user") d2 = db.multiget_slice(myOrgUsers, "doNotSpam", [myId, '*']) existing = yield d1 existing = utils.multiColumnsToDict(existing) doNotSpam = yield d2 doNotSpam = utils.multiColumnsToDict(doNotSpam) deferreds = [] for emailId in myOrgUsers: if emailId in existing and existing[emailId]: existingUsers.append(emailId) continue token = utils.getRandomKey() # Add invitation to the database localpart, domainpart = emailId.split('@') deferreds.append(db.insert(domainpart, "invitations", myId, token, emailId)) deferreds.append(db.insert(myId, "invitationsSent", '', emailId)) # Mail the invitation if everything is ok. if emailId in doNotSpam and doNotSpam[emailId]: blockedUsers.append(emailId) continue activationUrl = activationTmpl % locals() blockAllUrl = blockAllTmpl % locals() blockSenderUrl = blockSenderTmpl % locals() sameOrg = False if emailId in otherOrgUsers else True if not sameOrg: subject = otherOrgSubject textBody = (otherOrgBody + signature) % locals() else: subject = myOrgSubject textBody = (myOrgBody + signature) % locals() # XXX: getBlock blocks the application for disk reads when reading template htmlBody = t.getBlock("emails.mako", "invite", **locals()) deferreds.append(utils.sendmail(emailId, subject, textBody, htmlBody)) sentUsers.append(emailId) yield defer.DeferredList(deferreds) defer.returnValue((sentUsers, blockedUsers, existingUsers))