2022-11-19 12:38:25 -08:00
from django import forms
2023-05-03 21:42:37 -07:00
from django . contrib import messages
from django . contrib . auth . decorators import login_required
2022-11-19 12:38:25 -08:00
from django . core . files import File
from django . shortcuts import redirect
from django . utils . decorators import method_decorator
from django . views . generic import FormView
2023-01-29 16:46:22 -08:00
from core . html import FediverseHtmlParser
2022-12-14 09:15:46 -08:00
from core . models . config import Config
2022-12-21 09:13:39 -08:00
from users . models import IdentityStates
2022-12-21 12:48:39 -08:00
from users . services import IdentityService
2023-05-03 21:42:37 -07:00
from users . shortcuts import by_handle_or_404
2022-11-19 12:38:25 -08:00
2023-05-03 21:42:37 -07:00
@method_decorator ( login_required , name = " dispatch " )
2022-11-19 12:38:25 -08:00
class ProfilePage ( FormView ) :
"""
Lets the identity ' s profile be edited
"""
template_name = " settings/profile.html "
extra_context = { " section " : " profile " }
class form_class ( forms . Form ) :
name = forms . CharField ( max_length = 500 )
summary = forms . CharField (
widget = forms . Textarea ,
required = False ,
help_text = " Describe you and your interests " ,
label = " Bio " ,
)
icon = forms . ImageField (
required = False , help_text = " Shown next to all your posts and activities "
)
image = forms . ImageField (
required = False , help_text = " Shown at the top of your profile "
)
2022-11-25 17:32:45 -08:00
discoverable = forms . BooleanField (
2023-05-03 21:42:37 -07:00
help_text = " If this user is visible on the frontpage and in user directories " ,
2022-11-25 17:32:45 -08:00
widget = forms . Select (
choices = [ ( True , " Discoverable " ) , ( False , " Not Discoverable " ) ]
) ,
required = False ,
)
2022-12-14 09:15:46 -08:00
visible_follows = forms . BooleanField (
2023-05-03 21:42:37 -07:00
help_text = " Whether or not to show your following and follower counts in your profile " ,
2022-12-14 09:15:46 -08:00
widget = forms . Select ( choices = [ ( True , " Visible " ) , ( False , " Hidden " ) ] ) ,
required = False ,
)
2023-05-03 21:42:37 -07:00
search_enabled = forms . BooleanField (
help_text = " If a search feature is provided for your posts on the profile page \n (Disabling this will not prevent third-party search crawlers from indexing your posts) " ,
widget = forms . Select ( choices = [ ( True , " Enabled " ) , ( False , " Disabled " ) ] ) ,
required = False ,
)
2023-05-13 10:07:57 -07:00
boosts_on_profile = forms . BooleanField (
help_text = " Include your boosts with your posts on your profile page " ,
widget = forms . Select ( choices = [ ( True , " Enabled " ) , ( False , " Disabled " ) ] ) ,
required = False ,
)
2022-12-27 15:56:50 -08:00
metadata = forms . JSONField (
label = " Profile Metadata Fields " ,
2023-05-03 21:42:37 -07:00
help_text = " These values will appear on your profile below your bio " ,
2022-12-27 16:45:51 -08:00
widget = forms . HiddenInput ( attrs = { " data-min-empty " : 2 } ) ,
2022-12-27 15:56:50 -08:00
required = False ,
)
2022-11-19 12:38:25 -08:00
2022-12-27 16:45:51 -08:00
def clean_metadata ( self ) :
metadata = self . cleaned_data [ " metadata " ]
if metadata :
metadata = [ x for x in metadata if x [ " name " ] and x [ " value " ] ]
if not metadata :
return None
return metadata
2023-05-03 21:42:37 -07:00
def dispatch ( self , request , handle : str , * args , * * kwargs ) :
self . identity = by_handle_or_404 ( self . request , handle , local = True , fetch = False )
return super ( ) . dispatch ( request , * args , * * kwargs )
def get_context_data ( self , * * kwargs ) :
context = super ( ) . get_context_data ( * * kwargs )
context [ " identity " ] = self . identity
return context
2022-11-19 12:38:25 -08:00
def get_initial ( self ) :
return {
2023-05-03 21:42:37 -07:00
" name " : self . identity . name ,
2023-01-29 16:46:22 -08:00
" summary " : (
2023-05-03 21:42:37 -07:00
FediverseHtmlParser ( self . identity . summary ) . plain_text
if self . identity . summary
2023-01-29 16:46:22 -08:00
else " "
) ,
2023-05-03 21:42:37 -07:00
" icon " : self . identity . icon and self . identity . icon . url ,
" image " : self . identity . image and self . identity . image . url ,
" discoverable " : self . identity . discoverable ,
" visible_follows " : self . identity . config_identity . visible_follows ,
" metadata " : self . identity . metadata or [ ] ,
" search_enabled " : self . identity . config_identity . search_enabled ,
2023-05-13 10:07:57 -07:00
" boosts_on_profile " : self . identity . config_identity . boosts_on_profile ,
2022-11-19 12:38:25 -08:00
}
def form_valid ( self , form ) :
2022-11-25 17:32:45 -08:00
# Update basic info
2023-05-03 21:42:37 -07:00
service = IdentityService ( self . identity )
self . identity . name = form . cleaned_data [ " name " ]
self . identity . discoverable = form . cleaned_data [ " discoverable " ]
2023-01-15 15:15:57 -08:00
service . set_summary ( form . cleaned_data [ " summary " ] )
2022-11-19 12:38:25 -08:00
# Resize images
icon = form . cleaned_data . get ( " icon " )
image = form . cleaned_data . get ( " image " )
if isinstance ( icon , File ) :
2023-01-15 15:15:57 -08:00
service . set_icon ( icon )
2022-11-19 12:38:25 -08:00
if isinstance ( image , File ) :
2023-01-15 15:15:57 -08:00
service . set_image ( image )
2023-05-03 21:42:37 -07:00
self . identity . metadata = form . cleaned_data . get ( " metadata " )
2023-01-08 13:47:44 -08:00
# Clear images if specified
if " icon__clear " in self . request . POST :
2023-05-03 21:42:37 -07:00
self . identity . icon = None
2023-01-08 13:47:44 -08:00
if " image__clear " in self . request . POST :
2023-05-03 21:42:37 -07:00
self . identity . image = None
2023-01-08 13:47:44 -08:00
# Save and propagate
2023-05-03 21:42:37 -07:00
self . identity . save ( )
self . identity . transition_perform ( IdentityStates . edited )
2022-12-21 09:13:39 -08:00
2022-12-14 09:15:46 -08:00
# Save profile-specific identity Config
Config . set_identity (
2023-05-03 21:42:37 -07:00
self . identity , " visible_follows " , form . cleaned_data [ " visible_follows " ]
2022-12-14 09:15:46 -08:00
)
2023-05-03 21:42:37 -07:00
Config . set_identity (
self . identity , " search_enabled " , form . cleaned_data [ " search_enabled " ]
)
2023-05-13 10:07:57 -07:00
Config . set_identity (
self . identity , " boosts_on_profile " , form . cleaned_data [ " boosts_on_profile " ]
)
2023-05-03 21:42:37 -07:00
messages . success ( self . request , " Your profile has been updated. " )
2022-11-19 12:38:25 -08:00
return redirect ( " . " )