Crazy Idea: OOP for "Hyperlink"

68 views
Skip to first unread message

guettli

unread,
Apr 23, 2018, 4:59:54 AM4/23/18
to Django users
I have a vague idea to use OOP for a hyperlink.

A hyperlink has these attributes for me:

- href
- verbose name
- Permission: Is the current user allowed to follow the link?
- Preview (on-mouse-over tooltip)

I like Django because it handles the "href" part very smart (via reverse()).

My current use case is the preview tooltip.

The app I develop has roughly ten different report types.

I can't remember the name, but I can remember how the report looked like.

I recall the shape and colors of the report.

That's why I would like to have a on-mouse-over tooltip for the hyperlink.

For example look at these chart types: https://developers.google.com/chart/interactive/docs/gallery

The tooltip should show a small version of the report/chart if I move the mouse over the hyperlink.

I don't want to automate the creation of the preview images.  It is enough if I am able to attach a small HTML snippet to
each Django-URL. This HTML snippet should be used for the preview tooltip.

What do you think?

Regards,
  Thomas

Adrien Cossa

unread,
Apr 23, 2018, 6:22:32 AM4/23/18
to django...@googlegroups.com, guettli

Hi,

On 04/23/2018 10:59 AM, guettli wrote:
I have a vague idea to use OOP for a hyperlink.

A hyperlink has these attributes for me:

- href
- verbose name
- Permission: Is the current user allowed to follow the link?
- Preview (on-mouse-over tooltip)

We have developed something similar in the company I work for. The use case is not exactly the same as yours, but we end up with some "Action" object that are similar to your "Hyperlink".

We have a mechanism based on mixins to define actions on our models, for example let's say "create child node". Now each action has some attributes telling what to display (e.g. "Create new child node") and what should happen when we click on it (e.g. POST to an URL). Now the interesting part is that we can also define some restrictions for every action (e.g. "forbid if user is not part of the manager group, or if a child already exist for the current object, or ... etc") and we have a serializer mixin that would automatically embed our actions information when serializing the model object.

It is then the frontend's job to display whatever you like (description or restriction) when the mouse is over, and to make the link clickable or not. If the user tries to trick us with a manual request, we will not allow the action because the view / model method execution is protected with the same restriction set.

That is to say, after having defined a list of actions in a model field, and a list of restriction for each action, we have a fully working action description and restriction mechanism to manipulate our objects. It looks a bit like this:

class X(ModelWithActionsMixin, Model):
    actions = [ Action(id="create_child", ..., restrictions=[Restriction(...), ...], ]

    @protect(action_id="create")
    def add_child(self):
        ...

or if you want to check the restrictions directly in your view instead of protecting the method:

class NodeCreateView(...):

    def post(self, ...):
        obj = self.get_object()
        try:
            protect_action(obj, "add_child")
        except ProtectedActionError as e:
            raise Error400(...)
        else:
            obj.add_child()


Maybe you can use some similar mechanism for your implementation?

PS: we are willing to make a proper package for our stuff and the idea behind would be to release it as free module, but I can't tell if that will really happen or when... but to see that you need something similar will probably push us :-)


I like Django because it handles the "href" part very smart (via reverse()).

My current use case is the preview tooltip.

The app I develop has roughly ten different report types.

I can't remember the name, but I can remember how the report looked like.

I recall the shape and colors of the report.

That's why I would like to have a on-mouse-over tooltip for the hyperlink.

For example look at these chart types: https://developers.google.com/chart/interactive/docs/gallery

The tooltip should show a small version of the report/chart if I move the mouse over the hyperlink.

I don't want to automate the creation of the preview images.  It is enough if I am able to attach a small HTML snippet to
each Django-URL. This HTML snippet should be used for the preview tooltip.

What do you think?

Regards,
  Thomas
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/c1df4a33-d077-42c4-8fd0-94902b4fad69%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

guettli

unread,
Apr 25, 2018, 4:30:35 AM4/25/18
to Django users
Thank Adrien for sharing your knowledge.

Our use cases have some parts that are common and some
parts that are distinct.

You want actions, I want static attributes.

You define them on the model.

I use URLs. In my case sometimes there is a 1:1 relationship between URL and model,
but sometimes not.

In my use case I have reports. I want a report to have a preview html snippet.

There are N reports (N URLs) for one model.

But I think there are more common things than distinct things.

One URL can have N attributes.

Some are actions, some static ones.

Where these attributes come from is a different topic.

If the URL  represents a model, the attributes (in your case "actions") of the model
can be propagated up to the URL.

I  hope you undestand what I wrote. If not, then tell me.

Up to now these are just basic thoughts. I won't do any coding
during the next days. If someone likes this idea and implements it,
please let me know. I am always curious.

Regards,
  Thomas

Anthony Flury

unread,
Apr 25, 2018, 8:04:22 AM4/25/18
to django...@googlegroups.com
Interestingly, I am thinking on something similar too - having a
report/notifications/actions view that have an auto generated URL. The
idea (on my concept) is that by getting this unique URL via email, a
user can access the report/action without needing to actually login -
the fact that a user accesses the url is authenticaton - it could
optionally require a password, since the userid is effectively part of
the URL

My thoughts :

Either a specific

A Notification/Report Model where one of the fields will be the
unique URL id
    An incoming URL with the right path would search for the
notification model with the extracted URL id
    My views would search my models for the incoming Id, and
confirm expected users, permissions etc.
    I would only have one view that needs this so my specific
solution would be highly specific


Or a  generic solution

A model for URLs and that model can have a one to one or one to one
to many relationship with other models; clearly this relationship is
dynamic so - I see this :

In the Model :

from hrefgeneric.models import HRefModel

class Notification(models.Model)
    href = models.OneToOne(HRefModel, ...)
    ...

In the views

from hrefgeneric.views import HRefRequiredMixin

class NotificationView(HRefRequiredMixin, View):
    class HREFRequired:
        login_url = '/login/'
        redirect_field_name='redirect_to'
        user_name_field = 'username'

         def get(self, HRef):
                # HRef is now the HRef instance relevant to the
incoming request (not just any text extracted from the URL
                      pass
           def post(self, HRef):
                # HRef is now the HRef instance relevant to the
incoming request (not just any text extracted from the URL
                      pass


If login_url is set to anything truthy, then the mixin will enforce
some form of authentication form, with 'user_name_field' set to the
expected user name of the HRef (assuming the expected user on the
HRef is not None).

For the generic solution it would be great if there a decorator for
non class based views which does something similar.

    The HRef instance needs the following fields

            field_name = <field_name> # The name of any URL field that
this HREF should be matched on
            field_value = <value> # The value that the above field
should have for this HREF
            user = <The expected user> # The user that is allowed to
access this HREF - can be None
            group = <permission group> # The permission Group that the
user - can be None
            permission = <Permissions> # One or more permissions that
the user - can be None

    On creation, field_name & field_value must be set

    Example:
          a pattern like this :
               path('/notification/<id>', my_view)

          and a href instance :
                HRef(field_name='id', field_value='aabbccddeeff')

         would match against an incoming path of
                http://host/notification/aabbccddeeff

    It might be that we need to match multiple fields on a url - not
sure how we do this.

I would happily contribute to an Django appropriate plugin etc - it
seems like there is a lot of commonality between the use cases.

--
Anthony Flury
email : *Anthon...@btinternet.com*
Twitter : *@TonyFlury <https://twitter.com/TonyFlury/>*
>> send an email to django-users...@googlegroups.com <javascript:>.
>> To post to this group, send email to django...@googlegroups.com
>> <javascript:>.
>> <https://groups.google.com/group/django-users>.
>> <https://groups.google.com/d/msgid/django-users/c1df4a33-d077-42c4-8fd0-94902b4fad69%40googlegroups.com?utm_medium=email&utm_source=footer>.
>> For more options, visit https://groups.google.com/d/optout
>> <https://groups.google.com/d/optout>.
>
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to django-users...@googlegroups.com
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto:django...@googlegroups.com>.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/082da970-b691-45ae-b546-50a3515bbd76%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/082da970-b691-45ae-b546-50a3515bbd76%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.


--
--
Anthony Flury
email : *Anthon...@btinternet.com*
Twitter : *@TonyFlury <https://twitter.com/TonyFlury/>*

Jani Tiainen

unread,
Apr 25, 2018, 11:29:36 AM4/25/18
to django...@googlegroups.com
Hi.

Most probably you get there by creating custom template tag and bunch of other code.

It would be easier to grasp your idea if you have some kind of an implementation to reference.

Or is there something that stops you from proceeding?


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.

guettli

unread,
Apr 26, 2018, 9:30:46 AM4/26/18
to Django users
I would like to keep both things separated:

- Attributes for URLs
- Access without login


What do you think?

Regards,
  Thomas


guettli

unread,
Apr 26, 2018, 9:33:53 AM4/26/18
to Django users


Am Mittwoch, 25. April 2018 17:29:36 UTC+2 schrieb Jani Tiainen:
Hi.

Most probably you get there by creating custom template tag and bunch of other code.

It would be easier to grasp your idea if you have some kind of an implementation to reference.

Or is there something that stops you from proceeding?


Yes, there is something that stops me: work, family, friends, tennis, sleeping. With other words "time".

Nevertheless, even if I had time, I would do asking, talking and thinking for some days before taking the keyboard :-)

Regards,
  Thomas

Matthew Pava

unread,
Apr 26, 2018, 9:54:31 AM4/26/18
to django...@googlegroups.com

I’ve been thinking about your idea, and I wonder if there could instead be some kind of widget for URL objects (or views).

--

You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

guettli

unread,
Apr 30, 2018, 3:24:32 AM4/30/18
to Django users


Am Donnerstag, 26. April 2018 15:54:31 UTC+2 schrieb Matthew Pava:

I’ve been thinking about your idea, and I wonder if there could instead be some kind of widget for URL objects (or views).

 


A widget ... Let me think about it. A widget is a libray, is source code.
The code needs some data to operate on.
Where should the data come from? Where should I store the data?
What kind of data/input does the widget need?

I have no clue

Matthew Pava

unread,
Apr 30, 2018, 8:39:47 AM4/30/18
to django...@googlegroups.com

Here’s Django’s documentation on widgets:

https://docs.djangoproject.com/en/2.0/ref/forms/widgets/

From the docs:

A widget is Django’s representation of an HTML input element.

 

This whole idea would take some time to implement and consider ramifications.  After all, a hyperlink isn’t really an HTML input element.

 

You have urlpatterns being a list of url objects.  If you could somehow take the attributes of the url object and copy them to a widget (through an __init__ method, for instance), and allow the user to customize those attributes in the URLWidget class.  The really tricky thing about it, though, would be handling permissions.

 

I think it is a DRY way of handling the situation.

 

So the data comes from your urls file, where it is also stored.  The input of the widget would be the url objects from the urls file.

 

 

From: django...@googlegroups.com [mailto:django...@googlegroups.com] On Behalf Of guettli
Sent: Monday, April 30, 2018 2:25 AM
To: Django users
Subject: Re: Crazy Idea: OOP for "Hyperlink"

 

--

You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Anthony Flury

unread,
Apr 30, 2018, 9:55:44 AM4/30/18
to django...@googlegroups.com
What is wrong with a special type of Field - which is a URL, but also
have augmented data items which are stored on the model?

As far as I know - there is no rule that states that each 'Field' on a
model has to have one and only one field in the database. Even if you
have one and only one field in the database, you could capture augmented
information about the URL, and store that as a blob field on the model.

The Custom field would also implement the right methods to turn the
field into html, or even a form.

All possible - the question is how much effort is needed to implement
and test this custom Field.

--
--
Anthony Flury
email : *Anthon...@btinternet.com*
Twitter : *@TonyFlury <https://twitter.com/TonyFlury/>*

> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to django-users...@googlegroups.com
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto:django...@googlegroups.com>.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/fd48e249-9d57-43f5-87e4-09bed383af62%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/fd48e249-9d57-43f5-87e4-09bed383af62%40googlegroups.com?utm_medium=email&utm_source=footer>.

guettli

unread,
May 4, 2018, 7:58:13 AM5/4/18
to Django users
In my use case there is no model involved.

I have a django url-mapping (regex to view) not model.

Regards,
  Thomas Güttler
Reply all
Reply to author
Forward
0 new messages