class ICredentials(form.Schema): username = schema.TextLine(title=_(u'The username.'), ) password = schema.Password( title=_(u'Password'), description=_(u'The provided username account password.'), )
class MAXUISettingsEditForm(controlpanel.RegistryEditForm): """MAXUI settings form. """ schema = IMAXUISettings id = "MAXUISettingsEditForm" label = _(u"MAX UI settings") description = _(u"help_maxui_settings_editform", default=u"Settings related to MAX, including OAuth server " "endpoint and grant method.") def updateFields(self): super(MAXUISettingsEditForm, self).updateFields() # self.fields = self.fields.omit('max_app_token') def updateWidgets(self): super(MAXUISettingsEditForm, self).updateWidgets() @button.buttonAndHandler(_('Save'), name=None) def handleSave(self, action): data, errors = self.extractData() if errors: self.status = self.formErrorsMessage return # Auto-fill the oauth_server from max_server info endpoint maxclient = MaxClient(url=data['max_server']) data['oauth_server'] = maxclient.oauth_server self.applyChanges(data) # Imports required to be here because of circular dependencies from mrs5.max.utilities import IMAXClient from mrs5.max.utilities import IHubClient # Update the connection to the (singleton) clients utilities maxclient = getUtility(IMAXClient) maxclient.create_new_connection() hubclient = getUtility(IHubClient) hubclient.create_new_connection() notify(ConfigurationChangedEvent(self, data)) IStatusMessage(self.request).addStatusMessage(_(u"Changes saved"), "info") self.context.REQUEST.RESPONSE.redirect("@@maxui-settings") @button.buttonAndHandler(_('Cancel'), name='cancel') def handleCancel(self, action): IStatusMessage(self.request).addStatusMessage(_(u"Edit cancelled"), "info") self.request.response.redirect( "%s/%s" % (self.context.absolute_url(), self.control_panel_view))
def handleSave(self, action): data, errors = self.extractData() if errors: self.status = self.formErrorsMessage return # Auto-fill the oauth_server from max_server info endpoint maxclient = MaxClient(url=data['max_server']) data['oauth_server'] = maxclient.oauth_server self.applyChanges(data) # Imports required to be here because of circular dependencies from mrs5.max.utilities import IMAXClient from mrs5.max.utilities import IHubClient # Update the connection to the (singleton) clients utilities maxclient = getUtility(IMAXClient) maxclient.create_new_connection() hubclient = getUtility(IHubClient) hubclient.create_new_connection() notify(ConfigurationChangedEvent(self, data)) IStatusMessage(self.request).addStatusMessage(_(u"Changes saved"), "info") self.context.REQUEST.RESPONSE.redirect("@@maxui-settings")
class IEnhancedUserDataSchema(IRegisterSchema): """ Use all the fields from the default user data schema, and add various extra fields. """ # portrait = FileUpload(title=PLMF(u'label_portrait', default=u'Portrait'), # description=_(u'help_portrait', # default=u'To add or change the portrait: click the ' # '"Browse" button; select a picture of yourself.'), # required=False) pdelete = schema.Bool(title=PLMF(u'label_delete_portrait', default=u'Delete Portrait'), description=u'', required=False) twitter_username = schema.TextLine( title=_(u'label_twitter', default=u'Twitter username'), description=_(u'help_twitter', default=u"Fill in your Twitter username."), required=False, )
class getRestrictedTokenForm(form.SchemaForm): grok.name('getRestrictedToken') grok.require('cmf.ManagePortal') grok.template('gettokenform') grok.context(ISiteRoot) schema = ICredentials ignoreContext = True label = _(u'Get a valid token') description = _(u'Give the credentials of a valid account.') def update(self): # call the base class version - this is very important! super(getRestrictedTokenForm, self).update() # disable Plone's editable border self.request.set('disable_border', True) self.actions['get_token'].addClass('context') @button.buttonAndHandler(_(u'Get token'), name='get_token') def handleApply(self, action): data, errors = self.extractData() if errors: self.status = self.formErrorsMessage return username = data['username'] password = data['password'] maxclient, settings = getUtility(IMAXClient)() settings.max_restricted_username = username try: settings.max_restricted_token = maxclient.getToken( username, password) IStatusMessage(self.request).addStatusMessage( 'Restricted token issued for user: {}'.format(username), 'info') except AttributeError, error: IStatusMessage(self.request).addStatusMessage( error, 'Username or password invalid.') # Add context for this site MAX server with the restricted token portal = getSite() portal_permissions = dict(read='subscribed', write='subscribed', subscribe='restricted') # maxclient.setActor(self.maxui_settings.max_restricted_username) # maxclient.setToken(self.maxui_settings.max_restricted_token) # maxclient.addContext(portal.absolute_url(), # portal.title, # portal_permissions # ) context_params = { 'url': portal.absolute_url(), 'displayName': portal.title, 'permissions': portal_permissions } maxclient.setActor(settings.max_restricted_username) maxclient.setToken(settings.max_restricted_token) try: maxclient.contexts.post(**context_params) except: IStatusMessage(self.request).addStatusMessage( 'There was an error trying to create the default (portal root) URL into MAX server.', 'error') # Add the restricted token to the Plone admin user set_user_oauth_token('admin', settings.max_restricted_token) # Redirect back with a status message self.request.response.redirect('{}/{}'.format( self.context.absolute_url(), '@@maxui-settings'))
def title(self): """This property is used to give the title of the portlet in the "manage portlets" screen. """ return _(u'maxuiactivity', default=u'MAX UI: Activity')
class IMAXUISettings(form.Schema): """Global oAuth settings. This describes records stored in the configuration registry and obtainable via plone.registry. """ form.mode(oauth_server='hidden') oauth_server = schema.TextLine( title=_(u'label_oauth_server', default=u'OAuth token endpoint'), description=_( u'help_oauth_server', default=u"Please, specify the URI for the oAuth server."), required=True, default=DEFAULT_OAUTH_TOKEN_ENDPOINT) max_server = schema.TextLine( title=_(u'label_max_server', default=u'MAX Server URL'), description=_(u'help_max_server', default=u"Please, specify the MAX Server URL."), required=True, default=DEFAULT_MAX_SERVER) max_server_alias = schema.TextLine( title=_( u'label_max_server_alias', default=u'MAX Server URL Alias (Fallback when no CORS available)'), description=_(u'help_max_server_alias', default=u"Please, specify the MAX Server URL Alias."), required=False, default=DEFAULT_MAX_SERVER) form.mode(max_restricted_username='******') max_restricted_username = schema.TextLine( title=_(u'label_max_restricted_username', default=u'MAX restricted username'), description=_(u'help_max_restricted_username', default=u"Please, specify the MAX restricted username."), required=False, default=DEFAULT_MAX_RESTRICTED_USERNAME) form.mode(max_restricted_token='hidden') max_restricted_token = schema.Password( title=_(u'label_max_restricted_token', default=u'MAX restricted user token'), description=_( u'help_max_restricted_token', default=u"Please, specify the MAX restricted user token."), required=False, ) hub_server = schema.TextLine( title=_(u'label_hub_server', default=u'uLearnHub server'), description=_( u'help_hub_server', default=u"Please, specify the uLearnHub server for this site."), required=False, default=DEFAULT_HUB_SERVER) domain = schema.TextLine( title=_(u'label_domain', default=u'MAX domain'), description=_( u'help_domain', default=u"Please, specify the HUB domain for this site."), required=False, default=DEFAULT_DOMAIN)
def handleCancel(self, action): IStatusMessage(self.request).addStatusMessage(_(u"Edit cancelled"), "info") self.request.response.redirect( "%s/%s" % (self.context.absolute_url(), self.control_panel_view))