forked from fediversity/fediversity
		
	implement login URL as templatetag
this eases testing as it allows generating that URL inside a test instead of scraping HTML output
This commit is contained in:
		
							parent
							
								
									647d89798e
								
							
						
					
					
						commit
						fc586273dc
					
				
					 3 changed files with 19 additions and 2 deletions
				
			
		|  | @ -16,10 +16,11 @@ | |||
| 
 | ||||
|   <body> | ||||
|     <header> | ||||
|     {% load custom_tags %} | ||||
|     {% if user.is_authenticated %} | ||||
|       <p>Welcome, {{ user.username }}! <a href="{% url 'logout' %}?next={{ request.path }}">Logout</a></p> | ||||
|       <p>Welcome, {{ user.username }}! <a href="{% auth_url 'logout' %}">Logout</a></p> | ||||
|     {% else %} | ||||
|       <p><a href="{% url 'login' %}?next={{ request.path }}">Login</a></p> | ||||
|       <p><a href="{% auth_url 'login' %}">Login</a></p> | ||||
|     {% endif %} | ||||
|     </header> | ||||
|     {% block navigation %} | ||||
|  |  | |||
							
								
								
									
										0
									
								
								panel/src/panel/templatetags/__init__.py
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										0
									
								
								panel/src/panel/templatetags/__init__.py
									
										
									
									
									
										Normal file
									
								
							
							
								
								
									
										16
									
								
								panel/src/panel/templatetags/custom_tags.py
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								panel/src/panel/templatetags/custom_tags.py
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,16 @@ | |||
| from django import template | ||||
| from django.urls import reverse | ||||
| from urllib.parse import urlencode | ||||
| 
 | ||||
| register = template.Library() | ||||
| 
 | ||||
| @register.simple_tag(takes_context=True) | ||||
| def auth_url(context, action): | ||||
|     """Generate login/logout URL with current path as redirect.""" | ||||
|     request = context['request'] | ||||
|     view = context.get('view') | ||||
|     redirect_field_name = getattr(view, 'redirect_field_name', 'next') | ||||
| 
 | ||||
|     base_url = reverse(action) | ||||
|     query_params = {redirect_field_name: request.path} | ||||
|     return f"{base_url}?{urlencode(query_params)}" | ||||
		Loading…
	
	Add table
		
		Reference in a new issue