Re: [pgAdmin4][RM#3037] Allow user to disable Gravatar image

Поиск
Список
Период
Сортировка
От Murtuza Zabuawala
Тема Re: [pgAdmin4][RM#3037] Allow user to disable Gravatar image
Дата
Msg-id CAKKotZThnWbzYiWOOJkr6oH5FJnHF4bkJhbF_eP7-S8WDpEgmw@mail.gmail.com
обсуждение исходный текст
Ответ на Re: [pgAdmin4][RM#3037] Allow user to disable Gravatar image  (Joao De Almeida Pereira <jdealmeidapereira@pivotal.io>)
Список pgadmin-hackers
Hi Joao,

On Tue, Mar 6, 2018 at 10:56 PM, Joao De Almeida Pereira <jdealmeidapereira@pivotal.io> wrote:
Hello,

I just sent a video that talks exactly about this, in the journey to have a more maintainable code and easier to navigate, we sometimes increase complexity for a period of time in order to get to the state that we want to be.

In particular in this example I tried the following:
1. Demonstrate that we do not need templates to get things done
That raised questions in mind, 
​Why?
I
​f templates are bad then why every other framework provides them?
I've seen many projects using templates to serve HTML code, In pgAdmin4 also we are using templates just as starting point to load UI whether its main page or query tool/debugger page.

2. How to get the information we need to the place where it is needed, like moving the information of the server mode to the front end where decisions should be made

​I agree and that's what we are doing in pgAdmin4.
 

Sometimes just because we can do a simple change, doesn't mean we should do it because it might bit us in the future.


The PoC looks like a huge amount of code that is not even related to Gravatar, when we could just change a template file or 2, I hear that. But what I am talking about is a change of paradigm and Architecture where the Frontend is responsible for displaying the application and the Backend is only responsible for giving information that the Frontend Requires. Instead of the current mixed architecture where some things are done in the Frontend some things are done in the Backend a more predictable architecture would benefit the application and specially the developers.
 

That is why I always insist in refactor code as much as possible to ensure that the code is readable and helps people follow it. This doesn't mean abstracting classes of functions to make to code more generic, it means as an example to split 1 functions of 500 lines into 10 functions of 50 lines each or even 50 functions of 10 lines each but that are readable and not a "goat trails" of ifs and elses that are very hard to read and follow.

 ​I
​totally ​
agree that refacto
​ring​
code is good practice but in this
​particular ​
case I
​still ​
think
​that ​
using
​HTML ​
template is
​much ​
simpler and elegant solution
​.​


Thanks
Joao

On Tue, Mar 6, 2018 at 4:49 AM Dave Page <dpage@pgadmin.org> wrote:
Hi

On Tue, Mar 6, 2018 at 6:59 AM, Murtuza Zabuawala <murtuza.zabuawala@enterprisedb.com> wrote:
Hi Joao,

Your suggestion is good but in my own opinion it is overkill just to replace code {{ username| gravatar }} as rest of the syntax is pure HTML, This makes code much simpler and easy to understand.
Apart from that we will be rendering 'index.html' template anyways and I don't see any extra overhead.

For this particular issue, I'm inclined to agree.

I do however, like the idea of caching preferences (no-brainer really, assuming we ensure we update/invalidate the cache when needed), and using promises for accessing them.
 

I may be wrong, Let's wait for the input from other community members.

Regards, 
Murtuza

On Tue, Mar 6, 2018 at 1:17 AM, Joao De Almeida Pereira <jdealmeidapereira@pivotal.io> wrote:
Hello,
I understand that, but what do we win by using it? As you have noticed by now I am not very fond of using templates for everything, and I believe this is one of the things that we can skip templates. 
We might even shift this to a frontend concern and if we want there are node libraries to get gravatars.

I was able to create a PoC as a sample of what I was talking about:
 - Add gravatar library to package.json
 - Created a Preferences cache. (js/preferences.js)
   - So this was the concept I was talking about in a previous email we talked about caching.
      the getConfiguration and the getAllConfiguration are not great names, but they return a Promise that is consumed in the load_gravatar. The idea here is that we are caching the pgAdmin settings and when need need to consume them, we wait for it to be available.
 - load_gravatar is using the pattern to retrieve the configuration from a possible cache and then apply the gravatar


Things that need to be revisited in the PoC:
- No tests, just some spiked code
- Did not retrieve the username from the backend, we need to create an endpoint that give us this
- The url is hardcoded to get the configuration
- Maybe the Preferences is not the correct place to get server_mode configuration not sure, what do you think?


Thanks
Joao

On Mon, Mar 5, 2018 at 11:21 AM Murtuza Zabuawala <murtuza.zabuawala@enterprisedb.com> wrote:
Hi Joao,

We are using Flask-Gravatar module for this and it is designed to work with template only.

--
Regards,
Murtuza Zabuawala
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company


On Mon, Mar 5, 2018 at 8:57 PM, Joao De Almeida Pereira <jdealmeidapereira@pivotal.io> wrote:
Hi Murtuza,

Why are we doing this using templates? Can this be done with a request to the backend to get the image and then retrieve the Gravatar if it is defined or return a static image if not?

+
+{% if config.SERVER_MODE %}
 window.onload = function(e){
  setTimeout(function() {
-   var gravatarImg = '<img src="{{ username | gravatar }}" width="18" height="18" alt="Gravatar image for {{ username }}"> {{ username }} <span class="caret"></span>';
+   var gravatarImg = {{ IMG.PREPARE_HTML()|safe }}
    //$('#navbar-menu .navbar-right > li > a').html(gravatarImg);
    var navbarRight = document.getElementById("navbar-menu").getElementsByClassName("navbar-right")[0];
    if (navbarRight) {

what if we have 

var gravatarImg = '<img src="/user/{{username}}/gravatar" width="18" height="18" alt="Gravatar image for {{ username }}"> {{ username }} <span class="caret"></span>';

And then the endpoint
/user/{{username}}/gravatar 
would be responsible for returning a Gravatar or not depending on the configuration?


Thanks
Joao

On Mon, Mar 5, 2018 at 3:13 AM Murtuza Zabuawala <murtuza.zabuawala@enterprisedb.com> wrote:
Hi,

PFA patch to disable Gravatar image in Server mode.

Requirments & Issues:
- For security reasons.
- For systems which do not have internet access.
- Hangs pgAdmin4 while loading the page if connection has no internet access (as described in the ticket)

--
Regards,
Murtuza Zabuawala
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company






--
Dave Page
Blog: http://pgsnake.blogspot.com
Twitter: @pgsnake

EnterpriseDB UK: http://www.enterprisedb.com
The Enterprise PostgreSQL Company

В списке pgadmin-hackers по дате отправления:

Предыдущее
От: Murtuza Zabuawala
Дата:
Сообщение: Re: [pgAdmin4][RM#2989] To fix the issue in Table node
Следующее
От: Dave Page
Дата:
Сообщение: pgAdmin 4 commit: Ensure all messages are retrieved from the serverin