added animal view.
This commit is contained in:
		
							parent
							
								
									8d2e0381a7
								
							
						
					
					
						commit
						36e663e1ed
					
				
							
								
								
									
										67
									
								
								src/snek/view/avatar_animal_view.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										67
									
								
								src/snek/view/avatar_animal_view.py
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,67 @@
 | 
				
			|||||||
 | 
					# Written by retoor@molodetz.nl
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# This code defines a WebView class that inherits from BaseView and includes a method for rendering a web template, requiring login access for its usage.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# The code imports the BaseView class from the `snek.system.view` module.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# MIT License
 | 
				
			||||||
 | 
					#
 | 
				
			||||||
 | 
					# Permission is hereby granted, free of charge, to any person obtaining a copy
 | 
				
			||||||
 | 
					# of this software and associated documentation files (the "Software"), to deal
 | 
				
			||||||
 | 
					# in the Software without restriction, including without limitation the rights
 | 
				
			||||||
 | 
					# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 | 
				
			||||||
 | 
					# copies of the Software, and to permit persons to whom the Software is
 | 
				
			||||||
 | 
					# furnished to do so, subject to the following conditions:
 | 
				
			||||||
 | 
					#
 | 
				
			||||||
 | 
					# The above copyright notice and this permission notice shall be included in all
 | 
				
			||||||
 | 
					# copies or substantial portions of the Software.
 | 
				
			||||||
 | 
					#
 | 
				
			||||||
 | 
					# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 | 
				
			||||||
 | 
					# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 | 
				
			||||||
 | 
					# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 | 
				
			||||||
 | 
					# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 | 
				
			||||||
 | 
					# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 | 
				
			||||||
 | 
					# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 | 
				
			||||||
 | 
					# SOFTWARE.
 | 
				
			||||||
 | 
					import uuid
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					from aiohttp import web
 | 
				
			||||||
 | 
					from multiavatar import multiavatar
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					from snek.system.view import BaseView
 | 
				
			||||||
 | 
					from snek.view.avatar_animal import generate_avatar_with_options
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import functools
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class AvatarView(BaseView):
 | 
				
			||||||
 | 
					    login_required = False
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    def __init__(self, *args,**kwargs):
 | 
				
			||||||
 | 
					        super().__init__(*args,**kwargs)
 | 
				
			||||||
 | 
					        self.avatars = {}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    async def get(self):
 | 
				
			||||||
 | 
					        uid = self.request.match_info.get("uid")
 | 
				
			||||||
 | 
					        while True:
 | 
				
			||||||
 | 
					            try:
 | 
				
			||||||
 | 
					                return web.Response(text=self._get(uid), content_type="image/svg+xml")
 | 
				
			||||||
 | 
					            except Exception as e:
 | 
				
			||||||
 | 
					                pass
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    def _get(self, uid):
 | 
				
			||||||
 | 
					        if uid in self.avatars:
 | 
				
			||||||
 | 
					            return self.avatars[uid]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        avatar = generate_avatar_with_options(self.request.query)
 | 
				
			||||||
 | 
					        self.avatars[uid] = avatar
 | 
				
			||||||
 | 
					        return avatar
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    async def get2(self):
 | 
				
			||||||
 | 
					        uid = self.request.match_info.get("uid")
 | 
				
			||||||
 | 
					        if uid == "unique":
 | 
				
			||||||
 | 
					            uid = str(uuid.uuid4())
 | 
				
			||||||
 | 
					        avatar = multiavatar.multiavatar(uid, True, None)
 | 
				
			||||||
 | 
					        response = web.Response(text=avatar, content_type="image/svg+xml")
 | 
				
			||||||
 | 
					        response.headers["Cache-Control"] = f"public, max-age={1337*42}"
 | 
				
			||||||
 | 
					        return response
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user