Пример #1
0
def add_slug(mapper, connection, target):
    if isinstance(target, Organization):
        target.slug = slugify(target.name)
    elif isinstance(target, Repository):
        target.slug = slugify("-".join([target.organization.username,target.name]))
    elif isinstance(target, User):
        target.slug = slugify(target.login)
Пример #2
0
	def post(self):
		#get data from client
		try:
			data = request.get_json()
			post = Post(
				content = data.get('content'),
				title = data.get('title'),
				thumbnail = data.get('thumbnail'),
				slug = slugify(data.get('title')),
				description = data.get('description'),
				user_id = g.user.id,
				status = data.get('status', 1),
				view_count = 0,
				category_id = data.get('category_id')
			)
			db.session.add(post)
			db.session.commit()

			res = {
				"status": "ok",
				"code": 200,
				"message": "The post was successfully added",
				"post": post_schema.dump(post).data
			}
			return make_response(jsonify(res)), 200
		except Exception as e:
			res = {
				"status": "fail",
				"code": 400,
				"message": "failed",
			}
			return make_response(jsonify(res)), 400
Пример #3
0
	def post(self):
		#get data from client
		try:
			data = request.get_json()
			category = Category(
				name = data.get('name'),
				thumbnail = data.get('thumbnail'),
				slug = slugify(data.get('name')),
				description = data.get('description'),
				user_id = g.user.id,
				status = data.get('status', 1),
				parent_id = data.get('parent_id')
			)
			db.session.add(category)
			db.session.commit()

			res = {
				"status": "ok",
				"code": 200,
				"message": "The post was successfully added",
				"category": category_schema.dump(category).data
			}
			return make_response(jsonify(res)), 200
		except Exception as e:
			res = {
				"status": "fail",
				"code": 400,
				"message": "failed",
			}
			return make_response(jsonify(res)), 400
Пример #4
0
    def __init__(self, object):
        # Note to self:
        # likes = client.get('/users/<username>/favorites')
        #       -> type(likes) = <class 'soundcloud.resource.ResourceList'>
        #       -> type(likes[0]) = <class 'soundcloud.resource.Resource'>
        # r = client.get('/resolve', url=<playlist_url>)
        #       -> type(r.tracks) = <type 'list'>
        #       -> type(r.tracks[0]) = <type 'dict'>
        self.uploader = None;
        self.artist = None;
        self.title = None;
        self.original_title = None;
        self.permalink = None;
        self.description = None;
        self.stream_url = None;
        self.download_url = None;
        self.artwork_url = None;
        self.filename = None;

        if type(object) is soundcloud.resource.Resource:
            self.init_from_sc_resource(object)
        elif type(object) is dict:
            self.init_from_dict(object)
        else:
            raise TypeError("Unsupported type: %s." % str(type(object)))

        # just to be safe... shouldn't hurt.
        self.uploader = helpers.convert_to_ascii(self.uploader)
        self.artist = helpers.convert_to_ascii(self.artist);
        self.original_title = helpers.convert_to_ascii(self.original_title)
        self.title = helpers.convert_to_ascii(self.title);
        self.description = helpers.convert_to_ascii(self.description);

        # extract artist/title
        self.__get_artist_and_title__()

        # create track filename
        self.filename = helpers.slugify(self.artist) + "-" + helpers.slugify(self.title)

        # add soundcloud info to description
        sc_info  = "Downloaded from SoundCloud"
        sc_info += "\nUploader: " + self.uploader
        sc_info += "\nTitle: " + self.original_title
        self.description = sc_info + "\n" + "-"*60 + "\n" + self.description
Пример #5
0
    def generate_slug(self):
        """Generate slug from name value

        Returns:
            Boolean|None -- Result from helpers.generate_slug
        """
        name = self.current_parameters.get('name')
        if name:
            return slugify(User, name)
        return None
Пример #6
0
def parseMininingHardware(file):
    hardware = []
    with open(file, newline='') as csvfile:
        csvreader = csv.reader(csvfile, delimiter=',')
        fields = [slugify(f) for f in next(csvreader)]

        for row in csvreader:
            hardware.append(Hardware(fields, row))

    return hardware
def table_records_default_parser(sec):
    records = []
    table = sec.find('*/table')
    if table is not None:
        headers = [slugify(e.text) for e in table.cssselect('.header')[0]]
        # [None, 'Asset', 'Asset Type', 'Owner', 'Value', 'Income Type', 'Income']
        headers[0] = 'id'
        for r in table.cssselect('tr')[1:]:
            records.append({
                headers[i]: cleantext(r[i].text_content())
                for i in range(len(r))
            })
    return records
Пример #8
0
def new_startup(city_slug):
    if 'username' not in login_session:
        flash('You must be logged in to post a startup.', 'error')
        return redirect('login')

    city = session.query(City).filter_by(slug=city_slug).first()
    if not city:
        return render_template('404.html', logged_in=logged_in()), 404

    if request.method == 'POST':
        name = request.form['startup-name']
        description = request.form['startup-description']
        link = request.form['startup-link']
        careers_link = request.form['startup-careers-link']
        errors = check_errors(name, description, link, careers_link)
        if errors:
            for k, error in errors.iteritems():
                flash(error, 'error')
            return render_template('new-startup.html',
                                   city=city,
                                   name=name,
                                   description=description,
                                   link=link,
                                   careers_link=careers_link,
                                   logged_in=logged_in())

        new_startup = Startup(name=name,
                              description=description,
                              link=link,
                              careers_link=careers_link,
                              city_id=city.id,
                              slug=slugify(name),
                              user_id=login_session['user_id'])
        try:
            session.add(new_startup)
            session.commit()
            flash('You have added a new startup to {}.'.format(city.name),
                  'success')
            return redirect(
                url_for('show_startup',
                        city_slug=city_slug,
                        startup_slug=new_startup.slug))
        except sqlalchemy.exc.IntegrityError, exc:
            flash('The startup already exists for the current city.', 'error')
            session.rollback()
            return render_template('new-startup.html',
                                   city=city,
                                   logged_in=logged_in())
Пример #9
0
def cli_new_page(page_name, content='Add your content here'):
    """
    Generates a new page with given name using the defined default types (config.yaml)
    """
    if log.find(page_name):
        print(colored('Page already exists, skipping', 'grey'))
        return

    slugified_page_name = helpers.slugify(page_name)

    file_names = {
        'meta':
        os.path.join(input_dir,
                     slugified_page_name + os.extsep + default_meta_type),
        'page':
        os.path.join(input_dir,
                     slugified_page_name + os.extsep + default_page_type),
    }

    print(colored('Creating page', 'green'),
          colored(slugified_page_name, 'blue'))

    # Meta
    writer_meta = find_writer_for_ext(default_meta_type)()
    boilerplate_meta = {
        'title': page_name,
        'slug': slugified_page_name,
        'template': config['templates']['default_template'],
        'visibility': 'hidden',
        'status': 'draft'
    }

    meta_converted = writer_meta.write(boilerplate_meta)
    # Write meta_converted to actual file
    with io.open(file_names['meta'], 'w') as meta_file:
        meta_file.write(meta_converted)

    # Page
    writer_page = find_writer_for_ext(default_page_type)()
    page_converted = writer_page.write(
        "<h1>{name}</h1><p>{content}</p>".format(name=page_name,
                                                 content=content))
    # Write page_converted to actual file
    with io.open(file_names['page'], 'w') as page_file:
        page_file.write(page_converted)
Пример #10
0
def register_api_model(api, cls):
    model_name = slugify(cls.__name__)

    class SingleModelResource(ModelResource):
        def get_or_abort(self, pk, *args, **kwargs):
            try:
                return cls.objects.get(pk=pk, *args, **kwargs)
            except cls.DoesNotExist:
                abort(404)

        def get(self, model_pk):
            return self.to_dict(self.get_or_abort(model_pk))

        def put(self, model_pk):
            fields = get_request_json()
            model = self.get_or_abort(model_pk)
            try:
                model.update(**fields)
                model.save()
            except OperationError:
                abort(403)
            else:
                return self.to_dict(model)

        def delete(self, model_pk):
            model = self.get_or_abort(model_pk)
            try:
                model.delete()
            except OperationError:
                abort(403)

    class ModelListResource(ModelResource):
        def get(self):
            return self.to_dict(cls.objects)

        def post(self):
            fields = get_request_json()
            try:
                model = cls(**fields)
                model.save()
            except OperationError:
                abort(403)
            else:
                return self.to_dict(model)

    class ModelQueryResource(ModelResource):
        def post(self):
            data = get_request_json()
            if 'query' not in data:
                abort(400)
            query_handler_name = 'handle_%s' % data['query']
            if not hasattr(self, query_handler_name):
                abort(404)
            return getattr(self, query_handler_name)(data)

        def handle_related(self, query):
            if 'on' not in query or 'pk' not in query:
                abort(400)
            try:
                return self.to_dict(cls.objects(**{ query['on']: query['pk'] }))
            except InvalidQueryError:
                abort(403)

    # register api resources
    model_base_url = '/%s' % _inflect_engine.plural(model_name)
    api.add_resource(SingleModelResource, model_base_url + '/<model_pk>', endpoint=model_name)
    api.add_resource(ModelListResource, model_base_url, endpoint='%s_list' % model_name)
    api.add_resource(ModelQueryResource, model_base_url + '/query', endpoint='%s_query' % model_name)
    print ' * [API] registering model %s at %s' % (model_name, model_base_url)
Пример #11
0
	def get_slug(self, requested_slug=None):
		if requested_slug != "":
			self['slug'] = slugify(requested_slug)
		else:
			self['slug'] = slugify(self['title'])
Пример #12
0
 def validate_category(form, field):
     return slugify(field.data)
Пример #13
0
    def post(self):
        '''Handle post requests to create / edit blog posts'''
        new = self.get_argument('new', False)
        title = self.get_argument('title', None)
        text = self.get_argument('text', None)
        slug = self.get_argument('slug', None)
        tags = self.get_argument('tags', None).lower()

                
        if new:
            if not text:
                self.redirect("/blog/new?title="
                    +tornado.escape.url_escape(title or "")
                    +"&tags="+tornado.escape.url_escape(tags or "")
                    +"&text_missing=true")
                return
            elif not title:
                self.redirect("/blog/new?text="
                    +tornado.escape.url_escape(text or "")
                    +"&tags="+tornado.escape.url_escape(tags or "")
                    +"&title_missing=true")
                return
            else:
                slug = slugify( title ) # creates something to be used as 'id' and part of the url for the post


        else:   # edit post
            if not slug or slug=="undefined":
                raise tornado.web.HTTPError(400, "Post field slug missing")
            if not text:
                self.redirect("/blog/edit?title="
                    +tornado.escape.url_escape(title or "")
                    +"&slug="+slug
                    +"&tags="+tornado.escape.url_escape(tags or "")
                    +"&text_missing=true")
                return
            elif not title:
                self.redirect("/blog/new?text="
                    +tornado.escape.url_escape(text or "")
                    +"&slug="+slug
                    +"&tags="+tornado.escape.url_escape(tags or "")
                    +"&title_missing=true")
                return

        update = {}
        pubsubData = {}
        newSlug = slug
        if title:
            update['title'] = title
            if new or self.get_argument('change_slug', False):
                # change_slug is an extra option because you probably don't want to do this
                # by default as it breaks external links
                update['slug'] = slugify( title )
                newSlug = update['slug']
            
            pubsubData['title'] = title 

        if text:
            update['text'] = text
            pubsubData['summary'] = excerpt( text )
        if tags:
            update['tags'] = tags.split(" ")
       
        pubsubData['author'] = { 'name': self.settings["author_name"], 'email': self.settings["author_email"]}
        pubsubData['updated'] = datetime.datetime.utcnow().isoformat() + "Z"
        pubsubData['id'] = pubsubData['link'] = "http://%s/blog/%s"%(self.settings["domain"], newSlug)
        
        publish("blog", pubsubData, self.settings) # publish through xmpp pubsub
      
        db = self.settings["db"]
        if new:
            update['date'] =  datetime.datetime.utcnow()
            db.posts.insert( update,
                callback=lambda response, error: self._on_edit(response, error, slug=slug))
        
        else:
            db.posts.update( {'slug': slug},
                { '$set': update },
                callback=lambda response, error: self._on_edit(response, error, slug=newSlug))
Пример #14
0
def import_churchtrac_people(in_file):
    directory = os.path.dirname(in_file)
    ct_person_file = open(in_file, 'rU')
    ct_people = list(csv.DictReader(ct_person_file, dialect='excel'))
    print "Attempting to import %d people from ChurchTrac" % len(ct_people)
    ct_person_file.close()

    people = []
    households = {}
    not_imported = []
    for ct_person in ct_people:
        try:
            icon_person = {'phone': ''}
            phones = []
            for key in ct_person:
                if 'Age' in key: continue
                new_key = helpers.slugify(key)
                if new_key in transforms.people.mapper:
                    transforms.people.mapper[new_key](new_key, ct_person[key], icon_person)
                elif 'phone' in new_key:
                    transforms.people.phone_mapper(new_key, ct_person[key], ct_person, icon_person, phones)
                else:
                    icon_person[new_key] = ct_person[key]

            # generate a key for the households dict
            hh_key = helpers.slugify("%s %s %s" % (icon_person['last_name'], icon_person['address_1'], 
                                           icon_person['zip']))
            icon_person['household_id'] = hh_key
            # If the current hh_key isn't in the households dict, add it
            if hh_key not in households:
                households[hh_key] = transforms.people.household_from_person(icon_person)
            if phones:
                icon_person['phones'] = phones

            # Run transforms that need to be run after icon_person dict is complete
            transforms.people.relationship_handler(icon_person)
            transforms.people.greeting_handler(icon_person)

            # Add this icon_person
            people.append(icon_person)
        except transforms.people.InvalidPerson:
            not_imported.append(ct_person)

    print "Converted %d households" % len(households)
    print "Converted %d people" % len(people)

    yaml_households = yaml.dump(households, default_flow_style=False)
    icon_household_file = open(os.path.join(directory, 'icon.households.yaml'), 'w')
    icon_household_file.write(yaml_households)
    icon_household_file.close()

    yaml_persons = yaml.dump_all(people, default_flow_style=False)
    icon_person_file = open(os.path.join(directory, 'icon.people.yaml'), 'w')
    icon_person_file.write(yaml_persons)
    icon_person_file.close()
    
    print "%d people were not converted due to invalid format" % len(not_imported)
    yaml_not_importeds = yaml.dump_all(not_imported, default_flow_style=False)
    not_imported_file = open(os.path.join(directory, 'church_trac.people.not_imported.yaml'), 'w')
    not_imported_file.write(yaml_not_importeds)
    not_imported_file.close()

    import_icon_people(in_file)
Пример #15
0
 def url(self):
     if not self.is_saved():
         return url_for('get_system', key_name=slugify(self.name))
     return url_for('get_system', key_name=self.key_name)        
Пример #16
0
def edit_startup(city_slug, startup_slug):
    if 'username' not in login_session:
        flash('You must be logged in to edit a startup.', 'error')
        return redirect('login')

    startup = session.query(Startup).filter_by(slug=startup_slug).first()
    if not startup:
        return render_template('404.html'), 404

    if startup.user_id != login_session['user_id']:
        flash('You cannot edit a startup you did not add.', 'error')
        return redirect('/')

    if request.method == 'POST':
        name = request.form['startup-name']
        description = request.form['startup-description']
        link = request.form['startup-link']
        careers_link = request.form['startup-careers-link']
        errors = check_errors(name, description, link, careers_link)
        if errors:
            for k, error in errors.iteritems():
                flash(error, 'error')
            return render_template('edit-startup.html',
                                   startup=startup,
                                   name=name,
                                   description=description,
                                   link=link,
                                   careers_link=careers_link,
                                   logged_in=logged_in())
        startup.name = name
        startup.description = description
        startup.link = link
        startup.careers_link = careers_link
        startup.slug = slugify(name)
        try:
            session.commit()
            flash('Startup has been modified.', 'success')
            return redirect(
                url_for('show_startup',
                        city_slug=city_slug,
                        startup_slug=startup.slug))
        except:
            flash('The startup already exists for the current city.', 'error')
            session.rollback()
            return render_template('edit-startup.html',
                                   startup=startup,
                                   name=name,
                                   description=description,
                                   link=link,
                                   careers_link=careers_link,
                                   logged_in=logged_in())

    if request.method == 'GET':
        name = startup.name
        description = startup.description
        link = startup.link
        careers_link = startup.careers_link
        return render_template('edit-startup.html',
                               startup=startup,
                               name=name,
                               description=description,
                               link=link,
                               careers_link=careers_link,
                               logged_in=logged_in())
Пример #17
0
 def get_key_name(self):
     return '%s' % slugify(self.name)
Пример #18
0
def import_churchtrac_people(in_file):
    directory = os.path.dirname(in_file)
    ct_person_file = open(in_file, 'rU')
    ct_people = list(csv.DictReader(ct_person_file, dialect='excel'))
    print "Attempting to import %d people from ChurchTrac" % len(ct_people)
    ct_person_file.close()

    people = []
    households = {}
    not_imported = []
    for ct_person in ct_people:
        try:
            icon_person = {'phone': ''}
            phones = []
            for key in ct_person:
                if 'Age' in key: continue
                new_key = helpers.slugify(key)
                if new_key in transforms.people.mapper:
                    transforms.people.mapper[new_key](new_key, ct_person[key],
                                                      icon_person)
                elif 'phone' in new_key:
                    transforms.people.phone_mapper(new_key, ct_person[key],
                                                   ct_person, icon_person,
                                                   phones)
                else:
                    icon_person[new_key] = ct_person[key]

            # generate a key for the households dict
            hh_key = helpers.slugify(
                "%s %s %s" % (icon_person['last_name'],
                              icon_person['address_1'], icon_person['zip']))
            icon_person['household_id'] = hh_key
            # If the current hh_key isn't in the households dict, add it
            if hh_key not in households:
                households[hh_key] = transforms.people.household_from_person(
                    icon_person)
            if phones:
                icon_person['phones'] = phones

            # Run transforms that need to be run after icon_person dict is complete
            transforms.people.relationship_handler(icon_person)
            transforms.people.greeting_handler(icon_person)

            # Add this icon_person
            people.append(icon_person)
        except transforms.people.InvalidPerson:
            not_imported.append(ct_person)

    print "Converted %d households" % len(households)
    print "Converted %d people" % len(people)

    yaml_households = yaml.dump(households, default_flow_style=False)
    icon_household_file = open(os.path.join(directory, 'icon.households.yaml'),
                               'w')
    icon_household_file.write(yaml_households)
    icon_household_file.close()

    yaml_persons = yaml.dump_all(people, default_flow_style=False)
    icon_person_file = open(os.path.join(directory, 'icon.people.yaml'), 'w')
    icon_person_file.write(yaml_persons)
    icon_person_file.close()

    print "%d people were not converted due to invalid format" % len(
        not_imported)
    yaml_not_importeds = yaml.dump_all(not_imported, default_flow_style=False)
    not_imported_file = open(
        os.path.join(directory, 'church_trac.people.not_imported.yaml'), 'w')
    not_imported_file.write(yaml_not_importeds)
    not_imported_file.close()

    import_icon_people(in_file)
Пример #19
0
def main(bundle, ok):
    """
    Console script for i3asap
    """
    start_time = datetime.now()

    # check if it is reasonable to start at all
    if ok != 'yes':
        sys.exit()
    linux = DebianLinux()
    if not linux.verify_os():
        click.echo(
            "Currently this program only supports Kali Linux. Exiting..")
        sys.exit()

    # Setup properties
    bundle = slugify(bundle)
    repository = "https://raw.githubusercontent.com/SteveTabernacle/i3asap/master/bundles/" + bundle + "/"

    # Init logging
    logging.basicConfig(filename='i3asap.log', level=logging.DEBUG)
    logging.debug("Started at " + str(start_time))

    manifest = fetchJSON(repository + "manifest.json")

    # Download all specified files, e.g. dotfiles and wallpaper

    logging.debug("Downloading " + str(manifest["wallpaper"]))
    wallpaper = AsynkDownloader(
        [{
            "name": manifest["wallpaper"],
            "saveAs": "/usr/share/backgrounds/wallpaper.jpg"
        }], repository + "/bundle/")
    wallpaper.start()

    logging.debug("Downloading " + str(manifest["dotfiles"]))
    dotfiles = AsynkDownloader(manifest["dotfiles"], repository + "/bundle/")
    dotfiles.start()

    click.echo("* Uninstalling " + manifest["uninstall"])
    # Purge specified programs
    if "purge" in manifest and len(manifest["uninstall"]) > 1:
        logging.debug(linux.uninstall(manifest["uninstall"]))

    click.echo("* Installing " + manifest["install"])
    # Install specified programs
    if "install" in manifest and len(manifest["install"]) > 1:
        logging.debug(linux.install(manifest["install"]))

    # Install i3
    click.echo("* Installing " + linux.i3_base_packages())
    logging.debug(linux.install(linux.i3_base_packages()))

    # todo Create new user
    # Wait until downloads are complete
    click.echo("* Waiting for dotfiles to finish downloading..")
    dotfiles.join()
    click.echo("* Dotfiles finished downloading!")

    click.echo("* Waiting for wallpaper to finish downloading..")
    wallpaper.join()
    click.echo("* Wallpaper finished downloading!")

    click.echo("* Done! Time elapsed: " + str(datetime.now() - start_time))

    try:
        input("Press Enter to switch to i3 ..")
    except SyntaxError:
        pass
    linux.switch_wm()
Пример #20
0
 def url(self):
     return url_for('get_topic', system_key_name=self.system, category=slugify(self.category),name=slugify(self.name))
Пример #21
0
 def get_key_name(self):
     return '%s/%s/%s' % (self.system, slugify(self.category), slugify(self.name))