Beispiel #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)
Beispiel #2
0
def __route_farms():
    global PUBLISHED_FARMS
    permissions_dict = datautils.get_permissions_dict()
    for farmname in permissions_dict['farms']:
        farm_content = datautils.get_farm_content(farmname)
        if farm_content.get('publish', ['no'])[0] == 'yes':
            PUBLISHED_FARMS.append(farmname)
            route('/%s' % farmname, 'GET', partial(farmprofile, farmname))
Beispiel #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)
Beispiel #4
0
def resetpassword_post():
    form = cgi.FieldStorage()
    new_password = form.getlist("password")[0]
    salt = datautils.make_salt()
    hash = datautils.get_hash(new_password, salt)
    username = request.get_cookie('username', secret=sessionutils.KEY)
    permissions_dict = datautils.get_permissions_dict()
    permissions_dict['hashed_passwords'][username] = hash
    permissions_dict['password_salts'][username] = salt

    datautils.update_permissions_dict(permissions_dict)
    sessionutils.flash_message("Password for user <b>%s</b> reset" % username)
    redirect('/home')
Beispiel #5
0
def admin():
    # Retrieve data from the submitted form
    form = cgi.FieldStorage()
    form_keys = form.keys()

    # Get current database
    permissions_dict = datautils.get_permissions_dict()

    # Admins
    existing_admins = form.getlist("admin$existing")
    admins_to_remove = [
        x for x in permissions_dict['admins'] if x not in existing_admins
    ]
    for admin_to_remove in admins_to_remove:
        datautils.delete_user(admin_to_remove, 'admins', permissions_dict)
        LOGGER.info("Removed admin [%s]" % admin_to_remove)

    new_admins = [x for x in form.getlist("admin$new") if x != '']
    for new_admin in new_admins:
        datautils.add_user(new_admin, 'admins', permissions_dict)
        LOGGER.info("Added admin [%s]" % new_admin)

    # Editors
    existing_editors = form.getlist("editor$existing")
    editors_to_remove = [
        x for x in permissions_dict['editors'] if x not in existing_editors
    ]
    for editor_to_remove in editors_to_remove:
        datautils.delete_user(editor_to_remove, 'editors', permissions_dict)
        LOGGER.info("Removed editor [%s]" % editor_to_remove)

    new_editors = [x for x in form.getlist("editor$new") if x != '']
    for new_editor in new_editors:
        datautils.add_user(new_editor, 'editors', permissions_dict)
        LOGGER.info("Added editor [%s]" % new_editor)

    # Permissions
    existing_permissions = permissions_dict['permissions'].keys()
    for editor in existing_permissions:
        if editor not in permissions_dict['editors']:
            permissions_dict['permissions'].pop(editor)
    permission_keys = [x for x in form_keys if x.startswith('permission$')]
    for permission_key in permission_keys:
        editor = permission_key.split('$')[1]
        permission = form.getlist(permission_key)[0]
        permissions_dict['permissions'][
            editor] = '' if permission == 'None' else permission

    # Farms
    existing_farms = form.getlist("farm$existing")
    farms_to_remove = [
        x for x in permissions_dict['farms'] if x not in existing_farms
    ]
    for farm_to_remove in farms_to_remove:
        datautils.delete_farm(farm_to_remove, permissions_dict)
        LOGGER.info("Removed farm [%s]" % farm_to_remove)

    new_farms = [x for x in form.getlist("farm$new") if x != '']
    for new_farm in new_farms:
        datautils.add_farm(new_farm, permissions_dict)
        LOGGER.info("Added farm [%s]" % new_farm)

    datautils.update_permissions_dict(permissions_dict)
    redirect('/admin')