def perform_import(file):
     with open(file) as data_file:
         data = json.load(data_file)
         for person in data:
             user = User.query.filter_by(email=person['email']).first()
             if user is None:
                 user = User(email=person['email'])
                 db.session.add(user) and db.session.commit()
             created_at = parser.parse(person['created_at'])
             [person.pop(k,None) for k in ['email','created_at']]
             UserMessageInfo.first_or_create(user.id, created_at, **person)
             umi = None
             for item in UserMessageInfo.query.filter_by(user_id=user.id):
                 item.default = False
                 if umi is None or umi.created_at.replace(tzinfo=None) < item.created_at.replace(tzinfo=None):
                     umi = item
             if umi is not None:
                 umi.default = True
                 db.session.commit()
Example #2
0
 def perform_import(file):
     with open(file) as data_file:
         data = json.load(data_file)
         for person in data:
             user = User.query.filter_by(email=person['email']).first()
             if user is None:
                 user = User(email=person['email'])
                 db.session.add(user) and db.session.commit()
             created_at = parser.parse(person['created_at'])
             [person.pop(k, None) for k in ['email', 'created_at']]
             UserMessageInfo.first_or_create(user.id, created_at, **person)
             umi = None
             for item in UserMessageInfo.query.filter_by(user_id=user.id):
                 item.default = False
                 if umi is None or umi.created_at.replace(
                         tzinfo=None) < item.created_at.replace(
                             tzinfo=None):
                     umi = item
             if umi is not None:
                 umi.default = True
                 db.session.commit()
Example #3
0
    def validate_and_save_to_db(self, user, msg=None, accept_tos=True):
        """
        Validates the form and (if valid) saves the data to the database.

        @param user: the user
        @type user: models.User
        @param msg: the message
        @type msg: models.Message
        @return: True if validation and save is successful, False otherwise
        @rtype: boolean
        """
        # self._autocomplete_tos()

        self._autocomplete_email(user.email)
        self._autocomplete_phone()
        self._doctor_names()

        if self.validate():
            # get user's default info and either create new info or get the same info instance
            first_umi = UserMessageInfo.query.filter_by(user=user,
                                                        default=True).first()
            umi = UserMessageInfo.first_or_create(user.id, **self.data_dict())

            #umi = first_umi if (first_umi and first_umi.accept_tos is None) \
            #    else UserMessageInfo.first_or_create(user.id, **self.data_dict())

            # check if the new user info differs from the newly submitted info and adjust appropriately
            if first_umi.id is not umi.id:
                first_umi.default = False
                umi.default = True
                if msg is not None:
                    msg.user_message_info = umi

            # save form data to models
            for field, val in self.data.iteritems():
                try:
                    setattr(umi, field, str(val))
                except:
                    return False, 'error'

            # form is valid so user accepted tos at this time
            umi.accept_tos = datetime.datetime.now() if accept_tos else None

            # if everything succeeded then we commit and return True
            db.session.commit()

            if umi.determine_district(force=True) is None:
                return False, 'district_error'

            return True, 'success'
        else:
            return False, 'invalid_form_error'
Example #4
0
def create_admin(app, csrf):
    from models import db, User, Legislator, AdminUser, UserMessageInfo, Message, MessageLegislator, Topic
    from flask.ext.admin.menu import MenuLink

    admin = Admin(index_view=views.MyAdminIndexView(url=settings.BASE_PREFIX + '/admin'))
    admin.add_link(MenuLink('Logout', url=settings.BASE_PREFIX + '/admin/logout'))
    admin.add_view(User.ModelView(User, db.session))
    admin.add_view(Legislator.ModelView(Legislator, db.session))
    admin.add_view(AdminUser.ModelView(AdminUser, db.session))
    admin.add_view(UserMessageInfo.ModelView(UserMessageInfo, db.session))
    admin.add_view(Message.ModelView(Message, db.session))
    admin.add_view(MessageLegislator.ModelView(MessageLegislator, db.session))
    admin.add_view(Topic.ModelView(Topic, db.session))

    return admin
Example #5
0
    def validate_and_save_to_db(self, user, msg=None):
        """
        Validates the form and (if valid) saves the data to the database.

        @param user: the user
        @type user: models.User
        @param msg: the message
        @type msg: models.Message
        @return: True if validation and save is successful, False otherwise
        @rtype: boolean
        """
        self._autocomplete_tos()
        self._autocomplete_email(user.email)
        self._autocomplete_zip()
        self._autocomplete_phone()
        self._doctor_names()

        if self.validate():
            # get user's default info and either create new info or get the same info instance
            first_umi = UserMessageInfo.query.filter_by(user=user, default=True).first()
            umi = first_umi if (first_umi and first_umi.accept_tos is None) \
                else UserMessageInfo.first_or_create(user.id, **self.data_dict())

            # check if the new user info differs from the newly submitted info and adjust appropriately
            if first_umi.id is not umi.id:
                first_umi.default = False
                umi.default = True
                if msg is not None:
                    msg.user_message_info = umi

            # save form data to models
            for field, val in self.data.iteritems():
                try:
                    setattr(umi, field, str(val))
                except:
                    return False

            # form is valid so user accepted tos at this time
            umi.accept_tos = datetime.datetime.now()

            # if everything succeeded then we commit and return True
            db.session.commit()
            return True
        else:
            self.zip5.data = self.zip5.data + '-' + self.zip4.data
            return False