22"""
33Copyright (c) 2019 - present AppSeed.us
44"""
5-
5+ import wtforms
66from apps .home import blueprint
77from flask import render_template , request , redirect , url_for
88from jinja2 import TemplateNotFound
99from flask_login import login_required , current_user
1010from apps import db
11+ from apps .authentication .models import Users
12+ from flask_wtf import FlaskForm
1113
1214@blueprint .route ('/' )
1315@blueprint .route ('/index' )
@@ -31,28 +33,68 @@ def virtual_reality():
3133 return render_template ('pages/virtual-reality.html' , segment = 'virtual_reality' )
3234
3335
36+ def getField (column ):
37+ if isinstance (column .type , db .Text ):
38+ return wtforms .TextAreaField (column .name .title ())
39+ if isinstance (column .type , db .String ):
40+ return wtforms .StringField (column .name .title ())
41+ if isinstance (column .type , db .Boolean ):
42+ return wtforms .BooleanField (column .name .title ())
43+ if isinstance (column .type , db .Integer ):
44+ return wtforms .IntegerField (column .name .title ())
45+ if isinstance (column .type , db .Float ):
46+ return wtforms .DecimalField (column .name .title ())
47+ if isinstance (column .type , db .LargeBinary ):
48+ return wtforms .HiddenField (column .name .title ())
49+ return wtforms .StringField (column .name .title ())
50+
51+
3452@blueprint .route ('/profile' , methods = ['GET' , 'POST' ])
3553@login_required
3654def profile ():
37- if request .method == 'POST' :
38- first_name = request .form .get ('first_name' )
39- last_name = request .form .get ('last_name' )
40- address = request .form .get ('address' )
41- bio = request .form .get ('bio' )
42-
43- current_user .first_name = first_name
44- current_user .last_name = last_name
45- current_user .address = address
46- current_user .bio = bio
47-
48- try :
49- db .session .commit ()
50- except Exception as e :
51- db .session .rollback ()
5255
53- return redirect (url_for ('home_blueprint.profile' ))
56+ class ProfileForm (FlaskForm ):
57+ pass
58+
59+ readonly_fields = Users .readonly_fields
60+ full_width_fields = {"bio" }
61+
62+ for column in Users .__table__ .columns :
63+ if column .name == "id" :
64+ continue
5465
55- return render_template ('pages/profile.html' , segment = 'profile' )
66+ field_name = column .name
67+ if field_name in full_width_fields :
68+ continue
69+
70+ field = getField (column )
71+ setattr (ProfileForm , field_name , field )
72+
73+ for field_name in full_width_fields :
74+ if field_name in Users .__table__ .columns :
75+ column = Users .__table__ .columns [field_name ]
76+ field = getField (column )
77+ setattr (ProfileForm , field_name , field )
78+
79+ form = ProfileForm (obj = current_user )
80+
81+ if form .validate_on_submit ():
82+ readonly_fields .append ("password" )
83+ excluded_fields = readonly_fields
84+ for field_name , field_value in form .data .items ():
85+ if field_name not in excluded_fields :
86+ setattr (current_user , field_name , field_value )
87+
88+ db .session .commit ()
89+ return redirect (url_for ('home_blueprint.profile' ))
90+
91+ context = {
92+ 'segment' : 'profile' ,
93+ 'form' : form ,
94+ 'readonly_fields' : readonly_fields ,
95+ 'full_width_fields' : full_width_fields ,
96+ }
97+ return render_template ('pages/profile.html' , ** context )
5698
5799
58100# Helper - Extract current page name from request
0 commit comments