Python app.db.session() Examples
The following are 13
code examples of app.db.session().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
app.db
, or try the search function
.
Example #1
Source File: services_dao.py From notifications-api with MIT License | 6 votes |
def dao_archive_service(service_id): # have to eager load templates and api keys so that we don't flush when we loop through them # to ensure that db.session still contains the models when it comes to creating history objects service = Service.query.options( joinedload('templates'), joinedload('templates.template_redacted'), joinedload('api_keys'), ).filter(Service.id == service_id).one() service.active = False service.name = get_archived_db_column_value(service.name) service.email_from = get_archived_db_column_value(service.email_from) for template in service.templates: if not template.archived: template.archived = True for api_key in service.api_keys: if not api_key.expiry_date: api_key.expiry_date = datetime.utcnow()
Example #2
Source File: services_dao.py From notifications-api with MIT License | 6 votes |
def dao_add_user_to_service(service, user, permissions=None, folder_permissions=None): permissions = permissions or [] folder_permissions = folder_permissions or [] try: from app.dao.permissions_dao import permission_dao service.users.append(user) permission_dao.set_user_service_permission(user, service, permissions, _commit=False) db.session.add(service) service_user = dao_get_service_user(user.id, service.id) valid_template_folders = dao_get_valid_template_folders_by_id(folder_permissions) service_user.folders = valid_template_folders db.session.add(service_user) except Exception as e: db.session.rollback() raise e else: db.session.commit()
Example #3
Source File: services_dao.py From notifications-api with MIT License | 6 votes |
def dao_find_services_sending_to_tv_numbers(start_date, end_date, threshold=500): return db.session.query( Notification.service_id.label('service_id'), func.count(Notification.id).label('notification_count') ).filter( Notification.service_id == Service.id, Notification.created_at >= start_date, Notification.created_at <= end_date, Notification.key_type != KEY_TYPE_TEST, Notification.notification_type == SMS_TYPE, func.substr(Notification.normalised_to, 3, 7) == '7700900', Service.restricted == False, # noqa Service.research_mode == False, Service.active == True, ).group_by( Notification.service_id, ).having( func.count(Notification.id) > threshold ).all()
Example #4
Source File: services_dao.py From notifications-api with MIT License | 5 votes |
def dao_update_service(service): db.session.add(service)
Example #5
Source File: services_dao.py From notifications-api with MIT License | 5 votes |
def dao_remove_user_from_service(service, user): try: from app.dao.permissions_dao import permission_dao permission_dao.remove_user_service_permissions(user, service) service_user = dao_get_service_user(user.id, service.id) db.session.delete(service_user) except Exception as e: db.session.rollback() raise e else: db.session.commit()
Example #6
Source File: services_dao.py From notifications-api with MIT License | 5 votes |
def fetch_todays_total_message_count(service_id): result = db.session.query( func.count(Notification.id).label('count') ).filter( Notification.service_id == service_id, Notification.key_type != KEY_TYPE_TEST, func.date(Notification.created_at) == date.today() ).group_by( Notification.notification_type, Notification.status, ).first() return 0 if result is None else result.count
Example #7
Source File: services_dao.py From notifications-api with MIT License | 5 votes |
def _stats_for_service_query(service_id): return db.session.query( Notification.notification_type, Notification.status, func.count(Notification.id).label('count') ).filter( Notification.service_id == service_id, Notification.key_type != KEY_TYPE_TEST ).group_by( Notification.notification_type, Notification.status, )
Example #8
Source File: services_dao.py From notifications-api with MIT License | 5 votes |
def dao_suspend_service(service_id): # have to eager load api keys so that we don't flush when we loop through them # to ensure that db.session still contains the models when it comes to creating history objects service = Service.query.options( joinedload('api_keys'), ).filter(Service.id == service_id).one() for api_key in service.api_keys: if not api_key.expiry_date: api_key.expiry_date = datetime.utcnow() service.active = False
Example #9
Source File: models.py From flask-todolist with MIT License | 5 votes |
def __commit(self): """Commits the current db.session, does rollback on failure.""" from sqlalchemy.exc import IntegrityError try: db.session.commit() except IntegrityError: db.session.rollback()
Example #10
Source File: models.py From flask-todolist with MIT License | 5 votes |
def delete(self): """Deletes this model from the db (through db.session)""" db.session.delete(self) self.__commit()
Example #11
Source File: models.py From flask-todolist with MIT License | 5 votes |
def save(self): """Adds this model to the db (through db.session)""" db.session.add(self) self.__commit() return self
Example #12
Source File: services_dao.py From notifications-api with MIT License | 4 votes |
def dao_create_service( service, user, service_id=None, service_permissions=None, ): # the default property does not appear to work when there is a difference between the sqlalchemy schema and the # db schema (ie: during a migration), so we have to set sms_sender manually here. After the GOVUK sms_sender # migration is completed, this code should be able to be removed. if not user: raise ValueError("Can't create a service without a user") if service_permissions is None: service_permissions = DEFAULT_SERVICE_PERMISSIONS organisation = dao_get_organisation_by_email_address(user.email_address) from app.dao.permissions_dao import permission_dao service.users.append(user) permission_dao.add_default_service_permissions_for_user(user, service) service.id = service_id or uuid.uuid4() # must be set now so version history model can use same id service.active = True service.research_mode = False for permission in service_permissions: service_permission = ServicePermission(service_id=service.id, permission=permission) service.permissions.append(service_permission) # do we just add the default - or will we get a value from FE? insert_service_sms_sender(service, current_app.config['FROM_NUMBER']) if organisation: service.organisation_id = organisation.id service.organisation_type = organisation.organisation_type if organisation.email_branding: service.email_branding = organisation.email_branding if organisation.letter_branding and not service.letter_branding: service.letter_branding = organisation.letter_branding elif service.organisation_type in NHS_ORGANISATION_TYPES or email_address_is_nhs(user.email_address): service.email_branding = dao_get_email_branding_by_name('NHS') service.letter_branding = dao_get_letter_branding_by_name('NHS') if organisation: service.crown = organisation.crown elif service.organisation_type in CROWN_ORGANISATION_TYPES: service.crown = True elif service.organisation_type in NON_CROWN_ORGANISATION_TYPES: service.crown = False service.count_as_live = not user.platform_admin db.session.add(service)
Example #13
Source File: services_dao.py From notifications-api with MIT License | 4 votes |
def dao_fetch_todays_stats_for_all_services(include_from_test_key=True, only_active=True): today = date.today() start_date = get_london_midnight_in_utc(today) end_date = get_london_midnight_in_utc(today + timedelta(days=1)) subquery = db.session.query( Notification.notification_type, Notification.status, Notification.service_id, func.count(Notification.id).label('count') ).filter( Notification.created_at >= start_date, Notification.created_at < end_date ).group_by( Notification.notification_type, Notification.status, Notification.service_id ) if not include_from_test_key: subquery = subquery.filter(Notification.key_type != KEY_TYPE_TEST) subquery = subquery.subquery() query = db.session.query( Service.id.label('service_id'), Service.name, Service.restricted, Service.research_mode, Service.active, Service.created_at, subquery.c.notification_type, subquery.c.status, subquery.c.count ).outerjoin( subquery, subquery.c.service_id == Service.id ).order_by(Service.id) if only_active: query = query.filter(Service.active) return query.all()