<s> from __future__ import absolute_import , unicode_literals <EOL> from django . db import models <EOL> from django . utils . six . moves . urllib . parse import urlparse <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> class Redirect ( models . Model ) : <EOL> old_path = models . CharField ( verbose_name = _ ( "<STR_LIT>" ) , max_length = <NUM_LIT:255> , db_index = True ) <EOL> site = models . ForeignKey ( <EOL> '<STR_LIT>' , <EOL> verbose_name = _ ( '<STR_LIT>' ) , <EOL> null = True , blank = True , <EOL> related_name = '<STR_LIT>' , <EOL> db_index = True , <EOL> on_delete = models . CASCADE <EOL> ) <EOL> is_permanent = models . BooleanField ( verbose_name = _ ( "<STR_LIT>" ) , default = True , help_text = _ ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) ) <EOL> redirect_page = models . ForeignKey ( <EOL> '<STR_LIT>' , <EOL> verbose_name = _ ( "<STR_LIT>" ) , <EOL> null = True , blank = True , <EOL> on_delete = models . CASCADE <EOL> ) <EOL> redirect_link = models . URLField ( verbose_name = _ ( "<STR_LIT>" ) , blank = True ) <EOL> @ property <EOL> def title ( self ) : <EOL> return self . old_path <EOL> @ property <EOL> def link ( self ) : <EOL> if self . redirect_page : <EOL> return self . redirect_page . url <EOL> else : <EOL> return self . redirect_link <EOL> def get_is_permanent_display ( self ) : <EOL> if self . is_permanent : <EOL> return "<STR_LIT>" <EOL> else : <EOL> return "<STR_LIT>" <EOL> @ classmethod <EOL> def get_for_site ( cls , site = None ) : <EOL> if site : <EOL> return cls . objects . filter ( models . Q ( site = site ) | models . Q ( site = None ) ) <EOL> else : <EOL> return cls . objects . all ( ) <EOL> @ staticmethod <EOL> def normalise_path ( url ) : <EOL> url = url . strip ( ) <EOL> url_parsed = urlparse ( url ) <EOL> path = url_parsed [ <NUM_LIT:2> ] <EOL> if not path . startswith ( '<STR_LIT:/>' ) : <EOL> path = '<STR_LIT:/>' + path <EOL> if path . endswith ( '<STR_LIT:/>' ) : <EOL> path = path [ : - <NUM_LIT:1> ] <EOL> parameters = url_parsed [ <NUM_LIT:3> ] <EOL> parameters_components = parameters . split ( '<STR_LIT:;>' ) <EOL> parameters = '<STR_LIT:;>' . join ( sorted ( parameters_components ) ) <EOL> query_string = url_parsed [ <NUM_LIT:4> ] <EOL> query_string_components = query_string . split ( '<STR_LIT:&>' ) <EOL> query_string = '<STR_LIT:&>' . join ( sorted ( query_string_components ) ) <EOL> if parameters : <EOL> path = path + '<STR_LIT:;>' + parameters <EOL> if query_string : <EOL> path = path + '<STR_LIT:?>' + query_string <EOL> return path <EOL> def clean ( self ) : <EOL> self . old_path = Redirect . normalise_path ( self . old_path ) <EOL> class Meta : <EOL> verbose_name = _ ( '<STR_LIT>' ) <EOL> unique_together = [ ( '<STR_LIT>' , '<STR_LIT>' ) ] </s>
