Exemplo n.º 1
0
def template_edit(path):

    path = os.path.join(current_app.root_path, 'templates', "%s.html" % path)
    html = ""

    try:
        f = open(path)
        html = f.read()
        f.close()
    except:
        flash(_("Template file does not exists"), "error")

    form = TemplateForm(html=html.decode('utf8'))

    if form.validate_on_submit():

        f = open(path, 'w')
        f.write(form.html.data.encode('utf8'))
        f.close()

        flash(_("Saving success"), "success")

        return redirect(url_for("frontend.index"))

    return render_template("blog/template_edit.html",
                            form=form,
                            path=path)
Exemplo n.º 2
0
Arquivo: lotg.py Projeto: tojaku/lotg
def update_account():
    form = AccountDataForm()
    user = g.user
    if request.method == 'GET':
        form.email.data = user.email
        form.language.data = user.language
        form.timezone.data = user.timezone
    elif request.method == 'POST' and form.validate_on_submit():
        if password_hash(form.old_password.data) != user.password:
            form.old_password.errors.append(_('Wrong password'))
            return render_template('update_account.html', form=form)
        if form.email.data != user.email:
            user.email = form.email.data
            user.confirmed = False
            user.confirmation_string = random_string(20)
            msg = message_confirmation(user.id, user.email, user.confirmation_string)
            mail.send(msg)
            flash(_('Confirmation message sent, check your e-mail'))
        if form.password.data:
            user.password = password_hash(form.password.data)
            flash(_('Password successfully changed'))
        user.language = form.language.data
        user.timezone = form.timezone.data
        db.session.commit()
        return redirect('update_account')

    return render_template('update_account.html', form=form)
Exemplo n.º 3
0
    def validate_json_source(form, field):
        """Extract the vote data from JSON source code and and construct
        object representation."""

        if not field.data:
            return
        try:
            obj = json.loads(field.data)
            if not isinstance(obj, dict):
                raise ValidationError(_('Object must be a dictionary.'))

            for k in ('title', 'desc', 'items'):
                if k not in obj:
                    raise ValidationError(
                        _('Field "%(field)s" is required in a vote.',
                          field=k)
                    )
            if 'items' not in obj or not obj['items']:
                raise ValidationError(_('No option is defined for this vote.'))

            for itm in obj['items']:
                for k in ('title', 'desc'):
                    if k not in itm:
                        raise ValidationError(
                            _('Field "%(field)s" is required in an option.',
                              field=k)
                        )
                for k in ('logo', ):
                    if k not in itm:
                        itm[k] = None
        except ValidationError:
            raise
        except Exception:
            raise ValidationError(_('Could not parse the JSON text.'))
Exemplo n.º 4
0
def forgot_password():

    form = RecoverPasswordForm()

    if form.validate_on_submit():

        user = User.query.filter_by(email=form.email.data).first()

        if user:
            flash(_("Please see your email for instructions on "
                    "how to access your account"), "success")

            user.activation_key = str(uuid.uuid4())
            db.session.commit()

            body = render_template("emails/recover_password.html",
                                   user=user)

            message = Message(subject=_("Recover your password"),
                              body=body,
                              sender=current_app.config.get(
                                  'DEFAULT_MAIL_SENDER'),
                              recipients=[user.email])

            mail.send(message)

            return redirect(url_for("frontend.index"))

        else:

            flash(_("Sorry, no user found for that email address"), "error")

    return render_template("recover_password.html", form=form)
Exemplo n.º 5
0
def upload_package(package):
    encoding = request.headers.get("Content-Encoding")
    ctype = request.headers.get("Content-Type")
    if ctype == "application/x-tar" and encoding == "gzip":
        ctype = "application/x-tar-gz"
    if ctype not in ("application/x-tar-gz", "application/x-tgz"):
        return abortify(415, message=_("Only gziped tar file is allowed."))

    force = request.headers.get("X-Yuan-Force", False)
    if package.md5 and not force:
        return abortify(444)

    package.md5 = hashlib.md5(request.data).hexdigest()
    md5 = request.headers.get("X-Package-MD5", None)
    if md5 and md5 != package.md5:
        return abortify(400, message=_("MD5 does not match."))

    filename = "%s-%s.tar.gz" % (package.name, package.version)
    directory = os.path.dirname(package.datafile)
    if not os.path.exists(directory):
        os.makedirs(directory)

    f = open(os.path.join(directory, filename), "wb")
    f.write(request.data)
    f.close()
    package.filename = filename
    package.save()
    return package
Exemplo n.º 6
0
def index():
    ip = request.remote_addr
    try:
        mac = arpreq.arpreq(ip)
    except OSError as e:
        content = render_template("error.html",
                                  message=_("An error occurred while resolving "
                                            "IP address into a MAC address."))
        return content, 500
    if mac is None:
        content = render_template("error.html",
                                  error=_("No MAC address could be found for "
                                          "your IP address {}".format(ip)))
        return content, 500
    mac_groups = get_groups(mac)
    latest_auth_attempt = get_latest_auth_attempt(mac)
    if latest_auth_attempt:
        last_auth_groups, last_auth_date = latest_auth_attempt
    else:
        last_auth_groups, last_auth_date = [], None
    reasons = [messages[group] for group in mac_groups if group in messages]
    show_mac = False
    if not mac_groups:
        reasons.append(messages['unknown'])
        show_mac = True
    if 'unknown' in last_auth_groups and mac_groups:
        reasons.append(messages['wrong_port'])
    return render_template(
        "status.html", reasons=reasons,
        mac=mac,
        show_mac=show_mac,
    )
Exemplo n.º 7
0
def community_menu():
    form = CommunityMenuForm(request.form)
    community_menu = None
    if request.method == 'POST':
       pass #validate files here
    if form.validate_on_submit():
        cleaned_data = form.data #make a copy
        cleaned_data.pop('submit',None) 
        for key in request.files.keys():
            prompt_file = request.files[key]
            filepath = save_uploaded_file(prompt_file,os.path.join(DefaultConfig.CONTENT_DIR, "community-menu", request.form['station']))
            cleaned_data[key] = "{0}/{1}/{2}".format("community-menu",request.form['station'], filepath)
            
        community_menu = CommunityMenu(**cleaned_data) #create new object from data

        db.session.add(community_menu)
        db.session.commit()


        flash(_('Configuration saved.'), 'success')

    elif request.method == "POST":
        flash(_(form.errors.items()),'error')

    return render_template('content/community_menu.html', community_menu=community_menu, form=form)
Exemplo n.º 8
0
    def login():
        if g.current_user:
            return redirect(get_next())

        form = g.login_form

        if form.validate_on_submit():
            # Find a user matching the credentials
            user = User.by_credentials(form.email_address.data,
                                       form.password.data)

            # Check if the user wants a cookie
            remember = form.remember_me.data

            # Check if a user was found and try to login
            if user and login_user(user, remember=remember):
                user.login_ip = request.remote_addr
                user.login_time = datetime.utcnow()

                flash(_('You are now logged in. Welcome back, %(user)s!', user=user))
                return redirect(get_next())
            else:
                form.email_address.errors.append(_('Login failed. Please check your eMail address.'))
                form.password.errors.append(_('Login failed. Please check your password.'))

        return render_template('login.jinja', form=form, next=get_next())
Exemplo n.º 9
0
  def validate_photo(self, field):
    if not field.has_file():
      field.data = None
      return

    data = field.data
    filename = data.filename
    valid = any(map(filename.lower().endswith, ('.png', '.jpg', '.jpeg')))

    if not valid:
      raise ValidationError(_(u'Only PNG or JPG image files are accepted'))

    img_type = imghdr.what('ignored', data.read())

    if not img_type in ('png', 'jpeg'):
      raise ValidationError(_(u'Only PNG or JPG image files are accepted'))

    data.stream.seek(0)
    try:
      # check this is actually an image file
      im = PIL.Image.open(data.stream)
      im.load()
    except:
      raise ValidationError(_(u'Could not decode image file'))

    data.stream.seek(0)
Exemplo n.º 10
0
def inventorylocation_list():
    column_labels = dict(building=_("Building#"),
        floor=_("Floor#"),
        inventory_type=_("Inventory Type"),
        location_name=_("Location Name"),
        remark=_("Remark"))
    return inventorylocationadmin.list_view(column_labels=column_labels)
Exemplo n.º 11
0
def login():
    form = LoginForm()
    if request.method == "GET":
        if current_user.is_anonymous():
            return render_template("user/login.html", form=form,
                                   error=request.args.get('error'),
                                   next_url=request.args.get('next'))
        return redirect("/")
    else:
        if form.validate_on_submit():
            username = form.username.data
            password = form.password.data
            try:
                user = apis.user.authenticate(username, password)
            except AuthenticateFailure:
                return render_template("user/login.html",
                                       error=_("invalid username or password"),
                                       form=form), 403
            if not login_user(user):
                return render_template("user/login.html",
                                       error=_("failed to login")), 403

            identity_changed.send(current_app._get_current_object(),
                                  identity=Identity(user.id))
            return redirect(request.args.get('next') or "/")
        return render_template("user/login.html",
                               error=_("please input username or password"),
                               form=form), 403
Exemplo n.º 12
0
def confirm_email():
    if current_user.is_authenticated:
        #logout_user()
        return redirect(request.args.get('next') or url_for('home.index'))
    action = request.args.get('action')
    if action == 'confirm':
        token = request.args.get('token')
        if not token:
            return render_template('feedback.html', status=False, message=_('此激活链接无效,请准确复制邮件中的链接。'))
        if RT.query.get(token):
            return render_template('feedback.html', status=False, message=_('此激活链接已被使用过。'))
        RT.add(token)
        try:
            email = ts.loads(token, salt="email-confirm-key", max_age=86400)
        except:
            abort(404)

        user = User.query.filter_by(email=email).first_or_404()
        user.confirm()
        flash(_('Your email has been confirmed'))
        login_user(user)
        return redirect(url_for('home.index'))
    elif action == 'send':
        email = request.args.get('email')
        user = User.query.filter_by(email=email).first_or_404()
        print(user)
        if not user.confirmed:
            print(email)
            send_confirm_mail(email)
        return render_template('feedback.html', status=True, message=_('邮件已经发送,请查收!'))
    else:
        abort(404)
Exemplo n.º 13
0
def signin():
    next_url = request.args.get('next') or url_for('home.index')
    if current_user.is_authenticated:
        return redirect(next_url)
    form = LoginForm()
    error = ''
    if form.validate_on_submit():
        user, status, confirmed = User.authenticate(form['username'].data,form['password'].data)
        remember = form['remember'].data
        if user:
            if status:
                #validate uesr
                login_user(user, remember=remember)
                if request.args.get('ajax'):
                    return jsonify(status=200, next=next_url)
                else:
                    return redirect(next_url)
            elif not confirmed:
                '''没有确认邮箱的用户'''
                message = '请点击邮箱里的激活链接。 <a href=%s>重发激活邮件</a>'%url_for('.confirm_email',
                    email=user.email,
                    action='send')
                if request.args.get('ajax'):
                    return jsonify(status=403, msg=message)
                else:
                    return render_template('feedback.html', status=False, message=message)
            else:
                error = _('用户名或密码错误!')
        else:
            error = _('用户名或密码错误!')
    #TODO: log the form errors
    if request.args.get('ajax'):
        return jsonify(status=404, msg=error)
    else:
        return render_template('signin.html',form=form, error=error)
Exemplo n.º 14
0
def user_update(user_id=0):
  if user_id:
    user_db = model.User.get_by_id(user_id)
  else:
    user_db = model.User(name='', username='')
  if not user_db:
    flask.abort(404)

  form = UserUpdateForm(obj=user_db)
  for permission in user_db.permissions:
    form.permissions.choices.append((permission, permission))
  form.permissions.choices = sorted(set(form.permissions.choices))
  if form.validate_on_submit():
    if not util.is_valid_username(form.username.data):
      form.username.errors.append(_('This username is invalid.'))
    elif not model.User.is_username_available(form.username.data, user_db.key):
      form.username.errors.append(_('This username is already taken.'))
    else:
      form.populate_obj(user_db)
      if auth.current_user_key() == user_db.key:
        user_db.admin = True
        user_db.active = True
      user_db.put()
      return flask.redirect(flask.url_for(
          'user_list', order='-modified', active=user_db.active,
        ))

  return flask.render_template(
      'user/user_update.html',
      title=user_db.name or _('New User'),
      html_class='user-update',
      form=form,
      user_db=user_db,
      api_url=flask.url_for('api.admin.user', user_key=user_db.key.urlsafe()) if user_db.key else ''
    )
Exemplo n.º 15
0
def signup():
    next_url = request.args.get('next', url_for('.setting'))
    token = request.args.get('token')
    if token:
        user = verify_auth_token(token, 1)
        if not user:
            flash(_('Invalid or expired token.'), 'error')
            return redirect(next_url)
        user.role = 2
        user.save()
        login_user(user)
        flash(_('This account is verified.'), 'info')
        return redirect(next_url)

    form = SignupForm()
    if form.validate_on_submit():
        user = form.save()
        login_user(user)
        # send signup mail to user
        msg = signup_mail(user)
        if current_app.debug:
            return msg.html
        flash(_('We have sent you an activate email, check your inbox.'),
              'info')
        return redirect(next_url)
    return render_template('account/signup.html', form=form)
Exemplo n.º 16
0
def massgit():
    git_ids = request.form.getlist('git')
    # pylint:disable-msg=E1101
    projects = Project.query.filter_by(
        login=g.user.login, is_github=True).all()

    pfn = {}
    for p in projects:
        if p.full_name not in git_ids:
            db.session.delete(p)
            flash(_(("Repository %(name)s successfully removed (but files"
                     " remain on the server)"), name=p.full_name))
        pfn[p.full_name] = p

    db.session.commit()
    for gid in git_ids:
        if not pfn.get(gid):
            project = Project(
                login=g.user.login,
                full_name=gid,
                # TODO: Use actual github clone string used by Github
                # clone='[email protected]:%s/%s.git' % (g.user.login, full_name),
                clone='git://github.com/%s.git' % gid,
                is_github=True
            )
            db.session.add(project)
            db.session.commit()
            flash(_("Repository successfully added"))
            project.sync()

    db.session.commit()
    return redirect(url_for('settings.repos') + "#tab_massgithub")
Exemplo n.º 17
0
def batch():
    # pylint:disable-msg=E1101
    urls = request.form.get('urls', '') + "\n"  # this makes m
    n = 0
    for l in urls.split("\n"):
        l = l.strip()
        if not l:
            continue
        if not parse(l).valid:
            flash(_("Url %(url)s isn't accepted, parse error", url=l))
        else:
            dup = Project.query.filter_by(login=g.user.login,
                                          is_github=False, clone=l).first()

            if dup:
                flash(_("Url %(url)s is duplicate", url=l))
            else:
                project = Project(login=g.user.login, clone=l, is_github=False)

                if project:
                    db.session.add(project)
                    db.session.commit()
                    db.session.refresh(project)

                project.sync()

                n = n + 1

    flash(_("%(num)s repositories successfuly added", num=n))

    return redirect(url_for('settings.repos'))
Exemplo n.º 18
0
def signup():

    form = SignupForm(next=request.args.get('next',None))

    if form.validate_on_submit():

        code = UserCode.query.filter_by(code=form.code.data).first()

        if code:
            user = User(role=code.role)
            form.populate_obj(user)

            db.session.add(user)
            db.session.delete(code)
            db.session.commit()

            identity_changed.send(current_app._get_current_object(),
                                  identity=Identity(user.id))

            flash(_("Welcome, %(name)s", name=user.nickname), "success")

            next_url = form.next.data

            if not next_url or next_url == request.path:
                next_url = url_for('frontend.blog', username=user.username)

            return redirect(next_url)
        else:
            form.code.errors.append(_("Code is not allowed"))

    return render_template("account/signup.html", form=form)
Exemplo n.º 19
0
def delhook(full_name):
    auth = github.get_session(token=g.user.token)

    old_hooks = auth.get('/repos/%s/hooks' % full_name)
    if old_hooks.status_code != 200:
        logging.error('Repos API reading error for user %s' % g.user.login)
        flash(_('GitHub API access error, please try again later'))
        return redirect(url_for('settings.repos') + "#tab_github")

    exist_id = False
    if old_hooks.json():
        for i in old_hooks.json():
            if i.has_key('name') and i['name'] == 'web':
                if i.has_key('config') and i['config'].has_key('url') \
                        and i['config']['url'] == current_app.config.get('HOOK_URL').format(id=full_name):
                    exist_id = i['id']

    if exist_id:
        resp = auth.delete('/repos/%(full_name)s/hooks/%(id)s' %
                           {'full_name': full_name, 'id': exist_id})
        if resp.status_code != 204:
            flash(_('Error deleting old webhook: Delete it manually, or retry'))
    else:
        flash(_("Webhook is not registered on Github, it was probably deleted manually"))

    # pylint:disable-msg=E1101
    project = Project.query.filter_by(
        login=g.user.login, full_name=full_name).first()

    if project:
        db.session.delete(project)
        db.session.commit()

    return redirect(url_for('settings.repos') + "#tab_github")
Exemplo n.º 20
0
def init_navigation():
    """Initilize the global layout navigation."""
    #: assertion functions
    logout = lambda: not current_user()
    login = lambda: current_user()

    #: lazy url generate
    person_home_url = lambda: url_for("account.person", id=current_user().id)

    #: build session menu items
    session_menu_items = [
            (_("Login"), url_for("account.login"), logout),
            (_("Sign Up"), url_for("account.signup"), logout),
            (_("Logout"), url_for("account.logout"), login)]

    #: build account menu items
    account_menu_items = [(_("My Home"), person_home_url, login)]

    #: register session menu
    for label, url, assertion in session_menu_items:
        nav.add_menu_item("session", label, url, assertion)

    #: register account menu
    for label, url, assertion in account_menu_items:
        nav.add_menu_item("account", label, url, assertion)
Exemplo n.º 21
0
def login():

    form = LoginForm(login=request.args.get('login',None),
                     next=request.args.get('next',None))

    if form.validate_on_submit():

        user, authenticated = User.query.authenticate(form.login.data,
                                                      form.password.data)

        if user and authenticated:
            session.permanent = form.remember.data

            identity_changed.send(current_app._get_current_object(),
                                  identity=Identity(user.id))

            flash(_("Welcome back, %(name)s", name=user.username), "success")

            next_url = form.next.data

            if not next_url or next_url == request.path:
                next_url = url_for('frontend.blog', username=user.username)

            return redirect(next_url)

        else:

            flash(_("Sorry, invalid login"), "error")

    return render_template("account/login.html", form=form)
Exemplo n.º 22
0
def reset_password():
    form = RecoverPasswordForm()

    if form.validate_on_submit():
        user = User.query.filter_by(email=form.email.data).first()

        if user:
            flash(_('Please check your email for instructions on how to access your account'), 'success')

            user.activation_key = str(uuid4())
            db.session.add(user)
            db.session.commit()

            url = url_for('user.change_password',
                email=user.email,
                activation_key=user.activation_key,
                _external=True)
            body = render_template('user/email/reset_password.txt',
                sitename=current_app.config['SITENAME'],
                username=user.name,
                url=url)
            message = Message(subject='Reset your password for ' + current_app.config['SITENAME'],
                body=body,
                recipients=[user.email])
            mail.send(message)

            return render_template('user/reset_password.html', form=form)
        else:
            flash(_('Sorry, no user found for that email address'), 'error')

    return render_template('user/reset_password.html', form=form)
Exemplo n.º 23
0
def _get_package_data(project, version=None):
    data = _get_request_data()
    if 'version' in data:
        version = data['version']
    try:
        _ver = StrictVersion(version)
    except:
        return abortify(406, message=_(
            'Invalid version %(version)s.', version=version))
    dependencies = data.get('dependencies', [])
    if not isinstance(dependencies, list):
        return abortify(406, message=_('Invalid dependencies.'))
    dct = {}
    dct['version'] = version
    dct['dependencies'] = ' '.join(dependencies)
    dct['md5value'] = data.get('md5')

    for key in ['tag', 'readme', 'download_url']:
        if data.get(key, None):
            dct[key] = data.get(key)

    if 'tag' not in dct and _ver.prerelease:
        dct['tag'] = 'unstable'
    dct['project_id'] = project.id
    return dct
Exemplo n.º 24
0
def create_user():
    form = CreateUserForm()
    if form.validate_on_submit():
        user = User(form.data['email'], form.data['password'])
        user.realname = form.data['realname']
        user.florlp_name = form.data['florlp_name']
        user.type = form.data.get('type')
        user.street = form.data['street']
        user.housenumber =  form.data['housenumber']
        user.zipcode = form.data['zipcode']
        user.city = form.data['city']
        if not form.data['verified']:
            verify = EmailVerification.verify(user)
            db.session.add(verify)
            send_mail(
                _("Email verification mail subject"),
                render_template("user/verify_mail.txt", user=user, verify=verify, _external=True),
                [user.email]
            )
        else:
            user.verified = True
            if form.data['activate']:
                user.active = True
        db.session.add(user)
        db.session.commit()

        init_user_boxes(user, current_app.config.get('COUCH_DB_URL'))

        flash(_('User created', email=user.email), 'success')
        return redirect(url_for('admin.user_list'))
    return render_template('admin/create_user.html', form=form)
Exemplo n.º 25
0
def edit_post(post_id, slug=None):
    post = Post.query.filter_by(id=post_id).first()

    # abort if no post is found
    if not post:
        abort(404)

    # check if the user has the right permissions to edit this post
    if not can_modify(post, current_user):
        flash(_("You are not allowed to delete this post."), "danger")
        return redirect(url_for("blog.index"))

    form = PostForm()
    if form.validate_on_submit():
        # this will update the changed attributes
        form.populate_obj(post)
        post.save()
        flash(_("This post has been edited"), "success")
        return redirect(url_for("blog.view_post", post_id=post.id,
                                slug=post.slug))
    else:
        form.title.data = post.title
        form.content.data = post.content

    return render_template("blog/post_form.html", post=post, form=form,
                           mode="edit")
Exemplo n.º 26
0
def feed_rss():
    q = db.session.query(Dataset)
    if not auth.account.is_admin():
        q = q.filter_by(private=False)
    feed_items = q.order_by(Dataset.created_at.desc()).limit(20)
    items = []
    for feed_item in feed_items:
        items.append({
            'title': feed_item.label,
            'pubdate': feed_item.updated_at,
            'link': url_for('dataset.view', dataset=feed_item.name),
            'description': feed_item.description,
            'author_name': ', '.join([person.fullname for person in
                                      feed_item.managers if
                                      person.fullname]),
        })
    desc = _('Recently created datasets on %(site_title)s',
             site_title=current_app.config.get('SITE_TITLE'))
    feed = Rss201rev2Feed(_('Recently Created Datasets'),
                          url_for('home.index'), desc)
    for item in items:
        feed.add_item(**item)
    sio = StringIO()
    feed.write(sio, 'utf-8')
    return Response(sio.getvalue(), mimetype='application/xml')
Exemplo n.º 27
0
def twitter():

    if g.user.twitter:
        flash(_("You twitter's access token is already exists"), "error")
        return redirect(url_for('frontend.people', username=g.user.username))

    consumer_key = current_app.config['TWITTER_KEY']
    consumer_secret = current_app.config['TWITTER_SECRET']

    signature_method_hmac_sha1 = oauth.SignatureMethod_HMAC_SHA1()
    oauth_consumer             = oauth.Consumer(key=consumer_key, secret=consumer_secret)
    oauth_client               = oauth.Client(oauth_consumer)
    
    try:
        resp, content = oauth_client.request(REQUEST_TOKEN_URL, 'GET')
    except AttributeError:
        flash(_("Can not connect twitter.com"))
        return redirect(url_for('frontend.people',username=g.user.username))

    if resp['status'] != '200':
        return 'Invalid respond from Twitter requesting temp token: %s' % resp['status']
    else:
        request_token = dict(parse_qsl(content))

        session['token'] = request_token

        return redirect('%s?oauth_token=%s' % (AUTHORIZATION_URL.replace("https:","http:"), 
                                               request_token['oauth_token']))
Exemplo n.º 28
0
def program_add():
    form = ProgramForm(request.form)
    program = None
    
    #hosts in my network
    hosts = Person.query.join(Person, Network.people).join(User, Network.networkusers).filter(User.id == current_user.id).all()
    news = ContentTrack.query.join(ContentType).filter(ContentType.name=="News").all()
    ads = ContentTrack.query.join(ContentType).filter(ContentType.name=="Advertisements").all()
    medias = ContentTrack.query.join(ContentType).filter(ContentType.name=="Media").all()
    podcasts = ContentPodcast.query.all()
    community_contents = {"data":[{"type":"Ads", "category_id":"1"},{"type":"Announcements", "category_id":"2"},{"type":"Greetings", "category_id":"3"}]}

    if form.validate_on_submit():
        cleaned_data = form.data #make a copy
        cleaned_data.pop('submit',None) #remove submit field from list
        cleaned_data.pop('program_structure')
        cleaned_data['program_type_id'] = 1
        program = Program(**cleaned_data) #create new object from data
        
        db.session.add(program)
        db.session.commit()
        flash(_('Program added.'), 'success') 
    elif request.method == "POST":
        flash(_('Validation error'),'error')

    return render_template('radio/program.html', program=program,hosts=hosts,news=news,podcasts=podcasts, ads=ads, medias=medias, community_contents=community_contents["data"], form=form)
Exemplo n.º 29
0
def profile_password():
  if not config.CONFIG_DB.has_email_authentication:
    flask.abort(418)
  user_db = auth.current_user_db()
  form = ProfilePasswordForm(obj=user_db)

  if form.validate_on_submit():
    errors = False
    old_password = form.old_password.data
    new_password = form.new_password.data
    if new_password or old_password:
      if user_db.password_hash:
        if util.password_hash(user_db, old_password) != user_db.password_hash:
          form.old_password.errors.append(_('Invalid current password'))
          errors = True
      if not errors and old_password and not new_password:
        form.new_password.errors.append(_('This field is required.'))
        errors = True

      if not (form.errors or errors):
        user_db.password_hash = util.password_hash(user_db, new_password)
        flask.flash(__('Your password has been changed.'), category='success')

    if not (form.errors or errors):
      user_db.put()
      return flask.redirect(flask.url_for('profile'))

  return flask.render_template(
      'profile/profile_password.html',
      title=user_db.name,
      html_class='profile-password',
      form=form,
      user_db=user_db,
    )
Exemplo n.º 30
0
def login():
    if current_user.is_authenticated():
        return redirect(url_for('admin.dashboard'))

    form = LoginForm(login=request.args.get('login', None),
                     next=request.args.get('next', None))

    if form.validate_on_submit():
        user, authenticated = User.authenticate(form.login.data,
                                                form.password.data)

        if user and authenticated:
            remember = request.form.get('remember') == 'y'
            if login_user(user, remember=remember):
                flash(_("Logged in"), 'success')
            else:
                flash(_("Unable to log in"), 'warning')

            user.last_accessed = datetime.now()
            db.session.add(user)
            db.session.commit()

            return redirect(form.next.data or url_for('admin.dashboard'))
        else:
            flash(_('Sorry, invalid login'), 'warning')

    return render_template('user/login.html', form=form)
Exemplo n.º 31
0
 def validate_email(self, field):
     if User.query.filter_by(email=field.data).first() is not None:
         raise ValidationError(_('This email is already registered'))
Exemplo n.º 32
0
    def create(self, gid, sid, did, scid, tid, exid=None):
        """
        This function will create a Exclusion constraint.

        Args:
          gid: Server Group ID
          sid: Server ID
          did: Database ID
          scid: Schema ID
          tid: Table ID
          exid: Exclusion constraint ID

        Returns:

        """
        required_args = ['columns']

        data = request.form if request.form else json.loads(
            request.data.decode())

        for k, v in data.items():
            try:
                data[k] = json.loads(v)
            except (ValueError, TypeError):
                data[k] = v

        for arg in required_args:
            if arg not in data:
                return make_json_response(
                    status=400,
                    success=0,
                    errormsg=_("Couldn't find required parameter (%s)." %
                               str(arg)))
            elif isinstance(data[arg], list) and len(data[arg]) < 1:
                return make_json_response(
                    status=400,
                    success=0,
                    errormsg=_("Couldn't find required parameter (%s)." %
                               str(arg)))

        data['schema'] = self.schema
        data['table'] = self.table
        try:
            if 'name' not in data or data['name'] == "":
                SQL = render_template("/".join(
                    [self.template_path, 'begin.sql']))
                # Start transaction.
                status, res = self.conn.execute_scalar(SQL)
                if not status:
                    self.end_transaction()
                    return internal_server_error(errormsg=res)

            # The below SQL will execute CREATE DDL only
            SQL = render_template("/".join([self.template_path, 'create.sql']),
                                  data=data,
                                  conn=self.conn)
            status, res = self.conn.execute_scalar(SQL)
            if not status:
                self.end_transaction()
                return internal_server_error(errormsg=res)

            if 'name' not in data or data['name'] == "":
                sql = render_template("/".join(
                    [self.template_path, 'get_oid_with_transaction.sql']),
                                      tid=tid)

                status, res = self.conn.execute_dict(sql)
                if not status:
                    self.end_transaction()
                    return internal_server_error(errormsg=res)

                self.end_transaction()

                data['name'] = res['rows'][0]['name']

            else:
                sql = render_template("/".join(
                    [self.template_path, 'get_oid.sql']),
                                      name=data['name'])
                status, res = self.conn.execute_dict(sql)
                if not status:
                    self.end_transaction()
                    return internal_server_error(errormsg=res)

            return jsonify(node=self.blueprint.generate_browser_node(
                res['rows'][0]['oid'],
                tid,
                data['name'],
                icon="icon-exclusion_constraint"))

        except Exception as e:
            self.end_transaction()

            return make_json_response(status=400, success=0, errormsg=e)
Exemplo n.º 33
0
class ExclusionConstraintModule(ConstraintTypeModule):
    """
    class ForeignKeyConstraintModule(CollectionNodeModule)

        A module class for Exclusion constraint node derived from ConstraintTypeModule.

    Methods:
    -------
    * __init__(*args, **kwargs)
      - Method is used to initialize the ForeignKeyConstraintModule and it's base module.

    * get_nodes(gid, sid, did)
      - Method is used to generate the browser collection node.

    * node_inode()
      - Method is overridden from its base class to make the node as leaf node.

    * script_load()
      - Load the module script for language, when any of the database node is
        initialized.
    """

    NODE_TYPE = 'exclusion_constraint'
    COLLECTION_LABEL = _("Exclusion Constraints")

    def __init__(self, *args, **kwargs):
        """
        Method is used to initialize the ForeignKeyConstraintModule and it's base module.

        Args:
          *args:
          **kwargs:

        Returns:

        """
        self.min_ver = None
        self.max_ver = None
        super(ExclusionConstraintModule, self).__init__(*args, **kwargs)

    def get_nodes(self, gid, sid, did, scid, tid):
        """
        Generate the collection node
        """
        pass

    @property
    def node_inode(self):
        """
        Override this property to make the node a leaf node.

        Returns: False as this is the leaf node
        """
        return False

    @property
    def script_load(self):
        """
        Load the module script for exclusion_constraint, when any of the table node is
        initialized.

        Returns: node type of the server module.
        """
        return database.DatabaseModule.NODE_TYPE
Exemplo n.º 34
0
from project.database import db_session
from . import mod


@mod.route('/')
@login_required
def userindex():
    try:
        return 'You are loggined as ' + g.user.username
    except:
        return redirect(url_for('user.login'))


@mod.route('/login/', methods=['GET', 'POST'])
#@not_login_required
@title(_('login'))
def login():
    """
    """
    if request.method != 'POST':
        form = LoginForm()
        return render('user/login.html', login_form=form)

    next = request.args.get('next', None)
    form = LoginForm(request.form)
    form.validate()

    username = form.data['username']
    password = form.data['password']
    if username != '':
        try:
Exemplo n.º 35
0
class LoginForm(Form):

    email = TextField(_('Email'), [Required(), Email()])
    password = PasswordField(_('Password'), [Required()])
Exemplo n.º 36
0
def index():
    return bad_request(errormsg=_('This URL can not be called directly.'))
Exemplo n.º 37
0
class RecoverPasswordForm(Form):
    email = EmailField(_('Your email'), [Email()])
    submit = SubmitField(_('Send instructions'))
Exemplo n.º 38
0
class ContentTrackForm(ContentTrackFormBase):
    name = StringField(u'Name', [Required()])
    description = TextAreaField()
    submit = SubmitField(_('Save'))
Exemplo n.º 39
0
class CleverAuthenticationAPI(OAuthAuthenticationProvider):

    URI = "http://librarysimplified.org/terms/auth/clever"

    NAME = 'Clever'

    DESCRIPTION = _("""
        An authentication service for Open eBooks that uses Clever as an
        OAuth provider.""")

    SETTINGS = [
        {
            "key": ExternalIntegration.USERNAME,
            "label": _("Client ID")
        },
        {
            "key": ExternalIntegration.PASSWORD,
            "label": _("Client Secret")
        },
    ] + OAuthAuthenticationProvider.SETTINGS

    # Unlike other authentication providers, external type regular expression
    # doesn't make sense for Clever. This removes the LIBRARY_SETTINGS from the
    # parent class.
    LIBRARY_SETTINGS = []

    TOKEN_TYPE = "Clever token"
    TOKEN_DATA_SOURCE_NAME = 'Clever'

    EXTERNAL_AUTHENTICATE_URL = "https://clever.com/oauth/authorize?response_type=code&client_id=%(client_id)s&redirect_uri=%(oauth_callback_url)s&state=%(state)s"
    CLEVER_TOKEN_URL = "https://clever.com/oauth/tokens"
    CLEVER_API_BASE_URL = "https://api.clever.com"

    # To check Title I status we need state, which is associated with
    # a school in Clever's API. Any users at the district-level will
    # need to get a code from First Book instead.
    SUPPORTED_USER_TYPES = ['student', 'teacher']

    # Begin implementations of OAuthAuthenticationProvider abstract
    # methods.

    def oauth_callback(self, _db, code):
        """Verify the incoming parameters with the OAuth provider. Exchange
        the authorization code for an access token. Create or look up
        appropriate database records.

        :param code: The authorization code generated by the
        authorization server, as per section 4.1.2 of RFC 6749. This
        method will exchange the authorization code for an access
        token.

        :return: A ProblemDetail if there's a problem. Otherwise, a
        3-tuple (Credential, Patron, PatronData). The Credential
        contains the access token provided by the OAuth provider. The
        Patron object represents the authenticated Patron, and the
        PatronData object includes information about the patron
        obtained from the OAuth provider which cannot be stored in the
        circulation manager's database, but which should be passed on
        to the client.

        """
        # Ask the OAuth provider to verify the code that was passed
        # in.  This will give us a bearer token we can use to look up
        # detailed patron information.
        token = self.remote_exchange_code_for_bearer_token(_db, code)
        if isinstance(token, ProblemDetail):
            return token

        # Now that we have a bearer token, use it to look up patron
        # information.
        patrondata = self.remote_patron_lookup(token)
        if isinstance(patrondata, ProblemDetail):
            return patrondata

        # Convert the PatronData into a Patron object.
        patron, is_new = patrondata.get_or_create_patron(_db, self.library_id)

        # Create a credential for the Patron.
        credential, is_new = self.create_token(_db, patron, token)
        return credential, patron, patrondata

    # End implementations of OAuthAuthenticationProvider abstract
    # methods.

    def remote_exchange_code_for_bearer_token(self, _db, code):
        """Ask the OAuth provider to convert a code (passed in to the OAuth
        callback) into a bearer token.

        We can use the bearer token to act on behalf of a specific
        patron. It also gives us confidence that the patron
        authenticated correctly with Clever.

        :return: A ProblemDetail if there's a problem; otherwise, the
        bearer token.
        """
        payload = self._remote_exchange_payload(_db, code)
        authorization = base64.b64encode(self.client_id + ":" +
                                         self.client_secret)
        headers = {
            'Authorization': 'Basic %s' % authorization,
            'Content-Type': 'application/json',
        }
        response = self._get_token(payload, headers)
        invalid = INVALID_CREDENTIALS.detailed(
            _("A valid Clever login is required."))
        if not response:
            return invalid
        token = response.get('access_token', None)
        if not token:
            return invalid
        return token

    def _remote_exchange_payload(self, _db, code):
        library = self.library(_db)
        return dict(
            code=code,
            grant_type='authorization_code',
            redirect_uri=OAuthController.oauth_authentication_callback_url(
                library.short_name))

    def remote_patron_lookup(self, token):
        """Use a bearer token to look up detailed patron information.

        :return: A ProblemDetail if there's a problem. Otherwise, a
        PatronData.
        """
        bearer_headers = {'Authorization': 'Bearer %s' % token}
        result = self._get(self.CLEVER_API_BASE_URL + '/me', bearer_headers)
        data = result.get('data', {})

        identifier = data.get('id', None)

        if not identifier:
            return INVALID_CREDENTIALS.detailed(
                _("A valid Clever login is required."))

        if result.get('type') not in self.SUPPORTED_USER_TYPES:
            return UNSUPPORTED_CLEVER_USER_TYPE

        links = result['links']

        user_link = [l for l in links if l['rel'] == 'canonical'][0]['uri']
        user = self._get(self.CLEVER_API_BASE_URL + user_link, bearer_headers)

        user_data = user['data']
        school_id = user_data['school']
        school = self._get(
            self.CLEVER_API_BASE_URL + '/v1.1/schools/%s' % school_id,
            bearer_headers)

        school_nces_id = school['data'].get('nces_id')

        # TODO: check student free and reduced lunch status as well

        if school_nces_id not in TITLE_I_NCES_IDS:
            self.log.info("%s didn't match a Title I NCES ID" % school_nces_id)
            return CLEVER_NOT_ELIGIBLE

        if result['type'] == 'student':
            grade = user_data.get('grade')
            external_type = None
            if grade in ["Kindergarten", "1", "2", "3"]:
                external_type = "E"
            elif grade in ["4", "5", "6", "7", "8"]:
                external_type = "M"
            elif grade in ["9", "10", "11", "12"]:
                external_type = "H"
        else:
            external_type = "A"

        patrondata = PatronData(permanent_id=identifier,
                                authorization_identifier=identifier,
                                external_type=external_type,
                                personal_name=user_data.get('name'),
                                complete=True)
        return patrondata

    def _get_token(self, payload, headers):
        response = HTTP.post_with_timeout(self.CLEVER_TOKEN_URL,
                                          json.dumps(payload),
                                          headers=headers)
        return response.json()

    def _get(self, url, headers):
        return HTTP.get_with_timeout(url, headers=headers).json()
Exemplo n.º 40
0
 def validate_name(self, field):
     if User.query.filter_by(name=field.data).first() is not None:
         raise ValidationError(_('This username is already registered'))
Exemplo n.º 41
0
 def twitter_url(self):
     link = u"http://atlas.media.mit.edu{}".format(self.attr.get_profile_url())
     lang_txt = u"&lang={}".format(g.locale) if g.locale != "en" else ""
     title = u"{} {}".format(self.attr.get_name(), _('Profile'))
     return u"https://twitter.com/share?url={}{}&text={}&hashtags=oec,{}" \
             .format(link, lang_txt, title, self.attr.get_name())
Exemplo n.º 42
0
class ContentPodcastForm(Form):
    name = StringField('Name of the stream', [Required()])
    uri = StringField('URL', [Required()])
    description = TextAreaField('Description')
    submit = SubmitField(_('Save'))
Exemplo n.º 43
0
    def sections(self):
        sections = []
        ''' Trade Section
        '''

        if self.attr.id == "xxwld":

            export_tmap = Build("tree_map", "hs92", "export", self.attr, "all", "show", self.year)
            this_yo = self.models.Yo.query.filter_by(year = self.year).all()

            export_val = sum([o.export_val for o in this_yo])
            export_subtitle = _(u"The total world trade in %(year)s was %(export_val)s.", year=self.year, export_val=num_format(export_val, "export_val"))
            export_subtitle += u" "

            past_yr = self.year - 5
            past_yo = self.models.Yo.query.filter_by(year = past_yr).all()
            growth_val = median([o.export_val_growth_pct_5 for o in this_yo])
            chg = "increased" if growth_val >= 0 else "decreased"
            export_subtitle += _(u"During the last five years exports have %(increased_decreased)s at a median annualized rate of %(change_rate)s%%, from $%(past_export_val)s in %(past_year)s to $%(current_export_val)s in %(current_year)s.",
                                    increased_decreased=chg, change_rate=num_format(growth_val*100), \
                                    past_export_val=num_format(sum([o.export_val for o in past_yo])), past_year=past_yr, current_export_val=num_format(export_val), current_year=self.year)
            export_subtitle += u" "

            top_exports = self.models.Yp.query.filter_by(year = self.year, hs92_id_len=6).order_by(desc("export_val")).limit(2).all()
            export_subtitle += _(u"The most recent exports are led by %(top_export)s which represent %(top_export_pct)s%% of the total products exported, followed by %(second_export)s, which account for %(second_export_pct)s%%.",
                                    top_export=top_exports[0].product.get_profile_link(), top_export_pct=num_format((top_exports[0].export_val/export_val)*100), \
                                    second_export=top_exports[1].product.get_profile_link(), second_export_pct=num_format((top_exports[1].export_val/export_val)*100))
            
            origins_tmap = Build("tree_map", "hs92", "import", self.attr, "show", "all", self.year)
            yo_exp = self.models.Yo.query.filter_by(year = self.year).order_by(desc("export_val")).limit(5).all()
            origin_list = self.stringify_items(yo_exp, "export_val", "country")
            origin_subtitle = _(u"The top exporters globally are %(origins)s.", origins=origin_list)

            trade_section = {
                "builds": [
                    {"title": _(u"Exports"), "build": export_tmap, "subtitle": export_subtitle},
                    {"title": _(u"Origins"), "build": origins_tmap, "subtitle": origin_subtitle},
                ]
            }

        else:

            export_subtitle, import_subtitle, dest_subtitle, origin_subtitle = [None]*4

            export_tmap = Build("tree_map", "hs92", "export", self.attr, "all", "show", self.year)
            import_tmap = Build("tree_map", "hs92", "import", self.attr, "all", "show", self.year)

            yop_base = self.models.Yop.query.filter_by(year = self.year, origin = self.attr, hs92_id_len=6)
            # get growth
            past_yr = self.year - 5
            past_yo = self.models.Yo.query.filter_by(year = past_yr, country = self.attr).first()
            this_yo = self.models.Yo.query.filter_by(year = self.year, country = self.attr).first()
            exp_val_stat = filter(lambda s: s["key"] == "export_val", self.stats())
            if exp_val_stat:
                exp_val_stat = exp_val_stat.pop()
                export_subtitle = ""
                if self.attr.id != "xxwld":
                    exp_rank = num_format(exp_val_stat["rank"], "ordinal") if exp_val_stat["rank"] > 1 else ""
                    export_subtitle += _(u"In %(year)s %(country)s exported $%(export_val)s, making it the %(export_rank)s largest exporter in the world.",
                                        year=self.year, country=self.attr.get_name(article=True), export_val=num_format(exp_val_stat["val"]), export_rank=exp_rank)
                    export_subtitle += u" "
                if past_yo:
                    chg = "increased" if this_yo.export_val_growth_pct_5 >= 0 else "decreased"
                    export_subtitle += _(u"During the last five years the exports %(of_country)s have %(increased_decreased)s at an annualized rate of %(change_rate)s%%, from $%(past_export_val)s in %(past_year)s to $%(current_export_val)s in %(current_year)s.",
                                            of_country=self.attr.get_name(article="of"), increased_decreased=chg, change_rate=num_format(this_yo.export_val_growth_pct_5*100), \
                                            past_export_val=num_format(past_yo.export_val), past_year=past_yr, current_export_val=num_format(this_yo.export_val), current_year=self.year)
                    export_subtitle += u" "
                top_exports = yop_base.order_by(desc("export_val")).limit(2).all()
                if top_exports:
                    # raise Exception(top_exports[0].product.get_profile_link(), num_format((top_exports[0].export_val/exp_val_stat["val"])*100), self.attr.get_name(article="of"), top_exports[1].product.get_profile_link(), num_format((top_exports[1].export_val/exp_val_stat["val"])*100))
                    export_subtitle += _(u"The most recent exports are led by %(top_export)s which represent %(top_export_pct)s%% of the total exports %(of_country)s, followed by %(second_export)s, which account for %(second_export_pct)s%%.",
                                            top_export=top_exports[0].product.get_profile_link(), top_export_pct=num_format((top_exports[0].export_val/exp_val_stat["val"])*100), \
                                            of_country=self.attr.get_name(article="of"), second_export=top_exports[1].product.get_profile_link(), second_export_pct=num_format((top_exports[1].export_val/exp_val_stat["val"])*100))
            imp_val_stat = filter(lambda s: s["key"] == "import_val", self.stats())
            if imp_val_stat:
                imp_val_stat = imp_val_stat.pop()
                import_subtitle = ""
                if self.attr.id != "xxwld":
                    imp_rank = num_format(imp_val_stat["rank"], "ordinal") if imp_val_stat["rank"] > 1 else ""
                    import_subtitle += _(u"In %(year)s %(country)s imported $%(import_val)s, making it the %(import_rank)s largest importer in the world.",
                                        year=self.year, country=self.attr.get_name(article=True), import_val=num_format(imp_val_stat["val"]), import_rank=imp_rank)
                    import_subtitle += u" "
                if past_yo:
                    chg = "increased" if this_yo.import_val_growth_pct_5 >= 0 else "decreased"
                    import_subtitle += _(u"During the last five years the imports %(of_country)s have %(increased_decreased)s at an annualized rate of %(change_rate)s%%, from $%(past_import_val)s in %(past_year)s to $%(current_import_val)s in %(current_year)s.",
                                            of_country=self.attr.get_name(article="of"), increased_decreased=chg, change_rate=num_format(this_yo.import_val_growth_pct_5*100), \
                                            past_import_val=num_format(past_yo.import_val), past_year=past_yr, current_import_val=num_format(this_yo.import_val), current_year=self.year)
                    import_subtitle += u" "
                top_imports = yop_base.order_by(desc("import_val")).limit(2).all()
                if top_imports:
                    import_subtitle += _(u"The most recent imports are led by %(top_import)s which represent %(top_import_pct)s%% of the total imports %(of_country)s, followed by %(second_import)s, which account for %(second_import_pct)s%%.",
                                            top_import=top_imports[0].product.get_profile_link(), top_import_pct=num_format((top_imports[0].import_val/imp_val_stat["val"])*100), \
                                            of_country=self.attr.get_name(article="of"), second_import=top_imports[1].product.get_profile_link(), second_import_pct=num_format((top_imports[1].import_val/imp_val_stat["val"])*100))

            dests_tmap = Build("tree_map", "hs92", "export", self.attr, "show", "all", self.year)
            yod_exp = self.models.Yod.query.filter_by(year = self.year, origin = self.attr).order_by(desc("export_val")).limit(5).all()
            if yod_exp:
                dest_list = self.stringify_items(yod_exp, "export_val", "dest")
                dest_subtitle = _(u"The top export destinations %(of_country)s are %(destinations)s.", of_country=self.attr.get_name(article="of"), destinations=dest_list)

            origins_tmap = Build("tree_map", "hs92", "import", self.attr, "show", "all", self.year)
            yod_imp = self.models.Yod.query.filter_by(year = self.year, dest = self.attr).order_by(desc("export_val")).limit(5).all()
            if yod_imp:
                origin_list = self.stringify_items(yod_imp, "export_val", "origin")
                origin_subtitle = _(u"The top import origins %(of_country)s are %(origins)s.", of_country=self.attr.get_name(article="of"), origins=origin_list)

            # trade balance viz --
            first_yo = self.models.Yo.query.filter_by(year = available_years["hs92"][-1], country = self.attr).first()
            tb_subtitle = ""
            tb_build = Build("line", "hs92", "show", self.attr, "all", "all", available_years["hs92"])
            if first_yo:
                net_trade = this_yo.export_val - this_yo.import_val
                trade_balance = _("positive") if net_trade >= 0 else _("negative")
                trade_direction = _("exports") if net_trade >= 0 else _("imports")
                tb_subtitle = _(u"As of %(year)s %(country)s had a %(positive_negative)s trade balance of $%(net_trade)s in net %(exports_imports)s.",
                                year=self.year, country=self.attr.get_name(article=True), positive_negative=trade_balance, net_trade=num_format(abs(net_trade)), exports_imports=trade_direction)
                old_yo = self.models.Yo.query.filter_by(year = available_years["hs92"][0], country = self.attr).first()
                if old_yo:
                    old_net_trade = old_yo.export_val - old_yo.import_val
                    old_trade_balance = _("positive") if old_net_trade >= 0 else _("negative")
                    old_trade_direction = _("exports") if old_net_trade >= 0 else _("imports")
                    is_diff = True if old_trade_balance != trade_balance else False
                    still_or_not = _("still") if old_trade_balance == trade_balance else ""
                    tb_subtitle += u" "
                    tb_subtitle += _(u"As compared to their trade balance in %(year)s when they %(still)s had a %(positive_negative)s trade balance of $%(net_trade)s in net %(exports_imports)s.",
                                    year=available_years["hs92"][0], still=still_or_not, positive_negative=old_trade_balance, net_trade=num_format(abs(old_net_trade)), exports_imports=old_trade_direction)

            trade_section = {
                "builds": [
                    {"title": _(u"Exports"), "build": export_tmap, "subtitle": export_subtitle, "tour":"This is just a test", "seq":5},
                    {"title": _(u"Imports"), "build": import_tmap, "subtitle": import_subtitle},
                    {"title": _(u"Trade Balance"), "build": tb_build, "subtitle": tb_subtitle},
                    {"title": _(u"Destinations"), "build": dests_tmap, "subtitle": dest_subtitle},
                    {"title": _(u"Origins"), "build": origins_tmap, "subtitle": origin_subtitle},
                ]
            }

        sections.append(trade_section)

        ''' Product Space Section
        '''
        subtitle = False
        if self.attr.id != "xxwld":
            num_exports_w_rca = db.session.query(func.count(self.models.Yop.hs92_id)) \
                        .filter_by(year = self.year, origin = self.attr) \
                        .filter(self.models.Yop.export_rca >= 1) \
                        .filter(func.char_length(self.models.Yop.hs92_id)==6) \
                        .scalar()
            this_attr_yo = attrs.Yo.query.filter_by(year = self.year, country = self.attr).first()
            if this_attr_yo:
                eci = this_attr_yo.eci
                eci_rank = this_attr_yo.eci_rank
                if eci_rank:
                    subtitle = _(u"The economy %(of_country)s has an Economic Complexity Index (ECI) of %(eci)s making it the %(eci_rank)s most complex country.",
                                of_country=self.attr.get_name(article="of"), eci=num_format(eci), eci_rank=num_format(eci_rank, "ordinal"))
                    subtitle += u" "
                else:
                    subtitle = ""
                subtitle += _(u"%(country)s exports %(num_of_exports)s products with revealed comparative advantage " \
                    u"(meaning that its share of global exports is larger than what " \
                    u"would be expected from the size of its export economy " \
                    u"and from the size of a product’s global market).",
                    country=self.attr.get_name(article=True), num_of_exports=num_exports_w_rca)
        product_space = Build("network", "hs92", "export", self.attr, "all", "show", self.year)
        ps_text = _(u"The product space is a network connecting products that are likely to be co-exported and can be used to predict the evolution of a country’s export structure.")
        if subtitle:
            ps_text = u"{}</p><p>{}".format(ps_text, subtitle)
        ps_section = {
            "title": _(u"Economic Complexity %(of_country)s", of_country=self.attr.get_name(article="of")),
            "builds": [
                {"title": _(u"Product Space"), "build": product_space, "subtitle": ps_text, "tour":"The product space...", "seq":6}
            ]
        }

        ''' ECI Ranking Section
        '''
        if self.attr.id == "xxwld":
            line_rankings = Build("line", "sitc", "eci", "show", "all", "all", [y for y in available_years["sitc"] if y >= 1964])
            start_year = 1980
            start_year = max(1964, start_year) if start_year != 1980 else 1964
            year_range = self.year - start_year
            subtitle = _("The Economic Complexity of each country visualized over the past %(year_range)s years.", year_range=year_range)
            ps_section["builds"].append({"title": _(u"Economic Complexity Ranking"), "build": line_rankings, "subtitle": subtitle})

        elif this_attr_yo and this_attr_yo.eci != None:
            line_rankings = Build("line", "sitc", "eci", "show", self.attr, "all", [y for y in available_years["sitc"] if y >= 1964])
            start_year = earliest_data.get(self.attr.id, 1980)
            start_year = max(1964, start_year) if start_year != 1980 else 1964
            year_range = self.year - start_year

            attr_yo_historic = attrs.Yo.query.filter_by(country=self.attr).filter(attrs.Yo.year == start_year).first()
            if attr_yo_historic.eci_rank:
                eci_delta = this_attr_yo.eci_rank - attr_yo_historic.eci_rank
                inc_dec = _('increased') if eci_delta < 0 else _('decreased')
                subtitle = _("The Economic Complexity ranking %(of_country)s has %(increased_or_decreased)s by %(rank_delta)s places over the past %(year_range)s years from %(old_eci)s in %(old_year)s to %(current_eci)s in %(current_year)s.",
                    of_country=self.attr.get_name(article="of"), increased_or_decreased=inc_dec,
                    rank_delta=abs(eci_delta), year_range=year_range, old_eci=num_format(attr_yo_historic.eci_rank, "ordinal"),
                    old_year=start_year, current_eci=num_format(this_attr_yo.eci_rank, "ordinal"), current_year=self.year)
            ps_section["builds"].append({"title": _(u"Economic Complexity Ranking"), "build": line_rankings, "subtitle": subtitle})
        sections.append(ps_section)

        sections.append({
            "title": _(u"More on %(country)s from our other sites", country=self.attr.get_name(article=True)),
            "source": "sisters"
        })

        ''' DataViva
        '''
        dv_section = make_dv_section(self)
        sections.append(dv_section)

        ''' Data USA
        '''
        if self.attr.id == "nausa":
            us_section = make_us_section()
            sections.append(us_section)

        ''' Pantheon
        '''
        pantheon_id = "all" if self.attr.id == "xxwld" else self.attr.id_2char
        if pantheon_id:
            if self.attr.id != "xxwld":
                pantheon_id = pantheon_id.upper()
            
            pantheon_section = make_pantheon_section(pantheon_id, self.attr)
            sections.append(pantheon_section)
        
        return sections
Exemplo n.º 44
0
from api.config import Configuration
from core.model import (
    get_one,
    get_one_or_create,
    Credential,
    DataSource,
    ExternalIntegration,
    Patron,
)
from core.util.http import HTTP
from api.problem_details import *

UNSUPPORTED_CLEVER_USER_TYPE = pd(
    "http://librarysimplified.org/terms/problem/unsupported-clever-user-type",
    401,
    _("Your Clever user type is not supported."),
    _("Your Clever user type is not supported. You can request a code from First Book instead"
      ),
)

CLEVER_NOT_ELIGIBLE = pd(
    "http://librarysimplified.org/terms/problem/clever-not-eligible",
    401,
    _("Your Clever account is not eligible to access this application."),
    _("Your Clever account is not eligible to access this application."),
)

# Load Title I NCES ID data from json.
TITLE_I_NCES_IDS = None
clever_dir = os.path.split(__file__)[0]
Exemplo n.º 45
0
class BaseCirculationAPI(object):
    """Encapsulates logic common to all circulation APIs."""

    DEFAULT_LOAN_PERIOD = "default_loan_period"
    DEFAULT_RESERVATION_PERIOD = "default_reservation_period"

    SETTINGS = [
        {
            "key": DEFAULT_LOAN_PERIOD,
            "label": _("Default Loan Period (in Days)"),
            "optional": True,
            "type": "number"
        },
        {
            "key": DEFAULT_RESERVATION_PERIOD,
            "label": _("Default Reservation Period (in Days)"),
            "optional": True,
            "type": "number"
        },
    ]

    BORROW_STEP = 'borrow'
    FULFILL_STEP = 'fulfill'

    # In 3M only, when a book is in the 'reserved' state the patron
    # cannot revoke their hold on the book.
    CAN_REVOKE_HOLD_WHEN_RESERVED = True

    # If the client must set a delivery mechanism at the point of
    # checkout (Axis 360), set this to BORROW_STEP. If the client may
    # wait til the point of fulfillment to set a delivery mechanism
    # (Overdrive), set this to FULFILL_STEP. If there is no choice of
    # delivery mechanisms (3M), set this to None.
    SET_DELIVERY_MECHANISM_AT = FULFILL_STEP

    # Different APIs have different internal names for delivery
    # mechanisms. This is a mapping of (content_type, drm_type)
    # 2-tuples to those internal names.
    #
    # For instance, the combination ("application/epub+zip",
    # "vnd.adobe/adept+xml") is called "ePub" in Axis 360 and 3M, but
    # is called "ebook-epub-adobe" in Overdrive.
    delivery_mechanism_to_internal_format = {}

    def internal_format(self, delivery_mechanism):
        """Look up the internal format for this delivery mechanism or
        raise an exception.
        """
        if not delivery_mechanism:
            return None
        d = delivery_mechanism.delivery_mechanism
        key = (d.content_type, d.drm_scheme)
        internal_format = self.delivery_mechanism_to_internal_format.get(key)
        if not internal_format:
            raise DeliveryMechanismError(
                _("Could not map Simplified delivery mechanism %(mechanism_name)s to internal delivery mechanism!",
                  mechanism_name=d.name))
        return internal_format

    def default_notification_email_address(self, patron, pin):
        """What email address should be used to notify this patron
        of changes?
        """
        return ConfigurationSetting.for_library(
            Configuration.DEFAULT_NOTIFICATION_EMAIL_ADDRESS,
            patron.library).value

    def checkin(self, patron, pin, licensepool):
        """  Return a book early.  

        :param patron: a Patron object for the patron who wants
        to check out the book.
        :param pin: The patron's alleged password.
        :param licensepool: Contains lending info as well as link to parent Identifier.
        """
        pass

    def checkout(self, patron, pin, licensepool, internal_format):
        """Check out a book on behalf of a patron.

        :param patron: a Patron object for the patron who wants
        to check out the book.
        :param pin: The patron's alleged password.
        :param licensepool: Contains lending info as well as link to parent Identifier.
        :param internal_format: Represents the patron's desired book format.

        :return: a LoanInfo object.
        """
        raise NotImplementedError()

    def fulfill(self, patron, pin, licensepool, internal_format):
        """ Get the actual resource file to the patron.
        :return a FulfillmentInfo object.
        """
        raise NotImplementedError()

    def patron_activity(self, patron, pin):
        """ Return a patron's current checkouts and holds.
        """
        raise NotImplementedError()

    def place_hold(self, patron, pin, licensepool, notification_email_address):
        """Place a book on hold.

        :return: A HoldInfo object
        """
        raise NotImplementedError()

    def release_hold(self, patron, pin, licensepool):
        """Release a patron's hold on a book.

        :raises CannotReleaseHold: If there is an error communicating
        with the provider, or the provider refuses to release the hold for
        any reason.
        """
        raise NotImplementedError()
Exemplo n.º 46
0
    def intro(self):
        all_paragraphs = []

        ''' Paragraph #2
        '''
        # get total world trade rank
        this_yp = self.models.Yp.query.filter_by(year = self.year, product = self.attr).first()
        all_yp = self.models.Yp.query.filter_by(year = self.year) \
                    .filter(func.char_length(getattr(self.models.Yp, "{}_id".format(self.classification))) == len(self.attr.id)) \
                    .order_by(desc("export_val")).all()
        if this_yp:
            econ_rank = num_format(all_yp.index(this_yp) + 1, "ordinal") if all_yp.index(this_yp) else ""
            # get PCI ranking
            p2 = _(u"%(product)s the %(economic_rank)s most traded product", product=self.attr.get_name(verb=True), economic_rank=econ_rank)
            pci_rank = this_yp.pci_rank
            if pci_rank:
                pci_ranking_link = u"<a href='/en/rankings/hs92/'>{} (PCI)</a>".format(_(u"Product Complexity Index"))
                pci_rank = num_format(pci_rank, "ordinal") if pci_rank > 1 else ""
                p2 += u" "
                p2 += _(u"and the %(pci_rank)s most complex product according to the %(pci_ranking_link)s", pci_rank=pci_rank, pci_ranking_link=pci_ranking_link)
            p2 += "."
            all_paragraphs.append(p2)

        ''' Paragraph #3
        '''
        yop_exp = self.models.Yop.query.filter_by(year = self.year, product = self.attr).filter(self.models.Yop.export_val!=None).order_by(desc("export_val")).limit(5).all()
        if yop_exp:
            exporters = self.stringify_items(yop_exp, "export_val", "origin")
            yop_imp = self.models.Yop.query.filter_by(year=self.year, product=self.attr).filter(self.models.Yop.import_val!=None).order_by(desc("import_val")).limit(5).all()
            importers = self.stringify_items(yop_imp, "import_val", "origin")
            p3 = _(u"The top exporters of %(product)s are %(exporters)s. The top importers are %(importers)s.",
                    product=self.attr.get_name(), exporters=exporters, importers=importers)
            all_paragraphs.append(p3)

        ''' Paragraph #4
        '''
        p4 = []
        # find out which countries this product is their #1 export/import
        countries_top = self.models.Yo.query.filter_by(year = self.year)
        if len(self.attr.id) == 6:
            countries_top_export = countries_top.filter_by(top_export = self.attr.id) if self.classification == "sitc" else countries_top.filter_by(top_export_hs4 = self.attr.id)
            countries_top_import = countries_top.filter_by(top_import = self.attr.id) if self.classification == "sitc" else countries_top.filter_by(top_import_hs4 = self.attr.id)
        elif len(self.attr.id) == 8:
            countries_top_export = countries_top.filter_by(top_export_hs6 = self.attr.id)
            countries_top_import = countries_top.filter_by(top_import_hs6 = self.attr.id)
        countries_top_export = countries_top_export.order_by(desc('export_val')).limit(10).all()
        countries_top_import = countries_top_import.order_by(desc('import_val')).limit(10).all()
        if countries_top_export:
            countries_top_export = self.stringify_items(countries_top_export, None, "country")
            p4.append(_(u"%(product)s the top export of %(countries)s.", product=self.attr.get_name(verb=True), countries=countries_top_export))
        if countries_top_import:
            countries_top_import = self.stringify_items(countries_top_import, None, "country")
            p4.append(_(u"%(product)s the top import of %(countries)s.", product=self.attr.get_name(verb=True), countries=countries_top_import))
        if p4:
            all_paragraphs = all_paragraphs + p4

        ''' Paragraph #5
        '''
        keywords = self.attr.get_keywords()
        if keywords:
            all_paragraphs.append(_(u"%(product)s also known as %(keywords)s.", product=self.attr.get_name(verb=True), keywords=keywords))

        ''' Paragraph #1
        '''
        p1 = _(u"%(product)s a %(product_id_length)s digit %(classification)s product.", product=self.attr.get_name(verb=True), product_id_length=len(self.attr.get_display_id()), classification=self.classification.upper())
        all_paragraphs.append(p1)

        return all_paragraphs
Exemplo n.º 47
0
    def borrow(self,
               patron,
               pin,
               licensepool,
               delivery_mechanism,
               hold_notification_email=None):
        """Either borrow a book or put it on hold. Don't worry about fulfilling
        the loan yet.
        
        :return: A 3-tuple (`Loan`, `Hold`, `is_new`). Either `Loan`
        or `Hold` must be None, but not both.
        """
        # Short-circuit the request if the patron lacks borrowing
        # privileges.
        PatronUtility.assert_borrowing_privileges(patron)

        now = datetime.datetime.utcnow()
        if licensepool.open_access:
            # We can 'loan' open-access content ourselves just by
            # putting a row in the database.
            now = datetime.datetime.utcnow()
            __transaction = self._db.begin_nested()
            loan, is_new = licensepool.loan_to(patron, start=now, end=None)
            __transaction.commit()
            self._collect_checkout_event(patron, licensepool)
            return loan, None, is_new

        # Okay, it's not an open-access book. This means we need to go
        # to an external service to get the book.
        #
        # This also means that our internal model of whether this book
        # is currently on loan or on hold might be wrong.
        api = self.api_for_license_pool(licensepool)

        must_set_delivery_mechanism = (
            api.SET_DELIVERY_MECHANISM_AT == BaseCirculationAPI.BORROW_STEP)

        if must_set_delivery_mechanism and not delivery_mechanism:
            raise DeliveryMechanismMissing()

        content_link = content_expires = None

        internal_format = api.internal_format(delivery_mechanism)

        if patron.fines:
            max_fines = Configuration.max_outstanding_fines(patron.library)
            if patron.fines >= max_fines.amount:
                raise OutstandingFines()

        # Do we (think we) already have this book out on loan?
        existing_loan = get_one(self._db,
                                Loan,
                                patron=patron,
                                license_pool=licensepool,
                                on_multiple='interchangeable')

        loan_info = None
        hold_info = None
        if existing_loan:
            # Sync with the API to see if the loan still exists.  If
            # it does, we still want to perform a 'checkout' operation
            # on the API, because that's how loans are renewed, but
            # certain error conditions (like NoAvailableCopies) mean
            # something different if you already have a confirmed
            # active loan.

            # TODO: This would be a great place to pass in only the
            # single API that needs to be synced.
            self.sync_bookshelf(patron, pin)
            existing_loan = get_one(self._db,
                                    Loan,
                                    patron=patron,
                                    license_pool=licensepool,
                                    on_multiple='interchangeable')

        new_loan = False
        try:
            loan_info = api.checkout(patron, pin, licensepool, internal_format)

            # We asked the API to create a loan and it gave us a
            # LoanInfo object, rather than raising an exception like
            # AlreadyCheckedOut.
            #
            # For record-keeping purposes we're going to treat this as
            # a newly transacted loan, although it's possible that the
            # API does something unusual like return LoanInfo instead
            # of raising AlreadyCheckedOut.
            new_loan = True
        except AlreadyCheckedOut:
            # This is good, but we didn't get the real loan info.
            # Just fake it.
            identifier = licensepool.identifier
            loan_info = LoanInfo(licensepool.collection,
                                 licensepool.data_source,
                                 identifier.type,
                                 identifier.identifier,
                                 start_date=None,
                                 end_date=now + datetime.timedelta(hours=1))
        except AlreadyOnHold:
            # We're trying to check out a book that we already have on hold.
            hold_info = HoldInfo(licensepool.collection,
                                 licensepool.data_source,
                                 licensepool.identifier.type,
                                 licensepool.identifier.identifier, None, None,
                                 None)
        except NoAvailableCopies:
            if existing_loan:
                # The patron tried to renew a loan but there are
                # people waiting in line for them to return the book,
                # so renewals are not allowed.
                raise CannotRenew(
                    _("You cannot renew a loan if other patrons have the work on hold."
                      ))
            else:
                # That's fine, we'll just (try to) place a hold.
                #
                # Since the patron incorrectly believed there were
                # copies available, update availability information
                # immediately.
                api.update_availability(licensepool)
        except NoLicenses, e:
            # Since the patron incorrectly believed there were
            # licenses available, update availability information
            # immediately.
            api.update_availability(licensepool)
            raise e
Exemplo n.º 48
0
    def intro(self):
        all_paragraphs = []
        ''' Paragraph #1
        '''
        if self.attr.id == "xxwld":
            export_val = num_format(db.session.query(func.sum(self.models.Yo.export_val)).filter_by(year = self.year).first()[0], "export_val")
            all_paragraphs.append(_(u"The total world trade in %(year)s was %(export_val)s.", year=self.year, export_val=export_val))

            exports = self.stringify_items(self.models.Yp.query.filter_by(year = self.year, hs92_id_len=6).order_by(desc("export_val")).limit(10).all(), "export_val", "product")
            all_paragraphs.append(_(u"The 10 most traded products by dollar amount are %(exports_list)s, using the 1992 revision of the HS (Harmonized System) classification.", exports_list=exports))

            origins = self.stringify_items(self.models.Yo.query.filter_by(year = self.year).order_by(desc("export_val")).limit(10).all(), "export_val", "country")
            all_paragraphs.append(_(u"The top 10 exporting countries are %(origins_list)s.", origins_list=origins))
            # raise Exception(all_paragraphs[1])
        else:
            this_yo = self.models.Yo.query.filter_by(year = self.year, country = self.attr).first()
            all_yo = self.models.Yo.query.filter_by(year = self.year).order_by(desc("export_val")).all()
            if this_yo:
                p1 = []
                econ_rank = num_format(all_yo.index(this_yo) + 1, "ordinal") if all_yo.index(this_yo) else ""
                export_val = this_yo.export_val
                import_val = this_yo.import_val
                trade_balance = u"positive" if export_val > import_val else u"negative"
                trade_delta = abs(export_val - import_val)
                this_attr_yo = attrs.Yo.query.filter_by(year = self.year, country = self.attr).first()
                # eci_rank = this_attr_yo.eci_rank
                formatted_vals = {"export_val":export_val, "import_val":import_val, "trade_delta":trade_delta}
                formatted_vals = {k: num_format(v) for k, v in formatted_vals.items()}
                country_is = upperfirst(self.attr.get_name(article=True, verb="is"))
                p1.append(_(u"%(country_is)s the %(econ_rank)s largest export economy in the world",
                            country_is=country_is, econ_rank=econ_rank))
                if this_attr_yo and this_attr_yo.eci_rank:
                    eci_rank = num_format(this_attr_yo.eci_rank, "ordinal") if this_attr_yo.eci_rank > 1 else ""
                    p1 = p1 + [u" ", _("and the %(eci_rank)s most complex economy according to the Economic Complexity Index (ECI).", eci_rank=eci_rank), u" "]
                else:
                    p1.append(". ")
                p1.append(_(u"In %(year)s, %(country)s exported $%(export_val)s and imported $%(import_val)s, resulting in a %(positive_negative)s trade balance of $%(trade_delta)s.",
                            year=self.year, country=self.attr.get_name(article=True), export_val=formatted_vals["export_val"], import_val=formatted_vals["import_val"], positive_negative=trade_balance, trade_delta=formatted_vals["trade_delta"]))
                if this_attr_yo:
                    gdp = this_attr_yo.gdp
                    gdp_pc = this_attr_yo.gdp_pc_current_ppp or this_attr_yo.gdp_pc_current
                    formatted_vals = {"gdp":gdp, "gdp_pc":gdp_pc}
                    formatted_vals = {k: num_format(v) for k, v in formatted_vals.items()}
                    p1.append(u" ")
                    p1.append(_(u"In %(year)s the GDP %(of_country)s was $%(gdp)s and its GDP per capita was $%(gdp_pc)s.",
                                year=self.year, of_country=self.attr.get_name(article="of"), gdp=formatted_vals['gdp'], gdp_pc=formatted_vals['gdp_pc']))
                all_paragraphs.append("".join(p1))

            ''' Paragraph #2
            '''
            yop_exp = self.models.Yop.query.filter_by(year = self.year, origin = self.attr, hs92_id_len=6).order_by(desc("export_val")).limit(5).all()
            if yop_exp:
                exports_list = self.stringify_items(yop_exp, "export_val", "product")
                yop_imp = self.models.Yop.query.filter_by(year = self.year, origin = self.attr, hs92_id_len=6).order_by(desc("import_val")).limit(5).all()
                imports_list = self.stringify_items(yop_imp, "import_val", "product")
                p2 = _(u"The top exports %(of_country)s are %(exports_list)s, using the 1992 revision of the HS (Harmonized System) classification. Its top imports are %(imports_list)s.", of_country=self.attr.get_name(article="of"), exports_list=exports_list, imports_list=imports_list)
                all_paragraphs.append(p2)

            ''' Paragraph #3
            '''
            yod_exp = self.models.Yod.query.filter_by(year = self.year, origin = self.attr).order_by(desc("export_val")).limit(5).all()
            if yod_exp:
                dest_list = self.stringify_items(yod_exp, "export_val", "dest")
                yod_imp = self.models.Yod.query.filter_by(year = self.year, dest = self.attr).order_by(desc("export_val")).limit(5).all()
                origin_list = self.stringify_items(yod_imp, "export_val", "origin")
                p3 = _(u"The top export destinations %(of_country)s are %(destinations)s. The top import origins are %(origins)s.", of_country=self.attr.get_name(article="of"), destinations=dest_list, origins=origin_list)
                all_paragraphs.append(p3)

            ''' Paragraph #4
            '''
            land_borders = self.attr.borders()
            maritime_borders = self.attr.borders(maritime=True)
            if maritime_borders or land_borders:
                if maritime_borders and not land_borders:
                    p4 = _(u"%(country)s is an island and borders %(maritime_borders)s by sea.", country=self.attr.get_name(article=True).title(), maritime_borders=self.stringify_items(maritime_borders))
                if not maritime_borders and land_borders:
                    p4 = _(u"%(country)s borders %(land_borders)s.", country=self.attr.get_name(article=True).title(), land_borders=self.stringify_items(land_borders))
                if maritime_borders and land_borders:
                    p4 = _(u"%(country)s borders %(land_borders)s by land and %(maritime_borders)s by sea.", country=self.attr.get_name(article=True).title(), land_borders=self.stringify_items(land_borders), maritime_borders=self.stringify_items(maritime_borders))
                all_paragraphs.append(p4)

        return all_paragraphs
Exemplo n.º 49
0
 def flash_message(self, obj):
     flasher.info(
         unicode(_('New %(name)s created', name=self.name_singular)))
Exemplo n.º 50
0
    def fulfill(self,
                patron,
                pin,
                licensepool,
                delivery_mechanism,
                sync_on_failure=True):
        """Fulfil a book that a patron has previously checked out.

        :param delivery_mechanism: A LicensePoolDeliveryMechanism
        explaining how the patron wants the book to be delivered. If
        the book has previously been delivered through some other
        mechanism, this parameter is ignored and the previously used
        mechanism takes precedence.

        :return: A FulfillmentInfo object.
        """
        fulfillment = None
        loan = get_one(self._db,
                       Loan,
                       patron=patron,
                       license_pool=licensepool,
                       on_multiple='interchangeable')
        if not loan:
            if sync_on_failure:
                # Sync and try again.
                # TODO: Pass in only the single collection or LicensePool
                # that needs to be synced.
                self.sync_bookshelf(patron, pin)
                return self.fulfill(patron,
                                    pin,
                                    licensepool=licensepool,
                                    delivery_mechanism=delivery_mechanism,
                                    sync_on_failure=False)
            else:
                raise NoActiveLoan(
                    _("Cannot find your active loan for this work."))
        if loan.fulfillment is not None and loan.fulfillment != delivery_mechanism and not delivery_mechanism.delivery_mechanism.is_streaming:
            raise DeliveryMechanismConflict(
                _("You already fulfilled this loan as %(loan_delivery_mechanism)s, you can't also do it as %(requested_delivery_mechanism)s",
                  loan_delivery_mechanism=loan.fulfillment.delivery_mechanism.
                  name,
                  requested_delivery_mechanism=delivery_mechanism.
                  delivery_mechanism.name))

        if licensepool.open_access:
            fulfillment = self.fulfill_open_access(
                licensepool, delivery_mechanism.delivery_mechanism)
        else:
            api = self.api_for_license_pool(licensepool)
            internal_format = api.internal_format(delivery_mechanism)
            fulfillment = api.fulfill(patron, pin, licensepool,
                                      internal_format)
            if not fulfillment or not (fulfillment.content_link
                                       or fulfillment.content):
                raise NoAcceptableFormat()

        # Send out an analytics event to record the fact that
        # a fulfillment was initiated through the circulation
        # manager.
        if self.analytics:
            self.analytics.collect_event(
                patron.library,
                licensepool,
                CirculationEvent.CM_FULFILL,
            )

        # Make sure the delivery mechanism we just used is associated
        # with the loan.
        if loan.fulfillment is None and not delivery_mechanism.delivery_mechanism.is_streaming:
            __transaction = self._db.begin_nested()
            loan.fulfillment = delivery_mechanism
            __transaction.commit()
        return fulfillment
Exemplo n.º 51
0
from nose.tools import set_trace
import requests
import urlparse
from flask.ext.babel import lazy_gettext as _
from problem_detail import ProblemDetail as pd

INTEGRATION_ERROR = pd(
      "http://librarysimplified.org/terms/problem/remote-integration-failed",
      502,
      _("Third-party service failed."),
      _("A third-party service has failed."),
)

class RemoteIntegrationException(Exception):

    """An exception that happens when communicating with a third-party
    service.
    """
    title = _("Failure contacting external service")
    detail = _("The server tried to access %(service)s but the third-party service experienced an error.")
    internal_message = "Error accessing %s: %s"

    def __init__(self, url_or_service, message, debug_message=None):
        """Indicate that a remote integration has failed.
        
        `param url_or_service` The name of the service that failed
           (e.g. "Overdrive"), or the specific URL that had the problem.
        """
        super(RemoteIntegrationException, self).__init__(message)
        if any(url_or_service.startswith(x) for x in ('http:', 'https:')):
            self.url = url_or_service
Exemplo n.º 52
0
# coding: utf-8
from flask.ext.babel import lazy_gettext as _, lazy_pgettext

"Categories the user needs help with"
CATEGORIES = [
    # value, label, inline help text
    (
        "clinneg",
        _(u"Clinical negligence"),
        _(u"Doctors and nurses not treating you with due care during medical treatment"
          ),
    ),
    (
        "commcare",
        _(u"Community care"),
        _(u"You’re unhappy with the care being provided for yourself or a relative due "
          u"to age, disability or special educational needs - for example, in a care "
          u"home or your own home"),
    ),
    ("debt", _(u"Debt"),
     _(u"Bankruptcy, repossession, mortgage debt that is putting your home at risk"
       )),
    ("violence", _(u"Domestic abuse"),
     _(u"Abuse at home, child abuse, harassment by an ex-partner, forced marriage"
       )),
    (
        "discrimination",
        _(u"Discrimination"),
        _(u"Being treated unfairly because of your race, sex, sexual "
          u"orientation"),
    ),
Exemplo n.º 53
0
from flask import render_template, request, redirect, url_for, flash
from flask.ext.login import LoginManager, login_required, login_user, logout_user, current_user
from flask_wtf import Form
from wtforms import StringField, PasswordField, HiddenField
from wtforms.validators import DataRequired
from flask_scrypt import check_password_hash
from flask.ext.babel import gettext as _, lazy_gettext as _l
from functools import wraps

from .app import app
from .models import db, User, NoResultFound

login_manager = LoginManager(app)
login_manager.login_view = "ask_login"
login_manager.login_message = _("Please log in to access this part of the log.")

class NYLogUser:
    """User class for flask-login
    cf https://flask-login.readthedocs.org/en/latest/#your-user-class
    """

    def __init__(self, user):
        self.user = user

    def is_authenticated(self):
        return True

    def is_active(self):
        return True

    def is_anonymous(self):
Exemplo n.º 54
0
 def document_debug_message(self, debug=True):
     if debug:
         return _(unicode(self.detail), service=self.url)
     return None
Exemplo n.º 55
0
                          endpoint='api.locationtype')
location_api.add_resource(api.LocationTypeListResource,
                          '/api/locationtypes/',
                          endpoint='api.locationtypes')
location_api.add_resource(api.LocationItemResource,
                          '/api/location/<location_id>',
                          endpoint='api.location')
location_api.add_resource(api.LocationListResource,
                          '/api/locations/',
                          endpoint='api.locations')


@route(bp, '/locations/', methods=['GET'])
@register_menu(bp,
               'locations_list',
               _('Locations'),
               visible_when=lambda: permissions.edit_locations.can())
@permissions.edit_locations.require(403)
@login_required
def locations_list():
    template_name = 'frontend/location_list.html'
    page_title = _('Locations')

    queryset = services.locations.find()
    queryset_filter = filters.location_filterset()(queryset, request.args)

    args = request.args.copy()
    page = int(args.pop('page', '1'))

    subset = queryset_filter.qs.order_by('location_type')
Exemplo n.º 56
0
 def subtitle(self):
     if not self.groups:
         return _('Level %(level)d', level=self.level)
     else:
         return _('%(roomgroup)s, Level %(level)d', roomgroup=self.groups[0].collection_title, level=self.level)
Exemplo n.º 57
0
def deploy_delete(id):
    servers.delete_server(id)
    flash(_('Server deleted'))
    return redirect(url_for("deploy.deploy_list"))
Exemplo n.º 58
0
def locations_builder():
    template_name = 'frontend/location_builder.html'
    page_title = _('Administrative Divisions')

    if request.method == 'POST' and request.form.get('divisions_graph'):
        nx_graph = nx.DiGraph()
        divisions_graph = json.loads(request.form.get('divisions_graph'))

        nodes = filter(lambda cell: cell.get('type') == 'basic.Rect',
                       divisions_graph.get('cells'))
        links = filter(lambda cell: cell.get('type') == 'link',
                       divisions_graph.get('cells'))

        valid_node_ids = map(
            lambda cell: cell.get('id'),
            filter(lambda cell: helpers.is_objectid(cell.get('id')), nodes))

        # 1. Delete non-referenced location types
        services.location_types.find(id__nin=valid_node_ids).delete()

        # 2. Create location types and update ids
        for i, node in enumerate(nodes):
            try:
                lt = services.location_types.find().get(id=node.get('id'))
                lt.is_administrative = node.get('is_administrative')
                lt.is_political = node.get('is_political')
                lt.name = node.get('label')
                lt.ancestors_ref = []
                lt.save()
            except (models.LocationType.DoesNotExist, ValidationError):
                lt = services.location_types.create(
                    name=node.get('label'),
                    is_administrative=node.get('is_administrative'),
                    is_political=node.get('is_political'))

                # update graph node and link ids
                for j, link in enumerate(links):
                    if link['source'].get('id', None) == node.get('id'):
                        links[j]['source']['id'] = str(lt.id)
                    if link['target'].get('id', None) == node.get('id'):
                        links[j]['target']['id'] = str(lt.id)

            nodes[i]['id'] = str(lt.id)

        # 3. Build graph
        for node in nodes:
            nx_graph.add_node(node.get('id'))
        for link in links:
            if link['source'].get('id') and link['target'].get('id'):
                nx_graph.add_edge(link['source'].get('id'),
                                  link['target'].get('id'))

        # 4. Update ancestor relationships
        for link in links:
            if link['source'].get('id') and link['target'].get('id'):
                ancestors = nx.topological_sort(
                    nx_graph.reverse(),
                    nx_graph.reverse().subgraph(
                        nx.dfs_tree(nx_graph.reverse(),
                                    link['target'].get('id')).nodes()).nodes())
                services.location_types.find().get(
                    id=link['target'].get('id')).update(
                        add_to_set__ancestors_ref=filter(
                            lambda ancestor: ancestor != link['target'].get(
                                'id'), ancestors))

        divisions_graph['cells'] = nodes + links

        g.deployment.administrative_divisions_graph = json.dumps(
            divisions_graph)
        g.deployment.save()
        g.deployment.reload()

        flash(_('Your changes have been saved.'), category='locations_builder')

    return render_template(template_name, page_title=page_title)
Exemplo n.º 59
0
class AddQuestionForm(Form):

    description = TextAreaField(_(u"问题描述"))

    submit = SubmitField(_(u"提交"))
Exemplo n.º 60
0
def deploy_stop_task(provider, id):
    deploy.stop_task(provider, id)
    flash(_('Task stopped'))
    return redirect(url_for("deploy.deploy_list"))