Example #1
0
def farms():
    permissions_dict = datautils.get_permissions_dict()
    titles = {}
    coords_strs = {}
    for farmname in permissions_dict['farms']:
        farm_content = datautils.get_farm_content(farmname)
        titles[farmname] = farm_content['title'][0]
        coords_strs[farmname] = farm_content.get('coordinates', [''])[0]
    map_settings = json.load(
        open(os.path.join(DATA_DIR, MAP_SETTINGS_FILE), 'rb'))

    def get_farm_title(farmname):
        return titles.get(farmname, farmname.capitalize())

    def get_farm_coords(farmname):
        gps_coords_str = coords_strs.get(farmname, '')
        gps_coords_str = datautils.validate_gps_string(gps_coords_str,
                                                       map_settings)
        gps_coords = gps_coords_str.split(',')
        if len(gps_coords) == 2:
            map_coords = datautils.gps_to_map(gps_coords[0], gps_coords[1],
                                              map_settings)
            return map_coords
        return ''

    return sessionutils.render_template('farms',
                                        published_farms=PUBLISHED_FARMS,
                                        permissions_dict=permissions_dict,
                                        map_settings=map_settings,
                                        get_farm_title=get_farm_title,
                                        get_farm_coords=get_farm_coords)
Example #2
0
def login_post():
    sessionutils.clear_session()
    form = cgi.FieldStorage()
    username = cgi.escape(form.getfirst('username', ''))
    password = cgi.escape(form.getfirst('password', ''))
    nextpage = cgi.escape(form.getfirst('nextpage', ''))

    ok = datautils.check_password(username, password)
    if ok:
        (role, assigned_farm) = datautils.get_permissions(username)
        now = datetime.datetime.now().strftime("%Y/%m/%d %H:%M")
        sessionutils.flash_message("Signed in user <b>%s</b> on %s" %
                                   (username, now))
        LOGGER.info(
            "Signed in user [%s] with role [%s] and permissions for farm [%s]"
            % (username, role, assigned_farm))

        # Record in session
        sessionutils.setup_session(username, role, assigned_farm)

        # Redirect
        if role == 'admin':
            # E.g. '/admin' or '/edit/dublin'
            # Admins have access to edit any farm profile, so not necessary to check.
            LOGGER.info("User [%s] signed in as admin redirecting to [%s]" %
                        (username, nextpage))
            redirect('/%s' % nextpage)
        else:
            next_parts = nextpage.split('/')
            # Editors are associated with a particular farm, or no farm, os necessary to check if this matches
            # the requested page
            if len(next_parts) == 2 and next_parts[0] == 'edit':
                if next_parts[1] == assigned_farm:
                    LOGGER.info(
                        "User [%s] signed in and access granted to [%s]" %
                        (username, nextpage))
                    redirect('/%s' % nextpage)
                else:
                    sessionutils.flash_message(
                        "Redirected. No access to requested page")
                    LOGGER.info(
                        "User [%s] signed in but requested access to [%s] which doesn't match user's farm [%s] so redirecting"
                        % (username, nextpage, assigned_farm))
                    redirect('/home')
            else:
                LOGGER.info(
                    "User [%s] signed in but didn't request [/edit/<some farm>] so redirecting to home"
                    % username)
                redirect('/home')
    else:
        sessionutils.flash_message(
            "Sign in for user <b>%s</b> failed. Please contact admin to check your permissions."
            % username)
        LOGGER.info("Sign in failed for user [%s]" % username)
        return sessionutils.render_template('login', nextpage='')
Example #3
0
def admin():
    permissions_dict = datautils.get_permissions_dict()
    farms = ['None'] + permissions_dict['farms']

    def get_selected_farm(editor):
        associated = permissions_dict['permissions'].get(editor, '')
        associate = 'None' if associated == '' else associated
        return [('selected' if x == associated else '', x) for x in farms]

    return sessionutils.render_template('admin',
                                        permissions_dict=permissions_dict,
                                        get_selected_farm=get_selected_farm)
Example #4
0
def farmprofile(farm):
    data_layout = json.load(
        open(os.path.join(DATA_DIR, 'farm-data-layout.json'), 'rb'))

    def fixup_url(url):
        if url.startswith('http://') or url.startswith('https://'):
            return url
        return 'http://' + url

    def order_info_keys(keys):
        keys = sorted(keys)
        required_keys = []
        for key in data_layout['required-nested-inputs']:
            if key in keys:
                keys.remove(key)
                required_keys.append(key)
        required_keys = reversed(sorted(required_keys))
        for key in required_keys:
            keys.insert(0, key)
        return keys

    def get_profile_image(content):
        default = content.get('default-image', '')
        images = content.get('images', [])
        return "" if not images else (default if
                                      (default in images) else images[0])

    farm_content = datautils.get_farm_content(farm)
    captions = farm_content.get('captions', {})
    map_settings = json.load(
        open(os.path.join(DATA_DIR, MAP_SETTINGS_FILE), 'rb'))

    return sessionutils.render_template('farmprofile',
                                        farm=farm,
                                        farm_content=farm_content,
                                        captions=captions,
                                        map_settings=map_settings,
                                        fixup_url=fixup_url,
                                        order_info_keys=order_info_keys,
                                        get_profile_image=get_profile_image)
Example #5
0
def editfarm(farm):
    username = request.get_cookie('username', secret=sessionutils.KEY)
    role = request.get_cookie('role', secret=sessionutils.KEY)
    content = datautils.get_farm_content(farm)
    instructions = json.load(
        open(os.path.join(DATA_DIR, 'farm-data-instructions.json'), 'rb'))
    data_layout = json.load(
        open(os.path.join(DATA_DIR, 'farm-data-layout.json'), 'rb'))
    map_settings = json.load(
        open(os.path.join(DATA_DIR, MAP_SETTINGS_FILE), 'rb'))

    def format_instructions(instructions):
        return '<br>'.join("<i class='fa fa-info-circle'></i> %s" % x
                           for x in instructions)

    return sessionutils.render_template(
        'editfarm',
        farm=farm,
        content=content,
        instructions=instructions,
        data_layout=data_layout,
        map_settings=map_settings,
        format_instructions=format_instructions)
Example #6
0
def resources():
    return sessionutils.render_template('resources')
Example #7
0
def contact():
    return sessionutils.render_template('contact')
Example #8
0
def about():
    return sessionutils.render_template('about',
                                        images=datautils.get_published_images(
                                            randomize=True,
                                            max_imgs=MAX_IMAGES_IN_SLIDESHOW))
Example #9
0
def resetpassword():
    return sessionutils.render_template('resetpassword')
Example #10
0
def login_get():
    # A request for e.g. '/edit/dublin' will have been redirected to '/login?nextpage=edit/dublin'
    return sessionutils.render_template('login',
                                        nextpage=request.query.get(
                                            'nextpage', ''))