Exemplo n.º 1
0
 def save(self, location, language):
     data = self.cleaned_data
     user = super(RegistrationForm, self).save(commit=False)
     user.last_login = timezone.now()
     user.email = data['email']
     user.first_name = data['first_name']
     # user.last_name = data['last_name']
     user.save()
     profile = Profile(user=user, name=data.get('name', data['username']))
     if not location.id:
         location.save()
     profile.location = location
     # profile.name = data['first_name'] + ' ' + data['last_name']
     profile.save()
     profile.settings.email = data['email']
     profile.settings.language = language
     profile.settings.feed_radius = -1
     profile.settings.save()
     return profile
Exemplo n.º 2
0
def edit_about(userId):
    """ Profile post method to edit about """
    edit_about_form = EditAbout()

    if edit_about_form.validate_on_submit():
        birthday = edit_about_form.birthday.data
        political_spectrum = edit_about_form.political_spectrum.data
        political_party = edit_about_form.political_party.data
        favourite_news_websites = edit_about_form.favourite_news_websites.data
        allow_location_detection = edit_about_form.allow_location_detection.data

        profile = Profile.query.filter_by(user_id=userId).first()
        if profile:
            # update profile
            if profile.birthday != birthday:
                profile.birthday = birthday
                db.session.flush()

            if profile.political_spectrum != political_spectrum:
                profile.political_spectrum = political_spectrum
                db.session.flush()

            if profile.political_party != political_party:
                profile.political_party = political_party
                db.session.flush()

            if profile.favourite_news_websites != ",".join(
                    favourite_news_websites):
                profile.favourite_news_websites = ",".join(
                    favourite_news_websites)
                db.session.flush()

            if profile.allow_location_detection == True and allow_location_detection == 'no' or profile.allow_location_detection == False and allow_location_detection == 'yes':
                if allow_location_detection == 'yes':
                    profile.allow_location_detection = True
                else:
                    profile.allow_location_detection = False

                db.session.flush()
        else:
            # create new profile
            # convert allow_location_detection to bool
            allow_bool = None
            if allow_location_detection == 'yes':
                allow_bool = True
            else:
                allow_bool = False

            profile = Profile(userId, birthday, political_spectrum,
                              political_party, favourite_news_websites,
                              allow_bool, None, None, None)
            db.session.add(profile)
            db.session.flush()

        # if allow_location_detection, get location
        if profile.allow_location_detection == True:
            g = geocoder.ip('me')
            if profile.location != g.city + ", " + g.country or profile.lat != g.latlng[
                    0] or profile.lon != g.latlng[1]:
                # add location
                profile.lat = g.latlng[0]
                profile.lon = g.latlng[1]
                profile.location = g.city + ", " + g.country
                db.session.flush()

        db.session.commit()

    return redirect(url_for('about'))