Exemplo n.º 1
0
 def save(self):
     tag = Tag(wealth=self.wealth,
               name=self.cleaned_data['name'],
               modified_date=datetime.utcnow(),
               created_date=datetime.utcnow())
     tag.save()
     return tag
Exemplo n.º 2
0
def add_tags_to_page(tag_text, page):
    tag_list = Tag.select().where(Tag.id << tag_text)

    tags_to_delete = TagAssociation.delete().where(
        TagAssociation.page == page, ~TagAssociation.tag << (tag_list))

    tags_to_delete.execute()

    tags_in_page = page.tags.select(Tag.id).tuples()

    tags_to_add = tag_list.select().where(~Tag.id << (tags_in_page))

    for n in tags_to_add:
        add_tag = TagAssociation(tag=n, page=page)

        add_tag.save()

    new_tags = json.loads(request.forms.getunicode('new_tags'))

    for n in new_tags:
        new_tag = Tag(tag=n, blog=page.blog)
        new_tag.save()

        add_tag = TagAssociation(tag=new_tag, page=page)

        add_tag.save()

    return tags_to_add, tags_to_delete, new_tags
Exemplo n.º 3
0
def addTag(name, objtype, objid):
    query = Tag.objects.filter(name=name, objtype=objtype, objid=objid)
    if query.count() == 0:
        tag = Tag(name=name, objtype=objtype, objid=objid)
        tag.save()
        return tag
    return None
Exemplo n.º 4
0
 def save(self):
   tag = Tag(
       wealth=self.wealth,
       name=self.cleaned_data['name'],
       modified_date=datetime.utcnow(),
       created_date=datetime.utcnow())
   tag.save()
   return tag
Exemplo n.º 5
0
    def test_get_existing_tag(self):
        """Tests that gets a tag instead of creating a new one"""
        name = 'comida'
        old_tag = Tag(name=name)
        old_tag.save()

        tag = Tag.objects.get_or_create_tag(name=name)

        self.assertEqual(tag.name, name)
        self.assertEqual(tag.id, old_tag.id)
Exemplo n.º 6
0
def add_tags_to_page (tag_text, page, no_delete=False):
    '''
    Takes a list of tags, in JSON text format, and adds them to the page in question.
    Any tags not already in the system will be added.

    :param tag_text:
        List of tags to add.
    :param page:
        Page object to add the tags to.
    :param no_delete:
        When set to True, this will preserve tags already in the page if they are
        not found in tag_text. By default this is False, so any tags not specified in
        tag_text that exist in the page will be removed.
    '''
    tag_list = Tag.select().where(Tag.id << tag_text)

    if no_delete is False:

        tags_to_delete = TagAssociation.delete().where(
            TagAssociation.page == page,
            ~ TagAssociation.tag << (tag_list))

        tags_to_delete.execute()
    else:
        tags_to_delete = None

    tags_in_page = page.tags_all.select(Tag.id).tuples()

    tags_to_add = tag_list.select().where(~Tag.id << (tags_in_page))

    for n in tags_to_add:
        add_tag = TagAssociation(
           tag=n,
           page=page)

        add_tag.save()

    import json
    new_tags = json.loads(request.forms.getunicode('new_tags'))

    for n in new_tags:
        if n != '':
            new_tag = Tag(
                tag=n,
                blog=page.blog)
            new_tag.save()

            add_tag = TagAssociation(
                tag=new_tag,
                page=page)

            add_tag.save()

    return tags_to_add, tags_to_delete, new_tags
Exemplo n.º 7
0
 def node_tag_default(self, o, node, tagname, default):
     tags = Tag.select_by_content_object(node).filter(name=tagname)
     if tags:
         value = tags[0].value
     else:
         value = default
         logger.info("node %s: saving default value %s for tag %s" % (node.name, value, tagname))
         service = self.get_onos_service(o)
         tag = Tag(service=service, content_object=node, name=tagname, value=value)
         tag.save()
     return value
Exemplo n.º 8
0
    def test_retrieve_tags(self):
        Tag.objects.bulk_create([
            Tag(user=self.user, name='Vegan'),
            Tag(user=self.user, name='Dessert'),
        ])

        res = self.client.get(TAGS_URL)

        tags = Tag.objects.all().order_by('-name')
        serializer = TagSerializer(tags, many=True)
        self.assertEqual(res.status_code, status.HTTP_200_OK)
        self.assertEqual(res.data, serializer.data)
Exemplo n.º 9
0
        def save_m2m():
            """Handle the m2m relation between Post and Tags. This function
            will erase and add tags for every actions on a post."""

            instance.tags.clear()
            for tagname in self.cleaned_data['tags'].split(','):
                tagname = tagname.lower().strip()
                if len(tagname) > 0:
                    try:
                        tag = Tag.objects.get(name=tagname)
                    except Tag.DoesNotExist:
                        tag = Tag(name=tagname)
                        tag.save()
                    instance.tags.add(tag)
Exemplo n.º 10
0
 def wan_vm_mac(self):
     tags = Tag.select_by_content_object(self.instance).filter(name="vm_vrouter_tenant")
     if tags:
         tenant = VRouterTenant.objects.get(id=tags[0].value)
         return tenant.public_mac
     else:
         raise Exception("no vm_vrouter_tenant tag for instance %s" % o.instance)
Exemplo n.º 11
0
 def wan_vm_mac(self):
     tags = Tag.select_by_content_object(self.instance).filter(name="vm_vrouter_tenant")
     if tags:
         tenant = VRouterTenant.objects.get(id=tags[0].value)
         return tenant.public_mac
     else:
         raise Exception("no vm_vrouter_tenant tag for instance %s" % o.instance)
Exemplo n.º 12
0
def tag_make_for_page(blog_id=None, page_id=None):

    user = auth.is_logged_in(request)

    if page_id is None:
        # page = Page()
        blog = Blog.load(blog_id)
        page = None
        permission = auth.is_blog_editor(user, blog)
        assoc = {'blog':blog}
    else:
        page = Page.load(page_id)
        blog = None
        permission = auth.is_page_editor(user, page)
        assoc = {'page':page}

    tag_name = request.forms.getunicode('tag')

    if len(tag_name) < 1:
        return None

    # Note that this is a single tag only!

    tag = Tag.add_or_create(
        [tag_name, ],
        **assoc
        )

    if len(tag[0]) > 0:
        tpl = template(tag[0][0].new_tag_for_display)
    else:
        tpl = template(tag[1][0].for_display)

    return tpl
Exemplo n.º 13
0
def tag_make_for_page(blog_id=None, page_id=None):

    user = auth.is_logged_in(request)

    if page_id is None:
        # page = Page()
        blog = Blog.load(blog_id)
        page = None
        permission = auth.is_blog_editor(user, blog)
        assoc = {'blog': blog}
    else:
        page = Page.load(page_id)
        blog = None
        permission = auth.is_page_editor(user, page)
        assoc = {'page': page}

    tag_name = request.forms.getunicode('tag')

    if len(tag_name) < 1:
        return None

    # Note that this is a single tag only!

    tag = Tag.add_or_create([
        tag_name,
    ], **assoc)

    if len(tag[0]) > 0:
        tpl = template(tag[0][0].new_tag_for_display)
    else:
        tpl = template(tag[1][0].for_display)

    return tpl
Exemplo n.º 14
0
def tag_edit(request, tag_id=None):
    msg = False
    if tag_id:
        tag = get_object_or_404(Tag, pk=tag_id)
        submit_url = reverse("core:tag_edit", kwargs={"tag_id": tag.id})
    else:
        tag = Tag()
        submit_url = reverse("core:tag_add")

    if request.method == "POST":
        form = forms.TagForm(request.POST, instance=tag)
        if form.is_valid():
            tag_new = form.save(commit=False)
            tag_new.owner = get_login_user(request.user.id)
            tag_new.save()
            return redirect("core:tag_index")

    form = forms.TagForm(instance=tag)
    return render(
        request,
        "dashboard/tag/edit.html",
        {
            "msg": msg,
            "form": form,
            "tag": tag,
            "submit_url": submit_url
        },
    )
Exemplo n.º 15
0
def tag_edit(request, tag_id=None):
    msg = False
    if tag_id:
        tag = get_object_or_404(Tag, pk=tag_id)
        submit_url = reverse("dashboard:tag_edit",
                             kwargs={'tag_id':tag.id})
    else:
        tag = Tag()
        submit_url = reverse("dashboard:tag_add")

    if request.method == 'POST':
        form = forms.TagForm(request.POST, instance=tag)
        if form.is_valid():
            tag_new = form.save(commit=False)
            tag_new.owner = get_login_user(request.user.id)
            tag_new.save()
            return redirect('dashboard:tag_index')

    form = forms.TagForm(instance=tag)
    return render(request,
                  'dashboard/tag/edit.html',
                  {'msg': msg,
                   'form': form,
                   'tag': tag,
                   'submit_url': submit_url})
Exemplo n.º 16
0
def tag_search_results(request, blog_id=None, site_id=None):

    try:
        search_terms = request.query['search']
    except KeyError:
        raise KeyError('No search field in query.')

    if search_terms == "":
        raise ValueError('Search field is empty.')

    search_terms_enc = utf8_escape(search_terms)

    from core.models import Tag

    # TODO: move to DB.media_search for indexing

    tags_searched = (Tag.select(Tag.id)
        .where(Tag.tag.contains(search_terms_enc))
        .order_by(Tag.tag.asc()).tuples())

    # if site_id is not None:
        # tags_searched.select().where(Tag.blog.site == site_id)
    if blog_id is not None:
        tags_searched.select().where(Tag.blog == blog_id)

    return tags_searched, search_terms
Exemplo n.º 17
0
def tag_make_for_media(media_id=None, tag=None):

    user = auth.is_logged_in(request)
    media = Media.load(media_id)
    permission = auth.is_media_owner(user, media)

    if tag == None:
        tag_name = request.forms.getunicode('tag')
    else:
        tag_name = tag

    if len(tag_name) < 1:
        return None

    # Note that this is a single tag only!

    tag = Tag.add_or_create((tag_name,),
        media=media)

    if len(tag[0]) > 0:
        tpl = template(tag[0][0].new_tag_for_display)
    else:
        tpl = template(tag[1][0].for_display)

    return tpl
Exemplo n.º 18
0
def tag_search_results(request, blog_id=None, site_id=None):

    try:
        search_terms = request.query['search']
    except KeyError:
        raise KeyError('No search field in query.')

    if search_terms == "":
        raise ValueError('Search field is empty.')

    search_terms_enc = utf8_escape(search_terms)

    from core.models import Tag

    # TODO: move to DB.media_search for indexing

    tags_searched = (Tag.select(Tag.id).where(
        Tag.tag.contains(search_terms_enc)).order_by(Tag.tag.asc()).tuples())

    # if site_id is not None:
    # tags_searched.select().where(Tag.blog.site == site_id)
    if blog_id is not None:
        tags_searched.select().where(Tag.blog == blog_id)

    return tags_searched, search_terms
Exemplo n.º 19
0
def add_tags_to_page(tag_text, page, no_delete=False):
    '''
    Takes a list of tags, in JSON text format, and adds them to the page in question.
    Any tags not already in the system will be added.

    :param tag_text:
        List of tags to add.
    :param page:
        Page object to add the tags to.
    :param no_delete:
        When set to True, this will preserve tags already in the page if they are
        not found in tag_text. By default this is False, so any tags not specified in
        tag_text that exist in the page will be removed.
    '''
    tag_list = Tag.select().where(Tag.id << tag_text)

    if no_delete is False:

        tags_to_delete = TagAssociation.delete().where(
            TagAssociation.page == page, ~TagAssociation.tag << (tag_list))

        tags_to_delete.execute()
    else:
        tags_to_delete = None

    tags_in_page = page.tags_all.select(Tag.id).tuples()

    tags_to_add = tag_list.select().where(~Tag.id << (tags_in_page))

    for n in tags_to_add:
        add_tag = TagAssociation(tag=n, page=page)

        add_tag.save()

    import json
    new_tags = json.loads(request.forms.getunicode('new_tags'))

    for n in new_tags:
        if n != '':
            new_tag = Tag(tag=n, blog=page.blog)
            new_tag.save()

            add_tag = TagAssociation(tag=new_tag, page=page)

            add_tag.save()

    return tags_to_add, tags_to_delete, new_tags
Exemplo n.º 20
0
    def test_tag_str(self):
        user = sample_user()
        tag = Tag(
            user=user,
            name='Vegeterian',
        )

        self.assertEqual(str(tag), tag.name)
Exemplo n.º 21
0
    def test_url_is_unique(self):
        # Arrange.
        code = Tag.objects.get(name="Test 1").url

        # Act.
        tag = Tag(name="Test 2", url=code)

        # Assert.
        self.assertRaises(ValidationError, tag.save)
Exemplo n.º 22
0
def delete_orphaned_tags():
    '''
    Cleans up tags that no longer have any page associations.
    '''
    orphaned_tags = Tag.delete().where(
        ~Tag.id << (TagAssociation.select(TagAssociation.tag)))

    orphaned_tags.execute()

    return orphaned_tags
Exemplo n.º 23
0
    def get_extra_attributes(self, o):
        vtr_service = self.get_vtr_service(o)
        vcpe_service = self.get_vcpe_service(o)

        if not vcpe_service:
            raise Exception("No vcpeservice")

        instance = self.get_instance(o)

        if not instance:
            raise Exception("No instance")

        s_tags = []
        c_tags = []
        if o.target and o.target.volt:
            s_tags.append(o.target.volt.s_tag)
            c_tags.append(o.target.volt.c_tag)

        wan_vm_ip=""
        wan_vm_mac=""
        tags = Tag.select_by_content_object(instance).filter(name="vm_wan_addr")
        if tags:
            parts=tags[0].value.split(",")
            if len(parts)!=3:
                raise Exception("vm_wan_addr tag is malformed: %s" % value)
            wan_vm_ip = parts[1]
            wan_vm_mac = parts[2]
        else:
            if CORD_USE_VTN:
                raise Exception("no vm_wan_addr tag for instance %s" % instance)

        fields = {"s_tags": s_tags,
                "c_tags": c_tags,
                "isolation": instance.isolation,
                "wan_container_gateway_mac": vcpe_service.wan_container_gateway_mac,
                "wan_container_gateway_ip": vcpe_service.wan_container_gateway_ip,
                "wan_container_netbits": vcpe_service.wan_container_netbits,
                "wan_vm_mac": wan_vm_mac,
                "wan_vm_ip": wan_vm_ip,
                "container_name": "vcpe-%s-%s" % (s_tags[0], c_tags[0]),
                "dns_servers": [x.strip() for x in vcpe_service.dns_servers.split(",")],

                "result_fn": "%s-vcpe-%s-%s" % (o.test, s_tags[0], c_tags[0]),
                "resultcode_fn": "code-%s-vcpe-%s-%s" % (o.test, s_tags[0], c_tags[0]) }

        # add in the sync_attributes that come from the SubscriberRoot object

        if o.target and hasattr(o.target, "sync_attributes"):
            for attribute_name in o.target.sync_attributes:
                fields[attribute_name] = getattr(o.target, attribute_name)

        for attribute_name in o.sync_attributes:
            fields[attribute_name] = getattr(o,attribute_name)

        return fields
Exemplo n.º 24
0
    def save_instance(self, instance):
        with transaction.atomic():
            instance.volumes = "/etc/dnsmasq.d,/etc/ufw"
            super(VSGTenant, self).save_instance(instance)

            if instance.isolation in ["container", "container_vm"]:
                lan_network = self.get_lan_network(instance)
                port = self.find_or_make_port(instance, lan_network, ip="192.168.0.1", port_id="unmanaged")
                port.set_parameter("c_tag", self.volt.c_tag)
                port.set_parameter("s_tag", self.volt.s_tag)
                port.set_parameter("device", "eth1")
                port.set_parameter("bridge", "br-lan")

                wan_networks = [x for x in instance.slice.networks.all() if "wan" in x.name]
                if not wan_networks:
                    raise XOSProgrammingError("No wan_network")
                port = self.find_or_make_port(instance, wan_networks[0])
                port.set_parameter("next_hop", value="10.0.1.253")   # FIX ME
                port.set_parameter("device", "eth0")

            if instance.isolation in ["vm"]:
                lan_network = self.get_lan_network(instance)
                port = self.find_or_make_port(instance, lan_network)
                port.set_parameter("c_tag", self.volt.c_tag)
                port.set_parameter("s_tag", self.volt.s_tag)
                port.set_parameter("neutron_port_name", "stag-%s" % self.volt.s_tag)
                port.save()

            # tag the instance with the s-tag, so we can easily find the
            # instance later
            if self.volt and self.volt.s_tag:
                tags = Tag.objects.filter(name="s_tag", value=self.volt.s_tag)
                if not tags:
                    tag = Tag(service=self.provider_service, content_object=instance, name="s_tag", value=self.volt.s_tag)
                    tag.save()

            # VTN-CORD needs a WAN address for the VM, so that the VM can
            # be configured.
            if CORD_USE_VTN:
                tags = Tag.select_by_content_object(instance).filter(name="vm_vrouter_tenant")
                if not tags:
                    vrouter = self.get_vrouter_service().get_tenant(address_pool_name="addresses_vsg", subscriber_service = self.provider_service)
                    vrouter.set_attribute("tenant_for_instance_id", instance.id)
                    vrouter.save()
                    tag = Tag(service=self.provider_service, content_object=instance, name="vm_vrouter_tenant", value="%d" % vrouter.id)
                    tag.save()
Exemplo n.º 25
0
def tag_list_pages(blog_id, tag_id):

    user = auth.is_logged_in(request)
    blog = Blog.load(blog_id)
    permission = auth.is_blog_member(user, blog)
    tag = Tag.load(tag_id)

    return listing(request, tag, tag.pages.order_by(Page.publication_date.desc()),
                   'blog', 'blog_pages_for_tag',
                   user=user,
                   search_ui='blog_pages_with_tag',
                   search_context=tag_in_blog_search_results,
                   tags_data={'blog':blog}
                   )
Exemplo n.º 26
0
def delete_orphaned_tags(blog):
    '''
    Cleans up tags that no longer have any page associations.

    :param blog:
        A blog object used as the context for this cleanup.
    '''
    orphaned_tags = Tag.delete().where(
        Tag.blog == blog, ~Tag.id <<
        (TagAssociation.select(TagAssociation.tag)))

    orphaned_tags.execute()

    return orphaned_tags
Exemplo n.º 27
0
def tag_get(blog_id, tag_name):

    user = auth.is_logged_in(request)
    blog = Blog.load(blog_id)
    permission = auth.is_blog_member(user, blog)

    tag_name = url_unescape(tag_name)

    tag_list = Tag.select().where(Tag.tag.contains(tag_name), Tag.blog == blog)

    import json
    tag_list_json = json.dumps([{'tag': t.tag, 'id': t.id} for t in tag_list])

    return tag_list_json
Exemplo n.º 28
0
def delete_orphaned_tags(blog):
    '''
    Cleans up tags that no longer have any page associations.

    :param blog:
        A blog object used as the context for this cleanup.
    '''
    orphaned_tags = Tag.delete().where(
        Tag.blog == blog,
        ~ Tag.id << (TagAssociation.select(TagAssociation.tag)))

    orphaned_tags.execute()

    return orphaned_tags
Exemplo n.º 29
0
 def test_can_create_simple_tag(self):
     
     tag = Tag()
     tag.name = 'shouldBeName'
     tag.slug = 'shouldBeSlug'
     tag.save()
     
     tag_find = Tag.objects.get(name=tag.name)
     self.assertEquals(tag_find.name, tag.name)
     self.assertEquals(tag_find.slug, tag.slug)
     self.assertEquals(tag_find.id, tag.id)
     
     tag.delete()
Exemplo n.º 30
0
def tag_list_pages(blog_id, tag_id):

    user = auth.is_logged_in(request)
    blog = Blog.load(blog_id)
    permission = auth.is_blog_member(user, blog)
    tag = Tag.load(tag_id)

    return listing(request,
                   tag,
                   tag.pages.order_by(Page.publication_date.desc()),
                   'blog',
                   'blog_pages_for_tag',
                   user=user,
                   search_ui='blog_pages_with_tag',
                   search_context=tag_in_blog_search_results,
                   tags_data={'blog': blog})
Exemplo n.º 31
0
def tag_get(blog_id, tag_name):

    user = auth.is_logged_in(request)
    blog = Blog.load(blog_id)
    permission = auth.is_blog_member(user, blog)

    tag_name = url_unescape(tag_name)

    tag_list = Tag.select().where(
        Tag.tag.contains(tag_name),
        Tag.blog == blog)

    import json
    tag_list_json = json.dumps([{'tag':t.tag,
                                'id':t.id} for t in tag_list])

    return tag_list_json
Exemplo n.º 32
0
    def save_instance(self, instance):
        with transaction.atomic():
            instance.volumes = "/etc/dnsmasq.d,/etc/ufw"
            super(VSGTenant, self).save_instance(instance)

            if instance.isolation in ["container", "container_vm"]:
                lan_network = self.get_lan_network(instance)
                port = self.find_or_make_port(instance, lan_network, ip="192.168.0.1", port_id="unmanaged")
                port.set_parameter("c_tag", self.volt.c_tag)
                port.set_parameter("s_tag", self.volt.s_tag)
                port.set_parameter("device", "eth1")
                port.set_parameter("bridge", "br-lan")

                wan_networks = [x for x in instance.slice.networks.all() if "wan" in x.name]
                if not wan_networks:
                    raise XOSProgrammingError("No wan_network")
                port = self.find_or_make_port(instance, wan_networks[0])
                port.set_parameter("next_hop", value="10.0.1.253")   # FIX ME
                port.set_parameter("device", "eth0")

            if instance.isolation in ["vm"]:
                lan_network = self.get_lan_network(instance)
                port = self.find_or_make_port(instance, lan_network)
                port.set_parameter("c_tag", self.volt.c_tag)
                port.set_parameter("s_tag", self.volt.s_tag)
                port.set_parameter("neutron_port_name", "stag-%s" % self.volt.s_tag)
                port.save()

            # tag the instance with the s-tag, so we can easily find the
            # instance later
            if self.volt and self.volt.s_tag:
                tags = Tag.objects.filter(name="s_tag", value=self.volt.s_tag)
                if not tags:
                    tag = Tag(service=self.provider_service, content_object=instance, name="s_tag", value=self.volt.s_tag)
                    tag.save()

            # VTN-CORD needs a WAN address for the VM, so that the VM can
            # be configured.
            if CORD_USE_VTN:
                tags = Tag.select_by_content_object(instance).filter(name="vm_vrouter_tenant")
                if not tags:
                    vrouter = self.get_vrouter_service().get_tenant(address_pool_name="addresses_vsg", subscriber_service = self.provider_service)
                    vrouter.set_attribute("tenant_for_instance_id", instance.id)
                    vrouter.save()
                    tag = Tag(service=self.provider_service, content_object=instance, name="vm_vrouter_tenant", value="%d" % vrouter.id)
                    tag.save()
Exemplo n.º 33
0
def tags_get(blog_id, limit, page_limit):

    user = auth.is_logged_in(request)
    blog = Blog.load(blog_id)
    permission = auth.is_blog_member(user, blog)

    from core.models import TagAssociation

    blog_pages = blog.pages.published.select(Page.id).order_by(Page.publication_date.desc()).limit(page_limit)
    tag_assc = TagAssociation.select(TagAssociation.tag).where(TagAssociation.page << blog_pages)
    tag_list = Tag.select(Tag.tag, Tag.id).where(Tag.id << tag_assc)

    # tag_list = Blog.load(blog_id).tags_all.order_by(Tag.id.desc())

    if limit:
        tag_list = tag_list.limit(limit)

    import json
    tag_list_json = json.dumps([{'tag':t.tag,
                                'id':t.id} for t in tag_list])
    return tag_list_json
Exemplo n.º 34
0
def save_post():
    form = PostForm(request.form)
    if request.method == 'POST' and form.validate():
        blog = Routes.response_sql()
        title = form.title.data
        content = form.content.data
        p = Post(
            title=title,
            content=content,
            blog_id=blog.id_blog,
            create_date=datetime.datetime.now(),
            update_date=datetime.datetime.now(),
            status=1,
        )
        if 'tag' in request.form.keys() and request.form['tag'] != '':
            tag_post = request.form['tag'].split()
            tag_list = list()
            for tag in tag_post:
                tag_query = Tag.query.filter_by(name=tag_post)
                if tag_query.count() == 1:
                    id_tag = tag_query.first().id_tag
                else:
                    tag_model = Tag(
                        name=tag,
                        create_date=datetime.datetime.now(),
                        update_date=datetime.datetime.now(),
                        status=1,
                    )
                    tag_list += [tag_model]
            if len(tag_list) > 0:
                db.session.add_all(tag_list)
                db.session.commit()
                id_tag = db.session.query(Tag).order_by(
                    Tag.id_tag.desc()).first().id_tag

            p.tag_id = id_tag

        db.session.add(p)
        db.session.commit()
    return redirect(url_for('admin.default'))
Exemplo n.º 35
0
def tags_get(blog_id, limit, page_limit):

    user = auth.is_logged_in(request)
    blog = Blog.load(blog_id)
    permission = auth.is_blog_member(user, blog)

    from core.models import TagAssociation

    blog_pages = blog.pages.published.select(Page.id).order_by(
        Page.publication_date.desc()).limit(page_limit)
    tag_assc = TagAssociation.select(
        TagAssociation.tag).where(TagAssociation.page << blog_pages)
    tag_list = Tag.select(Tag.tag, Tag.id).where(Tag.id << tag_assc)

    # tag_list = Blog.load(blog_id).tags_all.order_by(Tag.id.desc())

    if limit:
        tag_list = tag_list.limit(limit)

    import json
    tag_list_json = json.dumps([{'tag': t.tag, 'id': t.id} for t in tag_list])
    return tag_list_json
Exemplo n.º 36
0
    def manage_vcpe(self):
        # Each VOLT object owns exactly one VCPE object

        if self.deleted:
            return

        # Check to see if the wrong s-tag is set. This can only happen if the
        # user changed the s-tag after the VoltTenant object was created.
        if self.vcpe and self.vcpe.instance:
            s_tags = Tag.select_by_content_object(self.vcpe.instance).filter(name="s_tag")
            if s_tags and (s_tags[0].value != str(self.s_tag)):
                self.vcpe.delete()

        if self.vcpe is None:
            from services.vsg.models import VSGService, VSGTenant
            vsgServices = VSGService.get_service_objects().all()
            if not vsgServices:
                raise XOSConfigurationError("No VSG Services available")

            vcpe = VSGTenant(provider_service = vsgServices[0],
                              subscriber_tenant = self)
            vcpe.caller = self.creator
            vcpe.save()
Exemplo n.º 37
0
def tag_make_for_media(media_id=None, tag=None):

    user = auth.is_logged_in(request)
    media = Media.load(media_id)
    permission = auth.is_media_owner(user, media)

    if tag == None:
        tag_name = request.forms.getunicode('tag')
    else:
        tag_name = tag

    if len(tag_name) < 1:
        return None

    # Note that this is a single tag only!

    tag = Tag.add_or_create((tag_name, ), media=media)

    if len(tag[0]) > 0:
        tpl = template(tag[0][0].new_tag_for_display)
    else:
        tpl = template(tag[1][0].for_display)

    return tpl
Exemplo n.º 38
0
    def test_can_add_tags_to_post(self):
        tag1 = Tag(name='my first tag', slug='my-first-tag')
        tag1.save()
        tag2 = Tag(name='my second tag', slug='my-second-tag')
        tag2.save()

        post = Post()
        post.title = "should be title"
        post.subtitle = 'should be subtitle'
        post.created = datetime.now()
        post.body =  "should be body"
        post.save()

        post.tags = [tag1, tag2]

        post_found = Post.objects.get(title=post.title)
        self.assertIn(tag1, post_found.tags.all())
        self.assertIn(tag2, post_found.tags.all())
        self.assertEquals(len(post_found.tags.all()), 2)
Exemplo n.º 39
0
def get_test_tag():
    return Tag(name='comida')
Exemplo n.º 40
0
def tag_delete(blog_id, tag_id):
    user = auth.is_logged_in(request)
    blog = Blog.load(blog_id)
    permission = auth.is_blog_publisher(user, blog)

    auth.check_tag_editing_lock(blog)

    try:
        tag = Tag.get(Tag.id == tag_id,
                      Tag.blog == blog_id)
    except Tag.DoesNotExist:
        raise Tag.DoesNotExist("No such tag #{} in blog {}.".format(
            tag_id,
            blog.for_log))

    from settings import BASE_URL
    tag_page_count = tag.pages.count()

    if request.forms.getunicode('confirm') == user.logout_nonce:

        from core.models import db

        if tag_page_count > 0:
            p_count = tag.pages.published.count()

            from core.cms import queue

            with db.atomic() as txn:
                queue.queue_page_actions(tag.pages.published)
                queue.queue_ssi_actions(blog)
                queue.queue_index_actions(blog, True)

            recommendation = '''
<p><b>{}</b> pages affected by this change have been pushed to the queue.</p>
'''.format(p_count)
        else:
            recommendation = '''
<p>No pages were associated with this tag.</p>
'''
        with db.atomic() as txn:
            tag.delete_instance(recursive=True)

        status = Status(
            type='success',
            close=False,
            message='''
Tag <b>{}</b> was successfully deleted from blog <b>{}</b>.</p>{}
'''.format(tag.for_log, blog.for_display, recommendation)
            )

    else:

        if tag_page_count > 0:
            recommendation = '''
<p><b>There are still <a target="_blank" href="{}/blog/{}/tag/{}/pages">{} pages</a> associated with this tag.</b></p>
'''.format(BASE_URL, blog.id, tag.id, tag_page_count)

            tag_modified = tag_recently_modified(tag)
            if tag_modified:
                recommendation += "<p><b>" + tag_modified + "</b></p>"

        else:
            recommendation = ''

        status = Status(
                type='warning',
                close=False,
                message='''
    You are about to delete tag <b>{}</b> in blog <b>{}</b>.</p>{}
    '''.format(tag.for_listing, blog.for_display, recommendation),
                url='{}/blog/{}/tag/{}/delete'.format(
                    BASE_URL, blog.id, tag.id),
                yes={'id':'delete',
                    'name':'confirm',
                    'label':'Yes, I want to delete this tag',
                    'value':user.logout_nonce},
                no={'label':'No, don\'t delete this tag',
                    'url':'{}/blog/{}/tag/{}'.format(
                    BASE_URL, blog.id, tag.id)}
                )

    tags = template_tags(
        user=user)
    tags.status = status

    return report(tags, 'blog_delete_tag', tag)
Exemplo n.º 41
0
    def get_extra_attributes(self, o):
        # This is a place to include extra attributes that aren't part of the
        # object itself. In the case of vCPE, we need to know:
        #   1) the addresses of dnsdemux, to setup dnsmasq in the vCPE
        #   2) CDN prefixes, so we know what URLs to send to dnsdemux
        #   3) BroadBandShield server addresses, for parental filtering
        #   4) vlan_ids, for setting up networking in the vCPE VM

        vcpe_service = self.get_vcpe_service(o)

        dnsdemux_ip = None
        cdn_prefixes = []

        cdn_config_fn = "/opt/xos/synchronizers/vcpe/cdn_config"
        if os.path.exists(cdn_config_fn):
            # manual CDN configuration
            #   the first line is the address of dnsredir
            #   the remaining lines are domain names, one per line
            lines = file(cdn_config_fn).readlines()
            if len(lines)>=2:
                dnsdemux_ip = lines[0].strip()
                cdn_prefixes = [x.strip() for x in lines[1:] if x.strip()]
        else:
            # automatic CDN configuiration
            #    it learns everything from CDN objects in XOS
            #    not tested on pod.
            if vcpe_service.backend_network_label:
                # Connect to dnsdemux using the network specified by
                #     vcpe_service.backend_network_label
                for service in HpcService.objects.all():
                    for slice in service.slices.all():
                        if "dnsdemux" in slice.name:
                            for instance in slice.instances.all():
                                for ns in instance.ports.all():
                                    if ns.ip and ns.network.labels and (vcpe_service.backend_network_label in ns.network.labels):
                                        dnsdemux_ip = ns.ip
                if not dnsdemux_ip:
                    logger.info("failed to find a dnsdemux on network %s" % vcpe_service.backend_network_label)
            else:
                # Connect to dnsdemux using the instance's public address
                for service in HpcService.objects.all():
                    for slice in service.slices.all():
                        if "dnsdemux" in slice.name:
                            for instance in slice.instances.all():
                                if dnsdemux_ip=="none":
                                    try:
                                        dnsdemux_ip = socket.gethostbyname(instance.node.name)
                                    except:
                                        pass
                if not dnsdemux_ip:
                    logger.info("failed to find a dnsdemux with a public address")

            for prefix in CDNPrefix.objects.all():
                cdn_prefixes.append(prefix.prefix)

        dnsdemux_ip = dnsdemux_ip or "none"

        # Broadbandshield can either be set up internally, using vcpe_service.bbs_slice,
        # or it can be setup externally using vcpe_service.bbs_server.

        bbs_addrs = []
        if vcpe_service.bbs_slice:
            if vcpe_service.backend_network_label:
                for bbs_instance in vcpe_service.bbs_slice.instances.all():
                    for ns in bbs_instance.ports.all():
                        if ns.ip and ns.network.labels and (vcpe_service.backend_network_label in ns.network.labels):
                            bbs_addrs.append(ns.ip)
            else:
                logger.info("unsupported configuration -- bbs_slice is set, but backend_network_label is not")
            if not bbs_addrs:
                logger.info("failed to find any usable addresses on bbs_slice")
        elif vcpe_service.bbs_server:
            bbs_addrs.append(vcpe_service.bbs_server)
        else:
            logger.info("neither bbs_slice nor bbs_server is configured in the vCPE")

        vlan_ids = []
        s_tags = []
        c_tags = []
        if o.volt:
            vlan_ids.append(o.volt.vlan_id)  # XXX remove this
            s_tags.append(o.volt.s_tag)
            c_tags.append(o.volt.c_tag)

        try:
            full_setup = Config().observer_full_setup
        except:
            full_setup = True

        safe_macs=[]
        if vcpe_service.url_filter_kind == "safebrowsing":
            if o.volt and o.volt.subscriber:
                for user in o.volt.subscriber.users:
                    level = user.get("level",None)
                    mac = user.get("mac",None)
                    if level in ["G", "PG"]:
                        if mac:
                            safe_macs.append(mac)

        wan_vm_ip=""
        wan_vm_mac=""
        tags = Tag.select_by_content_object(o.instance).filter(name="vm_wan_addr")
        if tags:
            parts=tags[0].value.split(",")
            if len(parts)!=3:
                raise Exception("vm_wan_addr tag is malformed: %s" % value)
            wan_vm_ip = parts[1]
            wan_vm_mac = parts[2]
        else:
            if CORD_USE_VTN:
                raise Exception("no vm_wan_addr tag for instance %s" % o.instance)

        fields = {"vlan_ids": vlan_ids,   # XXX remove this
                "s_tags": s_tags,
                "c_tags": c_tags,
                "dnsdemux_ip": dnsdemux_ip,
                "cdn_prefixes": cdn_prefixes,
                "bbs_addrs": bbs_addrs,
                "full_setup": full_setup,
                "isolation": o.instance.isolation,
                "wan_container_gateway_mac": vcpe_service.wan_container_gateway_mac,
                "wan_container_gateway_ip": vcpe_service.wan_container_gateway_ip,
                "wan_container_netbits": vcpe_service.wan_container_netbits,
                "wan_vm_mac": wan_vm_mac,
                "wan_vm_ip": wan_vm_ip,
                "safe_browsing_macs": safe_macs,
                "container_name": "vcpe-%s-%s" % (s_tags[0], c_tags[0]),
                "dns_servers": [x.strip() for x in vcpe_service.dns_servers.split(",")],
                "url_filter_kind": vcpe_service.url_filter_kind }

        # add in the sync_attributes that come from the SubscriberRoot object

        if o.volt and o.volt.subscriber and hasattr(o.volt.subscriber, "sync_attributes"):
            for attribute_name in o.volt.subscriber.sync_attributes:
                fields[attribute_name] = getattr(o.volt.subscriber, attribute_name)

        return fields
Exemplo n.º 42
0
    def call(self, **args):
        global glo_saved_vtn_maps

        logger.info("sync'ing vsg tenant to port addresses")

        # build up a dictionary of port-->[wan_addrs] mappings
        port_addrs = {}
        for vsg in VSGTenant.get_tenant_objects().all():
            if not vsg.instance:
                logger.info("skipping vsg %s because it has no instance" % vsg)

            wan_ip = vsg.wan_container_ip
            if not wan_ip:
                logger.info("skipping vsg %s because it has no wan_container_ip" % vsg)

            wan_mac = vsg.wan_container_mac
            if not wan_mac:
                logger.info("skipping vsg %s because it has no wan_container_mac" % vsg)

            lan_network = vsg.get_lan_network(vsg.instance)
            if not lan_network:
                logger.info("skipping vsg %s because it has no lan_network" % vsg)

            lan_port = Port.objects.filter(instance = vsg.instance, network=lan_network)
            if not lan_port:
                logger.info("skipping vsg %s because it has no lan_port" % vsg)
            lan_port = lan_port[0]

            if not lan_port.port_id:
                logger.info("skipping vsg %s because its lan_port has no port_id" % vsg)

            if not (lan_port.pk in port_addrs):
                port_addrs[lan_port.pk] = []
            entry = {"mac_address": wan_mac, "ip_address": wan_ip}
            addr_pairs = port_addrs[lan_port.pk]
            if not entry in addr_pairs:
                 addr_pairs.append(entry)

            # now do the VM_WAN_IP from the instance
            if vsg.instance:
                tags=Tag.select_by_content_object(vsg.instance).filter(name="vm_wan_addr")
                if tags:
                    parts=tags[0].value.split(",")
                    if len(parts)!=3:
                        raise Exception("vm_wan_addr tag is malformed: %s" % value)
                    entry = {"mac_address": parts[2], "ip_address": parts[1]}
                    if not entry in addr_pairs:
                        addr_pairs.append(entry)

        # Get all ports in all controllers
        ports_by_id = {}
        for controller in Controller.objects.all():
            if not controller.admin_tenant:
                logger.info("controller %s has no admin_tenant" % controller)
                continue
            try:
                driver = self.driver.admin_driver(controller = controller)
                ports = driver.shell.quantum.list_ports()["ports"]
            except:
                logger.log_exc("failed to get ports from controller %s" % controller)
                continue

            for port in ports:
                ports_by_id[port["id"]] = port

        for port_pk in port_addrs.keys():
            port = Port.objects.get(pk=port_pk)
            addr_pairs = port_addrs[port_pk]
            neutron_port = ports_by_id.get(port.port_id,None)
            if not neutron_port:
                logger.info("failed to get neutron port for port %s" % port)
                continue

            ips = [x["ip_address"] for x in addr_pairs]

            changed = False

            # delete addresses in neutron that don't exist in XOS
            aaps = neutron_port.get("allowed_address_pairs", [])
            for aap in aaps[:]:
                if not aap["ip_address"] in ips:
                    logger.info("removing address %s from port %s" % (aap["ip_address"], port))
                    aaps.remove(aap)
                    changed = True

            aaps_ips = [x["ip_address"] for x in aaps]

            # add addresses in XOS that don't exist in neutron
            for addr in addr_pairs:
                if not addr["ip_address"] in aaps_ips:
                    logger.info("adding address %s to port %s" % (addr, port))
                    aaps.append( addr )
                    aaps_ips.append(addr["ip_address"])
                    changed = True

            if changed:
                logger.info("updating port %s" % port)
                driver.shell.quantum.update_port(port.port_id, {"port": {"allowed_address_pairs": aaps}})
Exemplo n.º 43
0
 def get_node_tag(self, o, node, tagname):
     tags = Tag.select_by_content_object(node).filter(name=tagname)
     return tags[0].value
Exemplo n.º 44
0
def template_preview_core(template_id):
    '''
    UI for generating a preview of a given template
    '''
    from core.models import Page, FileInfo, page_status
    from core.cms import fileinfo
    from core.cms import invalidate_cache

    invalidate_cache()

    template = Template.load(template_id)

    # TODO: only rebuild mappings if the dirty bit is set

    if template.template_type == template_type.index:

        fi = template.default_mapping.fileinfos
        test_preview_mapping(fi.count(), template)
        fi = fi.get()
        tags = template_tags(blog=template.blog,
                             template=template,
                             fileinfo=fi)

    elif template.template_type == template_type.page:
        try:
            fi = Page.load(int(request.query['use_page'])).fileinfos[0]
        except (KeyError, TypeError):
            fi = template.fileinfos
            test_preview_mapping(fi.count(), template)
            fi = fi.select().join(Page).where(
                FileInfo.page == Page.id,
                Page.blog == template.blog,
                Page.status == page_status.published,
            ).order_by(Page.publication_date.desc()).get()
        tags = template_tags(
            template=template,
            page=fi.page,
        )

    elif template.template_type == template_type.include:
        if template.publishing_mode != publishing_mode.ssi:
            from core.error import PreviewException
            raise PreviewException(
                'You can only preview server-side includes.')
        page = template.blog.pages.published.order_by(
            Page.publication_date.desc()).get()
        fi = page.fileinfos[0]
        tags = template_tags(
            template=template,
            page=page,
        )

    elif template.template_type == template_type.archive:

        if 'use_page' in request.query:
            page_list = [Page.load(int(request.query['use_page']))]
        elif 'use_category' in request.query:
            from core.models import Category
            page_list = Category.load(int(
                request.query['use_category'])).pages.published.limit(1)
        elif 'use_tag' in request.query:
            from core.models import Tag
            page_list = Tag.load(int(
                request.query['use_tag'])).pages.published.limit(1)
        else:
            page_list = template.blog.pages.published.limit(1)

        fi = fileinfo.build_archives_fileinfos_by_mappings(template,
                                                           pages=page_list)
        test_preview_mapping(len(fi), template)
        fi = fi[0]

        archive_pages = fileinfo.generate_archive_context_from_fileinfo(
            fi.xref.archive_xref, template.blog.pages.published, fi)

        tags = template_tags(blog=template.blog,
                             archive=archive_pages,
                             archive_context=fi,
                             fileinfo=fi,
                             template=template)

    elif template.template_type in (template_type.media, template_type.system):
        from core.error import PreviewException
        raise PreviewException(
            'Template {} is of a type that cannot yet be previewed.'.format(
                template.for_log))

    import time
    from core.template import tplt
    tc = time.clock
    start = tc()
    tpl_output = tplt(template, tags)
    end = tc()

    tpl_output = r'<!-- Produced by template {}. Total render time:{} secs -->{}'.format(
        template.for_log, end - start, tpl_output)

    preview_file_path, preview_url = fi.make_preview()

    from core.cms import queue
    queue.write_file(tpl_output, template.blog.path, preview_file_path)

    return ("{}?_={}".format(preview_url, template.modified_date.microsecond))
Exemplo n.º 45
0
def profile_view(request, profile_id):

    profile = Profile.objects.get(pk=profile_id)
    now = datetime.datetime.now()

    if request.POST:
        comment = request.POST.get('comment')
        pro_tag = request.POST.get('pro-tag')
        con_tag = request.POST.get('con-tag')
        file = request.FILES.get('file')
        star_rating = request.POST.get('stars')
        if comment:
            title = request.POST.get('comment-title')
            review = Review(title=title, comment=comment, date=now)
            review.save()
            profile.reviews.add(review)
        elif pro_tag:
            try:
                tag = Tag.objects.get(title=pro_tag, isPro=1)
            except ObjectDoesNotExist:
                tag = Tag(title=pro_tag, isPro=1)
                tag.save()

            user_tag = UserTag(tag=tag)
            user_tag.save()
            profile.pro_tags.add(user_tag)
        elif con_tag:
            try:
                tag = Tag.objects.get(title=pro_tag, isPro=0)
            except ObjectDoesNotExist:
                tag = Tag(title=con_tag, isPro=0)
                tag.save()

            user_tag = UserTag(tag=tag)
            user_tag.save()
            profile.con_tags.add(user_tag)
        elif file:
            photo_form = PhotoForm(request.POST, request.FILES)
            if photo_form.is_valid():
                image = photo_form.save()
                profile.images.add(image)
        elif star_rating:
            rating = StarRating(value=star_rating, date=now)
            rating.save()
            profile.star_ratings.add(rating)

    # profile_id = request.POST.get('profile_id')

    total = 0
    count = 0
    for rating in profile.star_ratings.all():
        count += 1
        total += rating.value

    overall_rating = round(total / count, 0) if count else 0

    history = getHistory(profile)
    photo_form = PhotoForm()

    context = {
        'profile': profile,
        'rating': overall_rating,
        'history': history,
        'photo_form': photo_form
    }

    return render(request, 'core/profile.html', context)
Exemplo n.º 46
0
def tag_edit(blog_id, tag_id):
    user = auth.is_logged_in(request)
    blog = Blog.load(blog_id)
    permission = auth.is_blog_editor(user, blog)

    auth.check_tag_editing_lock(blog)

    try:
        tag = Tag.get(Tag.id == tag_id,
                      Tag.blog == blog_id)
    except Tag.DoesNotExist:
        raise Tag.DoesNotExist("No such tag #{} in blog {}.".format(
            tag_id,
            blog.for_log))

    tags = template_tags(
        user=user)

    from core.utils import html_escape

    if request.method == "POST":

        new_tag_name = request.forms.getunicode('tag_name')
        if new_tag_name != tag.tag:

            try:
                Tag.get(Tag.tag == new_tag_name)

            except Tag.DoesNotExist:
                tag_count = tag.pages.count()

                msg = "Tag changed from {} to <b>{}</b>. {} pages (and their archives) have been queued for republishing.".format(
                    tag.for_log,
                    html_escape(new_tag_name),
                    tag_count)

                tag.tag = new_tag_name
                tag.save()

                if tag_count > 0:

                    from core.cms import queue
                    from core.models import db

                    with db.atomic() as txn:

                        queue.queue_page_actions(tag.pages.published)
                        queue.queue_ssi_actions(blog)
                        queue.queue_index_actions(blog, True)

                tags.status = Status(
                    type='info',
                    message=msg
                    )

            else:

                msg = "Tag not renamed. A tag with the name '{}' already exists.".format(
                    html_escape(new_tag_name)
                )

                tags.status = Status(
                    type='danger',
                    message=msg,
                    no_sure=True)
    else:
        tag_modified = tag_recently_modified(tag)
        if tag_modified:
            tags.status = Status(
                    type='danger',
                    message=tag_modified,
                    no_sure=True)

    tpl = template('edit/tag',
        menu=generate_menu('blog_edit_tag', tag),
        search_context=(search_contexts['sites'], None),
        tag=tag,
        **tags.__dict__)

    return tpl
Exemplo n.º 47
0
 def save_tags(self, tags, post):
     for tag in tags:
         t = Tag(tag=tag)
         t.save()
         t.post.add(post)
Exemplo n.º 48
0
def tag_edit(blog_id, tag_id):
    user = auth.is_logged_in(request)
    blog = Blog.load(blog_id)
    permission = auth.is_blog_editor(user, blog)

    auth.check_tag_editing_lock(blog)

    try:
        tag = Tag.get(Tag.id == tag_id, Tag.blog == blog_id)
    except Tag.DoesNotExist:
        raise Tag.DoesNotExist("No such tag #{} in blog {}.".format(
            tag_id, blog.for_log))

    tags = template_tags(user=user)

    from core.utils import html_escape

    if request.method == "POST":

        new_tag_name = request.forms.getunicode('tag_name')
        if new_tag_name != tag.tag:

            try:
                Tag.get(Tag.tag == new_tag_name)

            except Tag.DoesNotExist:
                tag_count = tag.pages.count()

                msg = "Tag changed from {} to <b>{}</b>. {} pages (and their archives) have been queued for republishing.".format(
                    tag.for_log, html_escape(new_tag_name), tag_count)

                tag.tag = new_tag_name
                tag.save()

                if tag_count > 0:

                    from core.cms import queue
                    from core.models import db

                    with db.atomic() as txn:

                        queue.queue_page_actions(tag.pages.published)
                        queue.queue_ssi_actions(blog)
                        queue.queue_index_actions(blog, True)

                tags.status = Status(type='info', message=msg)

            else:

                msg = "Tag not renamed. A tag with the name '{}' already exists.".format(
                    html_escape(new_tag_name))

                tags.status = Status(type='danger', message=msg, no_sure=True)
    else:
        tag_modified = tag_recently_modified(tag)
        if tag_modified:
            tags.status = Status(type='danger',
                                 message=tag_modified,
                                 no_sure=True)

    tpl = template('edit/tag',
                   menu=generate_menu('blog_edit_tag', tag),
                   search_context=(search_contexts['sites'], None),
                   tag=tag,
                   **tags.__dict__)

    return tpl
Exemplo n.º 49
0
 def save_tags(self, tags, post):
     for tag in tags:
         t = Tag(tag=tag)
         t.save()
         t.post.add(post)
Exemplo n.º 50
0
    def handle(self, *args, **options):
        if not Tag.objects.filter(name=STARRED_TAGNAME).exists():
            t = Tag(name=STARRED_TAGNAME)
            t.save()

        arg_album_name = options['album_name'][0] if options[
            'album_name'] else None

        if arg_album_name is not None:
            album_list = [arg_album_name]
        else:
            album_list = img_utils.get_album_list(settings.PHOTOS_BASEDIR)

        for album_name in album_list:
            _logger.debug("Generating db items for %s: ", album_name)
            try:
                album = Album.objects.get(name=album_name)
            except Album.DoesNotExist:
                album = Album(name=album_name)
                album.save()

            for photo in img_utils.get_photo_list(settings.PHOTOS_BASEDIR,
                                                  album_name):
                photo_relpath = img_utils.get_photo_filesystem_relpath(
                    album_name, photo)
                photo_abspath = img_utils.get_photo_filesystem_path(
                    settings.PHOTOS_BASEDIR, album_name, photo)
                photo_f = None
                try:
                    photo_f = open(photo_abspath, 'rb')
                    hashsum = img_utils.calc_mediafile_hash(photo_f)
                    photo_f.seek(0)
                    try:
                        mf = MediaFile.objects.get(file_hash=hashsum)
                        if mf.file_location != photo_relpath:
                            mf.file_location = photo_relpath
                            mf.save()
                    except MediaFile.DoesNotExist:
                        im = None
                        try:
                            im = Image.open(photo_f)
                            width, height = im.size
                            img_datetime = None
                            if hasattr(im, '_getexif'):
                                # noinspection PyProtectedMember
                                img_exif = im._getexif()
                                if img_exif:
                                    img_datetimedata = img_exif.get(
                                        EXIF_DATETIMEORIGINAL_TAG, None)
                                    try:
                                        img_datetime = img_utils.parse_exif_date(
                                            img_datetimedata
                                        ) if img_datetimedata else None
                                        if img_datetime:
                                            img_datetime = img_datetime.replace(
                                                tzinfo=pytz.UTC)
                                    except ValueError as v:
                                        _logger.warning(
                                            "Could not process date for %s, err: %s",
                                            photo_abspath, str(v))

                            mf = MediaFile(file_hash=hashsum,
                                           mediatype=models.MEDIATYPE_PHOTO,
                                           file_location=photo_relpath,
                                           width=width,
                                           height=height,
                                           date_taken=img_datetime)
                            try:
                                mf.save()
                            except django.db.utils.IntegrityError:
                                # Most probably file was modified, just update old record to preserve
                                # tags and comments
                                mf_old = MediaFile.objects.get(
                                    file_location=photo_relpath)
                                if MediaFile.objects.filter(
                                        file_location="DEL").exists():
                                    # This shouldn't exist but if we don't do this check a
                                    # uncleanly stopped previous job would break the makedb
                                    # command permanently
                                    MediaFile.objects.get(
                                        file_location="DEL").delete()
                                # Do this since file_location needs to be unique
                                mf_old.file_location = str("DEL")
                                mf_old.save()
                                mf.save()
                                mf_old.transfer_relations_to_other(mf)
                                mf_old.delete()

                        except OSError:
                            # this is not an image file, check for video
                            # TODO: work in progress
                            if os.path.splitext(photo_relpath)[-1] == ".MP4":
                                _logger.info("FOUND VIDEO")
                                mf = MediaFile(
                                    file_hash=hashsum,
                                    mediatype=models.MEDIATYPE_VIDEO,
                                    file_location=photo_relpath,
                                    width=0,
                                    height=0)
                                mf.save()
                            else:
                                raise IOError("")
                        finally:
                            if im is not None:
                                im.close()

                    try:
                        album_item = AlbumItem.objects.get(
                            file_location=photo_relpath)
                        album_item.media_file = mf
                        album_item.album = album
                    except AlbumItem.DoesNotExist:
                        album_item = AlbumItem(file_location=photo_relpath,
                                               album=album,
                                               media_file=mf)
                    album_item.save()

                except IOError:
                    _logger.warning("Could not process file: %s",
                                    photo_abspath)
                finally:
                    if photo_f is not None:
                        photo_f.close()
            album.gen_album_dates()
            album.save()

        if arg_album_name is None:
            # Delete albums where underlying folder is missing
            # Nothing should be linked to Album or AlbumItem so we should just be deleting
            # generated data
            for album in Album.objects.all():
                if album.name not in album_list:
                    print("Deleting album %s" % (album.name, ))
                    album.delete()

            all_files = set()
            for album_name in album_list:
                photo_dir = os.path.join(settings.PHOTOS_BASEDIR, album_name)
                all_files.update([
                    os.path.join(album_name, file_name)
                    for file_name in os.listdir(photo_dir)
                ])

            # Delete albums where underlying folder is missing. Note that comments, tags
            # and AlbumItems are linked to mediafiles and will be removed but since the
            # file does not exist anymore it should be ok
            for mf in MediaFile.objects.all():
                if mf.file_location not in all_files:
                    print("Removing file %s from database" %
                          (mf.file_location, ))
                    mf.delete()
Exemplo n.º 51
0
def tag_delete(blog_id, tag_id):
    user = auth.is_logged_in(request)
    blog = Blog.load(blog_id)
    permission = auth.is_blog_publisher(user, blog)

    auth.check_tag_editing_lock(blog)

    try:
        tag = Tag.get(Tag.id == tag_id, Tag.blog == blog_id)
    except Tag.DoesNotExist:
        raise Tag.DoesNotExist("No such tag #{} in blog {}.".format(
            tag_id, blog.for_log))

    from settings import BASE_URL
    tag_page_count = tag.pages.count()

    if request.forms.getunicode('confirm') == user.logout_nonce:

        from core.models import db

        if tag_page_count > 0:
            p_count = tag.pages.published.count()

            from core.cms import queue

            with db.atomic() as txn:
                queue.queue_page_actions(tag.pages.published)
                queue.queue_ssi_actions(blog)
                queue.queue_index_actions(blog, True)

            recommendation = '''
<p><b>{}</b> pages affected by this change have been pushed to the queue.</p>
'''.format(p_count)
        else:
            recommendation = '''
<p>No pages were associated with this tag.</p>
'''
        with db.atomic() as txn:
            tag.delete_instance(recursive=True)

        status = Status(type='success',
                        close=False,
                        message='''
Tag <b>{}</b> was successfully deleted from blog <b>{}</b>.</p>{}
'''.format(tag.for_log, blog.for_display, recommendation))

    else:

        if tag_page_count > 0:
            recommendation = '''
<p><b>There are still <a target="_blank" href="{}/blog/{}/tag/{}/pages">{} pages</a> associated with this tag.</b></p>
'''.format(BASE_URL, blog.id, tag.id, tag_page_count)

            tag_modified = tag_recently_modified(tag)
            if tag_modified:
                recommendation += "<p><b>" + tag_modified + "</b></p>"

        else:
            recommendation = ''

        status = Status(type='warning',
                        close=False,
                        message='''
    You are about to delete tag <b>{}</b> in blog <b>{}</b>.</p>{}
    '''.format(tag.for_listing, blog.for_display, recommendation),
                        url='{}/blog/{}/tag/{}/delete'.format(
                            BASE_URL, blog.id, tag.id),
                        yes={
                            'id': 'delete',
                            'name': 'confirm',
                            'label': 'Yes, I want to delete this tag',
                            'value': user.logout_nonce
                        },
                        no={
                            'label':
                            'No, don\'t delete this tag',
                            'url':
                            '{}/blog/{}/tag/{}'.format(BASE_URL, blog.id,
                                                       tag.id)
                        })

    tags = template_tags(user=user)
    tags.status = status

    return report(tags, 'blog_delete_tag', tag)
def addTag(name, objtype, objid):
    tag = Tag(name=name, objtype=objtype, objid=objid)
    tag.save()
    return tag
Exemplo n.º 53
0
 def get_node_tag(self, o, node, tagname):
     tags = Tag.select_by_content_object(node).filter(name=tagname)
     return tags[0].value
Exemplo n.º 54
0
def blog_import (blog_id):
    user = auth.is_logged_in(request)
    blog = Blog.load(blog_id)
    permission = auth.is_blog_publisher(user, blog)
    reason = auth.check_template_lock(blog, True)

    tags = template_tags(blog=blog,
        user=user)

    import os, settings
    import_path = os.path.join(
        settings.APPLICATION_PATH,
        "data",
        "import.json")

    tags.status = reason

    if request.method == "POST":
        from core.models import db
        tpl = ''
        with db.atomic() as txn:
            import json
            from core.utils import string_to_date

            import_path = request.forms.getunicode('import_path')
            with open(import_path, 'r', encoding='utf8') as f:
                json_data = json.load(f)

            from core.models import page_status, MediaAssociation, Category
            from core.error import PageNotChanged
            from core.libs.peewee import InterfaceError
            from core.cms import media_filetypes
            format_str = "<b>{}</b> / (<i>{}</i>)"

            # TODO: go in chunks of 50 or something?
            # allow graceful disconnection?
            for n in json_data:
                q = []
                n_id = n['id']
                q.append("Checking {}".format(n_id))
                changed = False
                found = False
                match = Page.kv_get('legacy_id', n_id)
                if match.count() > 0:
                    if match[0].object_ref.blog == blog:
                        found = True
                        q.append(match[0].key + "/" + match[0].value + " / Exists: " + format_str.format(n['title'], n_id))
                        existing_entry = Page.load(match[0].objectid)
                        update = existing_entry.kv_get('update').count()
                        # raise Exception(update)
                        q.append('{} / {}'.format(string_to_date(n['modified_date']).replace(tzinfo=None), existing_entry.modified_date
                            ))
                        if string_to_date(n['modified_date']).replace(tzinfo=None) <= existing_entry.modified_date and update == 0:
                            q.append('Existing page {} not changed.'.format(existing_entry.id))
                        else:
                            changed = True
                            q.append('Updating data for existing page {}.'.format(existing_entry.id))
                            existing_entry.title = n['title']
                            existing_entry.text = n['text']
                            existing_entry.basename = n['basename']
                            existing_entry.excerpt = n['excerpt']

                            existing_entry.created_date = string_to_date(n['created_date']).replace(tzinfo=None)
                            existing_entry.modified_date = string_to_date(n['modified_date']).replace(tzinfo=None)
                            existing_entry.publication_date = string_to_date(n['publication_date']).replace(tzinfo=None)

                            try:
                                existing_entry.save(user, False, False, 'New revision from import')
                            except PageNotChanged:
                                pass
                            except InterfaceError:
                                raise Exception("Error saving {}. Check the JSON to make sure it's valid.".format(n_id))

                            for media in existing_entry.media:
                                media.kv_del()

                            existing_entry.clear_categories()
                            existing_entry.clear_kvs()
                            existing_entry.clear_tags()
                            existing_entry.clear_media()

                            entry = existing_entry

                if found is False:
                    q.append("Creating: " + format_str.format(n['title'], n_id))
                    changed = True
                    new_entry = Page(
                        title=n['title'],
                        text=n['text'],
                        basename=n['basename'],
                        excerpt=n['excerpt'],
                        user=user,
                        blog=blog,
                        created_date=string_to_date(n['created_date']),
                        publication_date=string_to_date(n['publication_date']),
                        modified_date=string_to_date(n['modified_date']),
                    )

                    new_entry.modified_date = new_entry.publication_date

                    if n['status'] in ('Publish', 'Published', 'Live'):
                        new_entry.status = page_status.published

                    new_entry.save(user)

                    entry = new_entry

                    q.append("New ID: {}".format(entry.id))

                    # Everything from here on out is

                if changed:

                    # Register a legacy ID for the page

                    entry.kv_set("legacy_id", n["id"])
                    entry.kv_set("legacy_user", n["user_id"])

                    # Category assignments

                    categories = n['categories']
                    if categories == []:
                        saved_page_category = PageCategory.create(
                            page=entry,
                            category=blog.default_category,
                            primary=True).save()
                    else:
                        primary = True
                        for category in categories:
                            cat_exists = False

                            category_id = category['id']
                            existing_category = Category.kv_get('legacy_id', category_id)
                            if existing_category.count() > 0:
                                if existing_category[0].object_ref.blog == blog:
                                    cat_exists = True

                            if cat_exists is False:

                                q.append('Created new category {}/{}'.format(
                                    category_id, category['name']
                                    ))
                                new_category = Category.create(
                                    blog=blog,
                                    title=category['name'],
                                    parent_category=getattr(category, 'parent', None)
                                    )
                                new_category.save()

                                new_category.kv_set('legacy_id',
                                    category_id
                                    )
                            else:
                                new_category = Category.load(existing_category[0].objectid)
                                q.append('Added to existing category {}/{}'.format(
                                    new_category.id, category['name']
                                    ))

                            saved_page_category = PageCategory.create(
                                page=entry,
                                category=new_category,
                                primary=primary
                                ).save()
                            primary = False

                    # Check to make sure a default category exists for the whole blog.
                    # If not, assign one based on the lowest ID.
                    # This can always be reassigned later.

                    # Register tags

                    tags_added, tags_existing, _ = Tag.add_or_create(
                        n['tags'], page=entry)

                    q.append('Tags added: {}'.format(','.join(n.tag for n in tags_added)))
                    q.append('Tags existing: {}'.format(','.join(n.tag for n in tags_existing)))

                    # Register KVs

                    kvs = n['kvs']
                    for key in kvs:
                        if key != "":
                            value = kvs[key]
                            entry.kv_set(key, value)
                            q.append('KV: {}:{}'.format(key, value))

                    # Register media

                    media = n['media']

                    for m in media:

                        if 'path' not in m:
                            continue

                        path = os.path.split(m['path'])

                        try:
                            new_media = Media.get(Media.url == m['url'])
                        except:
                            new_media = Media(
                                filename=path[1],
                                path=m['path'],
                                url=m['url'],
                                type=media_filetypes.image,
                                created_date=string_to_date(m['created_date']),
                                modified_date=string_to_date(m['modified_date']),
                                friendly_name=m['friendly_name'],
                                user=user,
                                blog=blog,
                                site=blog.site
                                )

                        # TODO: RBF
                        try:
                            new_media.save()
                        except Exception:
                            continue

                        media_association = MediaAssociation(
                            media=new_media,
                            page=entry)

                        media_association.save()

                        # Save legacy ID to KV on media

                        if 'id' in m:
                            new_media.kv_set('legacy_id', m['id'])

                        q.append('IMG: {}'.format(new_media.url))

                        # add tags for media

                        q.append('Tags: {}'.format(m['tags']))
                        new_tags = Tag.add_or_create(m['tags'], media=new_media)

                        kvs = m['kvs']
                        for key in kvs:
                            value = kvs[key]
                            new_media.kv_set(key, value)
                            q.append('KV: {}:{}'.format(key, value))

                    fileinfo.build_pages_fileinfos((entry,))
                    fileinfo.build_archives_fileinfos((entry,))

                tpl += ('<p>'.join(q)) + '<hr/>'
        return tpl

        # TODO:

        # Import or create categories as needed
        # Categories in export will need to have parent-child data
        # categories should have legacy identifiers where possible too

        # Import image files, assign those legacy KV identifiers
        # Modify URLs for imported images in posts
        # Make importing of image assets optional

    else:
        tpl = template('ui/ui_blog_import',
            menu=generate_menu('blog_import', blog),
            # search_context=(search_context['blog'], blog),
            import_path=import_path,
            **tags.__dict__)

        return tpl