Example #1
0
    def _add_link_to_db(self, link_type, num_links, src_switch, dst_switch,
                        src_ports, dst_ports):
        # create new link
        link = Link()
        link.topology = self._top
        link.dummy = False
        link.link_type = link_type
        link.num_links = num_links
        link.src_switch = src_switch
        link.dst_switch = dst_switch
        link.src_ports = ports_to_string(src_ports)
        link.dst_ports = ports_to_string(dst_ports)
        link.save()

        # create invidual physical links for matching with CDP data
        if num_links > 1 or link_type in [PORT_CHANNEL, VPC_PEER]:
            # set dummy = True for such links
            for index in range(num_links):
                link = Link()
                link.topology = self._top
                link.dummy = True
                link.link_type = PHYSICAL
                link.num_links = 1
                link.src_switch = src_switch
                link.dst_switch = dst_switch
                link.src_ports = src_ports[index]
                link.dst_ports = dst_ports[index]
                link.save()
Example #2
0
def fake_links():
    twitter = Link(name='Twitter', url='#')
    facebook = Link(name='Facebook', url='#')
    linkedin = Link(name='LinkedIn', url='#')
    google = Link(name='Google+', url='#')
    db.session.add_all([twitter, facebook, linkedin, google])
    db.session.commit()
Example #3
0
def fake_link():
    baidu = Link(name='Twitter', url='#')
    taobao = Link(name='Facebook', url='#')
    QQ = Link(name='LinkedIn', url='#')
    LOL = Link(name='Google+', url='#')
    db.session.add_all([baidu, taobao, QQ, LOL])
    db.session.commit()
Example #4
0
def save_link():
    collection_id = request.args.get("collection_id")
    payload = request.get_json()
    link_name = extract_name_from_url(payload["url"])
    id = uuid.uuid4().hex
    collection = Collection.query.filter(
        Collection.id == collection_id).one_or_none()

    if not collection:
        abort(404)

    new_link = Link(id=id,
                    url=payload["url"],
                    description=payload["description"],
                    name=link_name,
                    collection=collection_id)
    new_link.insert()
    return jsonify({
        "code": 200,
        "data": {
            "name": link_name,
            "id": id,
            "url": payload["url"],
            "description": payload["description"],
            "collection": collection_id
        }
    })
Example #5
0
def copyLink(params):
    logging.info('copylink(): Start')

    #Get  link from GUI
    link = params['link_key'].get()

    #Create new link
    new_link = Link(parent=genLinkParentKey())

    #Copy properties of this link to new link
    new_link.name = link.name
    new_link.description = link.description
    new_link.url = link.url
    new_link.website = link.website
    #new_link.link_category = link.link_category
    new_link.parent_folder = params[
        'target_folder_key']  #parent should be the one provided by the user
    new_link.date_c = datetime.datetime.now()
    new_link.date_m = datetime.datetime.now()

    #save new link
    new_link.put()

    #logging.info('copyLink: '+params['target_folder_key'].get().name)
    #increment n_items of new parent folder
    params['target_folder_key'].get().n_items += 1
    params['target_folder_key'].get().put()
    #logging.info('copyLink: '+ str(params['target_folder_key'].get().n_items))

    status = 'Success: from copyLink'
    return status
    def test_put_link(self, mock_auth):
        link = Link(state=self.state1_code,
                    subcategory_id=self.subcategory.id).save()

        data = {
            'text': 'Section 20 of Statute 39-B',
            'url': 'ny.gov/link/to/statute',
        }

        response = self.client.put('/links/%i' % link.id,
                                   json=data,
                                   headers=auth_headers())
        self.assertEqual(response.status_code, 200)
        mock_auth.assert_called_once()

        # Refresh link object
        link = Link.query.first()
        subcategory = Subcategory.query.first()

        self.assertEqual(link.text, 'Section 20 of Statute 39-B')
        self.assertEqual(link.url, 'ny.gov/link/to/statute')

        json_response = json.loads(response.data.decode('utf-8'))

        self.assertEqual(
            json_response, {
                'id': link.id,
                'subcategory_id': subcategory.id,
                'state': self.state1_code,
                'text': 'Section 20 of Statute 39-B',
                'url': 'ny.gov/link/to/statute',
                'active': True,
                'deactivated_at': None,
            })
Example #7
0
    def test_links(self):
        twitter = Service(name='twitter', url='http://www.twitter.com')
        u, p = self.make_profile('bob')

        link = Link(service=twitter,
                    url='http://www.twitter.com/bob',
                    text='@bob at Twitter',
                    owner=p)
        link.save()

        self.assertEquals(count(get_links_for(p)), 1)

        link2 = Link(url='http://myblog.blogger.com', text='my blog', owner=p)
        link2.save()

        self.assertEquals(count(get_links_for(p)), 2)
Example #8
0
    def xtest_ordered_list(self):

        u, p = self.make_profile('rowena')
        home = Link(url='htp://mysite/com', text='My Site', owner=p)
        flickr = Service(name='Flickr')
        flickr_pics = Link(service=flickr,
                           text='More Photos',
                           url='http://www.flickr.com/ropix',
                           owner=p)
        picassa_pics = Link(text="Photos",
                            url="http://www.picassa.com/ropix",
                            owner=p)

        ll = ListOfLinks([home, flickr_pics, picassa_pics], p)
        self.assertEquals(len(ll), 3)
        self.assertEquals(ll[0], home)
Example #9
0
  def insert_end(node, decision):
    """Insert DecisionEnd between node and node parents"""
    parent_links = node.get_parent_links().exclude(name='default')
    decision_end = decision.get_child_end()

    # Find parent decision node for every end's parent.
    # If the decision node is the one passed,
    # change the parent to link to the Decision node's DecisionEnd node.
    # Skip embedded decisions and forks along the way.
    decision_end_used = False
    for parent_link in parent_links:
      parent = parent_link.parent.get_full_node()
      node_temp = parent
      while node_temp and not isinstance(node_temp, Decision):
        if isinstance(node_temp, Join):
          node_temp = node_temp.get_parent_fork().get_parent()
        elif isinstance(node_temp, DecisionEnd):
          node_temp = node_temp.get_parent_decision().get_parent()
        else:
          node_temp = node_temp.get_parent()

      if node_temp.id == decision.id and parent.node_type != Decision.node_type:
        links = Link.objects.filter(parent=parent).exclude(name__in=['related', 'kill', 'error'])
        if len(links) != 1:
          raise RuntimeError(_('Cannot import workflows that have decision DAG leaf nodes with multiple children or no children.'))
        link = links[0]
        link.child = decision_end
        link.save()

        decision_end_used = True

    # Create link between DecisionEnd and terminal node.
    if decision_end_used and not Link.objects.filter(name='to', parent=decision_end, child=node).exists():
      link = Link(name='to', parent=decision_end, child=node)
      link.save()
def update_or_create_link(data, link=None):
    '''
    Takes a dict of data where the keys are fields of the link model.
    Valid keys are subcategory_id, state, text, url, and active. The 'active'
    key only uses a False value to deactivate the link.

    Once created, a link's subcategory or state cannot be changed.
    '''
    subcategory_id = data.get('subcategory_id')
    state = data.get('state')
    if link is None:
        link = Link(subcategory_id=subcategory_id, state=state)
    elif subcategory_id is not None and subcategory_id != link.subcategory_id:
        raise ValueError(strings.cannot_change_subcategory)
    elif state is not None and state != link.state:
        raise ValueError(strings.cannot_change_state)

    if 'text' in data.keys():
        link.text = data['text']
    if 'url' in data.keys():
        link.url = data['url']

    # You cannot reactivate a link after deactivating it
    if 'active' in data.keys() and not data['active']:
        link.deactivate()

    return link.save()
Example #11
0
def send_url(message):
    if db.session.query(User).get(message.from_user.id):

        url = message.text
        if db.session.query(Link).filter_by(user_id=message.from_user.id, url=url).first():
            bot.send_message(message.from_user.id, "You have already have this URL in your ten last links. Please, type /last to see it.")
            return

        data = {"url": url}
        response = requests.post(url='https://rel.ink/api/links/', json=data)

        if response.status_code == 400:
            bot.send_message(message.from_user.id, "Enter a valid URL.")
            return

        short_link = 'https://rel.ink/' + response.json()["hashid"]
        data = {"url": url, "short_link": short_link, "user_id": message.from_user.id}

        links = db.session.query(Link).filter_by(user_id=message.from_user.id).all()
        if len(links) >= 10:
            delete_link = db.session.query(Link).filter_by(user_id=message.from_user.id).first()
            db.session.delete(delete_link)

        link = Link(**data)

        db.session.add(link)
        db.session.commit()

        bot.send_message(message.from_user.id, f"Short link: {short_link} .")
    else:
        bot.send_message(message.from_user.id, 'Type /start to use this bot')
Example #12
0
def extract_section_to_urls(topics: List[Topic]) -> Dict[str, List[Link]]:
    """Create Dict of topic section to mapping of URL to titles, like:

    {
        "Streamlit Updates": [
            Link(url="https://www.streamlit.io/sharing", title="Streamlit sharing was announced today"),
            Link(url="https://share.streamlit.io/streamlit/demo-uber-nyc-pickups/", title="Check out the new Streamlit Sharing")
        ],
        "Articles": [
            ...
        ],
        ...
    }
    """
    parsed_html_per_topic = [
        BeautifulSoup(topic.post_stream.posts[0].cooked, "html.parser")
        for topic in topics
    ]

    section_to_links_per_topic = [{
        h2_section.text.strip(): [
            Link(url=li.find("a")["href"], title=li.text.strip())
            for li in h2_section.find_next_sibling("ul").find_all("li") if
            li.find("a") is not None and _validate_link(li.find("a")["href"])
        ]
        for h2_section in topic_html.find_all("h2")
    } for topic_html in parsed_html_per_topic]

    section_to_links = reduce(_merge_dict_of_lists, section_to_links_per_topic)

    return section_to_links
    def test_get_links(self):
        link1 = Link(
            subcategory_id=self.subcategory.id,
            state=self.state1_code,
            text='Section 20 of Statute 39-B',
            url='ny.gov/link/to/statute',
        )

        link2 = Link(
            subcategory_id=self.subcategory.id,
            state=self.state2_code,
            text='Statute 20 of Policy ABC',
            url='az.gov/link/to/statute',
        )

        link2.deactivate()

        Link.save_all([link1, link2])

        response = self.client.get('/links')
        self.assertEqual(response.status_code, 200)
        json_response = json.loads(response.data.decode('utf-8'))

        self.assertEqual(len(json_response), 2)
        self.assertEqual(
            json_response[0], {
                'id': link1.id,
                'subcategory_id': link1.subcategory_id,
                'state': self.state1_code,
                'text': 'Section 20 of Statute 39-B',
                'url': 'ny.gov/link/to/statute',
                'active': True,
                'deactivated_at': None,
            })

        link2_expected = {
            'id': link2.id,
            'subcategory_id': link2.subcategory_id,
            'state': 'AZ',
            'text': 'Statute 20 of Policy ABC',
            'url': 'az.gov/link/to/statute',
            'active': False,
        }

        # Assert that the expected results are a subset of the actual results
        self.assertTrue(link2_expected.items() <= json_response[1].items())
        self.assertTrue(isinstance(json_response[1]['deactivated_at'], str))
Example #14
0
    def post(self):
        if users.get_current_user():
            link = Link()
            link.author = users.get_current_user()
            link.content = urllib2.quote(self.request.get('link'))
            link.put()
            memcache.set(link.author.user_id(), link)
            self.response.out.write("Sent %s to the cloud." % self.request.get('link'))
	elif oauth.get_current_user():
            link = Link()
            link.author = oauth.get_current_user()
            link.content = urllib2.quote(self.request.get('link'))
            link.put()
            memcache.set(link.author.user_id(), link)
            self.response.out.write("Sent %s to the cloud." % self.request.get('link'))
        else:
            self.redirect(users.create_login_url("/links/add"))
Example #15
0
def _validate_link(link: str) -> bool:
    """Validate URL using Pydantic's AnyHttpUrl's validator"""
    try:
        Link(url=link)
        return True
    except ValidationError:
        print(f"error parsing URL: {link}")
        return False
Example #16
0
def add_link():
    original_url = request.form['original_url']
    link = Link(original_url=original_url)
    db.session.add(link)
    db.session.commit()

    return render_template('link_added.html', 
        new_link=link.short_url, original_url=link.original_url)
Example #17
0
    def add_topology(self, data, user):
        logger.debug("topology name = %s, model = %s", data[NAME],
                     data[MODEL_NAME])

        self._validate_defaults(data[DEFAULTS])

        self._top = Topology()
        self._top.name = data[NAME]
        self._top.model_name = data[MODEL_NAME]
        self._top.is_fabric = False
        self._top.updated_by = user
        self._top.save()

        # defaults object
        self._top.defaults = dict()
        self._top.defaults[SWITCHES] = list()
        self._top.defaults[LINKS] = list()

        # add topology default switches with dummy=True
        for item in data[DEFAULTS][SWITCHES]:
            if item[MODEL] == 1:
                raise IgniteException(ERR_CAN_NOT_ASSIGN_UNKOWN_MODEL)

            switch = Switch()
            switch.topology = self._top
            switch.dummy = True
            switch.name = DEFAULT
            switch.tier = item[TIER]
            switch.model = get_switch(item[MODEL])
            switch.image_profile = get_profile(item[IMAGE_PROFILE])
            switch.save()

            # add switch to topology defaults
            self._top.defaults[SWITCHES].append(switch)

        # add topology default links with dummy=True
        for item in data[DEFAULTS][LINKS]:
            link = Link()
            link.topology = self._top
            link.dummy = True
            link.src_ports = item[SRC_TIER]  # store tier name in ports
            link.dst_ports = item[DST_TIER]  # store tier name in ports
            link.link_type = item[LINK_TYPE]
            link.num_links = item[NUM_LINKS]
            link.save()

            # need tier names in link object while returning
            link.src_tier = link.src_ports
            link.dst_tier = link.dst_ports

            # add link to topology defaults
            self._top.defaults[LINKS].append(link)

        # new topology has no switches or links
        self._top.switches = list()
        self._top.links = list()

        return self._top
Example #18
0
def wwwinit_add_urls_to_text_set(text_set_id, urls):
    text_set = get(text_set_id)
    for url in urls:
        link = Link()
        link.url = url
        text_set_link = TextSetLink()
        text_set_link.link = link
        text_set.links.append(text_set_link)
    db.session.commit()
Example #19
0
  def decision_helper(decision):
    """
    Iterates through children, waits for ends.
    When an end is found, finish the decision.
    If the end has more parents than the decision has branches, bubble the end upwards.
    """
    # Create decision end if it does not exist.
    if not Link.objects.filter(parent=decision, name='related').exists():
      end = DecisionEnd(workflow=workflow, node_type=DecisionEnd.node_type)
      end.save()
      link = Link(name='related', parent=decision, child=end)
      link.save()

    children = [link.child.get_full_node() for link in decision.get_children_links().exclude(name__in=['error','default'])]

    ends = set()
    for child in children:
      end = helper(child)
      if end:
        ends.add(end)

    # A single end means that we've found a unique end for this decision.
    # Multiple ends mean that we've found a bad decision.
    if len(ends) > 1:
      raise RuntimeError(_('Cannot import workflows that have decisions paths with multiple terminal nodes that converge on a single terminal node.'))
    elif len(ends) == 1:
      end = ends.pop()
      # Branch count will vary with each call if we have multiple decision nodes embedded within decision paths.
      # This is because parents are replaced with DecisionEnd nodes.
      fan_in_count = len(end.get_parent_links().exclude(name__in=['error','default']))
      # IF it covers all branches, then it is an end that perfectly matches this decision.
      # ELSE it is an end for a decision path that the current decision node is a part of as well.
      # The unhandled case is multiple ends for a single decision that converge on a single end.
      # This is not handled in Hue.
      fan_out_count = len(decision.get_children_links().exclude(name__in=['error','default']))
      if fan_in_count > fan_out_count:
        insert_end(end, decision)
        return end
      elif fan_in_count == fan_out_count:
        insert_end(end, decision)
        # End node is a decision node.
        # This means that there are multiple decision nodes in sequence.
        # If both decision nodes are within a single decision path,
        # then the end may need to be returned, if found.
        if isinstance(end, Decision):
          end = decision_helper(end)
          if end:
            return end

        # Can do this because we've replace all its parents with a single DecisionEnd node.
        return helper(end)
      else:
        raise RuntimeError(_('Cannot import workflows that have decisions paths with multiple terminal nodes that converge on a single terminal node.'))
    else:
      raise RuntimeError(_('Cannot import workflows that have decisions paths that never end.'))

    return None
Example #20
0
def index():
    link = request.json
    newlink = Link()
    newlink.name = link["name"]
    newlink.href = link["href"]
    newlink.display = link["display"]
    newlink.description = link["description"]
    newlink.save()
    return jsonify(success=True, message="success")
 def test_init_invalid_state_code(self):
     with self.assertRaises(ValueError) as e:
         Link(
             subcategory_id=self.subcategory.id,
             state='fake-state-code',
             text='Section 20 of Statute 39-B',
             url='ny.gov/link/to/statute',
         )
     self.assertEqual(str(e.exception), invalid_state)
 def test_init_invalid_category(self):
     with self.assertRaises(ValueError) as e:
         Link(
             subcategory_id=0,
             state=self.state_code,
             text='Section 20 of Statute 39-B',
             url='ny.gov/link/to/statute',
         )
     self.assertEqual(str(e.exception), subcategory_not_found)
Example #23
0
def add_link():
    project = request.form['projectid']
    url = request.form['url']
    if url[0:7] != "http://" and url[0:8] != 'https://':
        url = "http://" + url
    link_name = request.form['link_name']
    link = Link(pid=project, url=url, link_name=link_name)
    db.session.add(link)
    db.session.commit()
    return redirect(request.referrer)
Example #24
0
def multiple_addition(links, lifetime):
    for link in links:
        if link.strip():
            row = Link(original_url=''.join(link.split()),
                       lifetime=lifetime,
                       date_expire=(datetime.now() + timedelta(days=lifetime)))
            db.session.add(row)
            db.session.commit()
    return jsonify(code=200,
                   log='Successfully added links')
Example #25
0
def delete_link(doc_id):
    '''Delete a previously created link'''
    result = False
    try:
        mylink = Link.Link(doc_id)
        result = mylink.delete_link()
        return True

    except Exception as e:
        logger.info("error deleting the link" + str(e))
        return False
Example #26
0
def get_link_details(link_id):
    ''' Retrieves the status of a Document link (signed or unsigned)'''
    result = False
    try:
        mylink = Link.Link()
        result = mylink.find_by_link(link_id)
        return result

    except Exception as e:
        logger.info("error deleting the link" + str(e))
        return False
Example #27
0
def new_link():
    form = LinkForm()
    if form.validate_on_submit():
        name = form.name.data
        url = form.url.data
        link = Link(name=name, url=url)
        db.session.add(link)
        db.session.commit()
        flash('Link created.', 'success')
        return redirect(url_for('.manage_link'))
    return render_template('admin/new_link.html', form=form)
Example #28
0
def single_addition(link, lifetime):
    row = Link(original_url=''.join(link.split()),
               lifetime=lifetime,
               date_expire=(datetime.now() + timedelta(days=lifetime)))
    db.session.add(row)
    db.session.commit()
    return jsonify(code=200,
                   log='Successfully added link',
                   original_link=row.original_url,
                   new_link=f'{request.host_url}{row.short_url}',
                   lifetime=f'{row.lifetime} days')
Example #29
0
def index():
    form = UrlForm()
    if request.method == "POST":
        original_url = request.form['original_url']
        link = Link(original_url=original_url, user=current_user)
        db.session.add(link)
        db.session.commit()
        short = link.short_url
        flash(short, "success")
        return redirect(url_for('index'))
    return render_template('index.html', form=form)
 def setUp(self):
     self.state_code = 'NY'
     create_state(code=self.state_code)
     self.category = create_category()
     self.subcategory = create_subcategory(self.category.id)
     self.link = Link(
         subcategory_id=self.subcategory.id,
         state=self.state_code,
         text='Section 20 of Statute 39-B',
         url='ny.gov/link/to/statute',
     ).save()