def handlePost(self, action): request = self.request data, errors = self.extractData() if errors: IStatusMessage(request).add( _('Please fix indicated errors.'), 'warning') else: discussion = IContentDiscussion(self.context) commentText = cgi.escape(data['comment']).replace('\n', '<br />') comment = Comment(request.principal.id, commentText) comment.date = datetime.now(ITZInfo(request, pytz.utc)) comment = discussion.add(comment) if 'authorName' in data: comment.authorName = data['authorName'] tab = 'guest' # TODO: check configlets if 'social_type' in data and data['social_type']: comment.social_type = data['social_type'] comment.social_name = comment.authorName if comment.social_type == 1: comment.twitter_id = self.request.cookies['screen_name'] comment.social_avatar_url = getUtility( ITwitterCommentingConfig).avatarUrl + comment.twitter_id tab = 'twitter' elif comment.social_type == 2: comment.facebook_id = self.request.cookies['facebook_id'] comment.social_avatar_url = getUtility( IFacebookCommentingConfig).avatarUrl + comment.facebook_id + '/picture' tab = 'facebook' else: # NOTE: set cookie for anon if not self.isPrincipal(): cookie_var = getVariablesForCookie(request) # 347 days expires = build_http_date(time.time() + 30000000) request.response.setCookie(cookie_var['name'], data['authorName'], path=cookie_var['path'], expires=expires) msg = _('Your comment is awaiting moderation.') if self.isPrincipal(): comment.approved = True msg = _('Comment has been added.') # NOTE: send modified event for original or new object notify(ObjectModifiedEvent(comment)) IStatusMessage(request).add(msg) # self.redirect('%s#comments'%request.getURL()) # self.redirect('%s?tab=%s#comment-form' % (request.getURL(), tab)) self.redirect('%s#%s' % (request.getURL(), tab))
def handlePost(self, action): request = self.request data, errors = self.extractData() if errors: IStatusMessage(request).add( _('Please fix indicated errors.'), 'warning') else: discussion = IContentDiscussion(self.context) commentText = cgi.escape(data['comment']).replace('\n', '<br />') comment = Comment(request.principal.id, commentText) comment.date = datetime.now(ITZInfo(request, pytz.utc)) comment = discussion.add(comment) comment.setParent(self.replyto) if 'authorName' in data: comment.authorName = data['authorName'] # NOTE: set cookie for anon if not self.isPrincipal(): cookie_var = getVariablesForCookie(request) # 347 days expires = build_http_date(time.time() + 30000000) request.response.setCookie(cookie_var['name'], data['authorName'], path=cookie_var['path'], expires=expires) msg = _('Your comment is awaiting moderation.') if self.isPrincipal(): comment.approved = True msg = _('Comment has been added.') # send modified event for original or new object notify(ObjectModifiedEvent(comment)) IStatusMessage(request).add(msg) self.redirect('%s#comments' % request.getURL())
def setRequestId(self, request, id): """Set cookie with id on request. This sets the response cookie: See the examples in getRequestId. Note that the id is checkec for validity. Setting an invalid value is silently ignored: >>> from zope.publisher.http import HTTPRequest >>> request = HTTPRequest(StringIO(''), {}, None) >>> bim = CookieClientIdManager() >>> bim.getRequestId(request) >>> bim.setRequestId(request, 'invalid id') >>> bim.getRequestId(request) For now, the cookie path is the application URL: >>> cookie = request.response.getCookie(bim.namespace) >>> cookie['path'] == request.getApplicationURL(path_only=True) True In the future, it should be the site containing the CookieClientIdManager By default, session cookies don't expire: >>> cookie.has_key('expires') False Expiry time of 0 means never (well - close enough) >>> bim.cookieLifetime = 0 >>> request = HTTPRequest(StringIO(''), {}, None) >>> bid = bim.getClientId(request) >>> cookie = request.response.getCookie(bim.namespace) >>> cookie['expires'] 'Tue, 19 Jan 2038 00:00:00 GMT' A non-zero value means to expire after than number of seconds: >>> bim.cookieLifetime = 3600 >>> request = HTTPRequest(StringIO(''), {}, None) >>> bid = bim.getClientId(request) >>> cookie = request.response.getCookie(bim.namespace) >>> import rfc822 >>> expires = time.mktime(rfc822.parsedate(cookie['expires'])) >>> expires > time.mktime(time.gmtime()) + 55*60 True """ # TODO: Currently, the path is the ApplicationURL. This is reasonable, # and will be adequate for most purposes. # A better path to use would be that of the folder that contains # the site manager this service is registered within. However, # that would be expensive to look up on each request, and would # have to be altered to take virtual hosting into account. # Seeing as this utility instance has a unique namespace for its # cookie, using ApplicationURL shouldn't be a problem. if self.cookieLifetime is not None: if self.cookieLifetime: expires = build_http_date(time.time() + self.cookieLifetime) else: expires = 'Tue, 19 Jan 2038 00:00:00 GMT' request.response.setCookie( self.namespace, id, expires=expires, path=request.getApplicationURL(path_only=True) ) else: request.response.setCookie( self.namespace, id, path=request.getApplicationURL(path_only=True) )
def _expire_time(period): """ period = 30000000 #347 days period = 1800 #30 min """ return build_http_date(time.time() + period)