def contrib_json(): """Receives a user contribution and saves to the database This function will return a JSON format with the result of the operation. That can be successful or an error, if it finds any problem in data received or the lack of the authentication. """ if not auth.is_authenticated(): return msg.error(_(u'User not authenticated')) raise Exception('Not funny') form = ContribForm(csrf_enabled=False) if form.validate_on_submit(): Contrib(title=form.data['title'].encode('utf-8'), content=form.data['content'].encode('utf-8'), theme=form.data['theme'], user=auth.authenticated_user()) session.commit() # Returning the csrf data = {'data': _('Contribution received successful')} data.update({'csrf': form.csrf.data}) return msg.ok(data) else: return format_csrf_error(form, form.errors, 'ValidationError')
def contrib_json(): """Receives a user contribution and saves to the database This function will return a JSON format with the result of the operation. That can be successful or an error, if it finds any problem in data received or the lack of the authentication. """ if not auth.is_authenticated(): return msg.error(_(u'User not authenticated')) raise Exception('Not funny') form = ContribForm(csrf_enabled=False) if form.validate_on_submit(): Contrib( title=form.data['title'].encode('utf-8'), content=form.data['content'].encode('utf-8'), theme=form.data['theme'], user=auth.authenticated_user()) session.commit() # Returning the csrf data = { 'data': _('Contribution received successful') } data.update({ 'csrf': form.csrf.data }) return msg.ok(data) else: return format_csrf_error(form, form.errors, 'ValidationError')
def profile_json(): """Validate the request of the update of a profile. This method will not operate in any user instance but the authenticated one. If there's nobody authenticated, there's no way to execute it successfuly. """ form = social(ProfileForm, False) if not form.validate_on_submit(): # This field is special, it must be validated before anything. If it # doesn't work, the action must be aborted. if not form.csrf_is_valid: return msg.error(_('Invalid csrf token'), 'InvalidCsrfToken') # Usual validation error return utils.format_csrf_error(form, form.errors, 'ValidationError') # Let's save the authenticated user's meta data mget = form.meta.get try: user = authapi.authenticated_user() except authapi.NobodyHome: return redirect(url_for('index')) # First, the specific ones email = mget('email') redologin = False if user.username == user.email and user.username != email \ and not (user.get_meta('twitteruser') or user.get_meta('facebookuser')): flash(_(u'You changed your email, please relogin.')) redologin = True user.username = email user.name = mget('name') user.email = email # Saving the thumbnail form.meta.pop('avatar') if bool(form.avatar.file): flike = form.avatar.file thumb = utils.thumbnail(flike, (48, 48)) form.meta['avatar'] = Upload.imageset.save( FileStorage(thumb, flike.filename, flike.name), 'thumbs/%s' % user.name[0].lower()) # And then, the meta ones, stored in `UserMeta' for key, val in form.meta.items(): user.set_meta(key, val) # return msg.ok({ # 'data': _('User profile updated successfuly'), # 'csrf': form.csrf.data, # }) flash(_(u'Profile update successful'), 'alert-success') if redologin: authapi.logout() return redirect(url_for('auth.login')) else: return redirect(url_for('.profile'))
def send_json(): form = forms.QuestionForm(csrf_enabled=False) form.theme.choices = [(None, '----')] + \ [(i['id'], i['name']) for i in wordpress.govr.getThemes()] if form.validate_on_submit(): wordpress.govr.createContrib( form.data['title'], form.data['theme'], form.data['question'], auth.authenticated_user().id, '', 0, 0 ) return msg.ok(u'Contribution received successful') else: return format_csrf_error(form, form.errors, 'ValidationError')