Example #1
0
 def test_change_and_save(self):
     obj = ShortcutsModel.objects.create(name='test1',
                                         datetime=timezone.now(),
                                         number=1)
     change_and_save(obj, name='modified')
     assert_equal(obj.name, 'modified')
     assert_equal(ShortcutsModel.objects.first().name,
                  'modified')  # instance is changed and saved to DB
Example #2
0
 def change_and_save(self, update_only_changed_fields=False, **changed_fields):
     """
     Changes a given `changed_fields` on this object, saves it and returns itself.
     :param update_only_changed_fields: only changed fields will be updated in the database.
     :param changed_fields: fields to change.
     :return: self
     """
     change_and_save(self, update_only_changed_fields=update_only_changed_fields, **changed_fields)
     return self
Example #3
0
 def change_and_save(self, update_only_changed_fields=False, **changed_fields):
     """
     Changes a given `changed_fields` on this object, saves it and returns itself.
     :param update_only_changed_fields: only changed fields will be updated in the database.
     :param changed_fields: fields to change.
     :return: self
     """
     change_and_save(self, update_only_changed_fields=update_only_changed_fields, **changed_fields)
     return self
Example #4
0
 def _finish(self, success=False):
     if self.command_log.stop is None:
         change_and_save(
             self.command_log,
             output=self.output.getvalue(),
             stop=now(),
             is_successful=success
         )
         self._stop_intercepting()
Example #5
0
def send(output_sms):
    try:
        if output_sms.state == ATS_STATES.PROCESSING:
            parsed_response = send_and_parse_response(output_sms)
            update_sms_state_from_response(output_sms, parsed_response)
            output_sms.save()
        return output_sms
    except SMSSendingError:
        change_and_save(output_sms, state=ATS_STATES.LOCAL_TO_SEND)
        raise
Example #6
0
 def test_decimal_field(self):
     change_and_save(self.inst, decimal=3)
     assert_equal(self.inst.decimal, 3)
     assert_raises(PersistenceException, change_and_save, self.inst, decimal='2.99')
     assert_raises(PersistenceException, change_and_save, self.inst, decimal='10.00001')
     try:
         change_and_save(self.inst, decimal='11.1')
         assert_true(False, 'Previous `change_and_save` suppose to raise an exception')
     except PersistenceException as ex:
         assert_true('decimal: ' in str(ex), 'Exception message was supposed to contain a field name.')
Example #7
0
def send(output_sms):
    try:
        if output_sms.state == config.ATS_STATES.PROCESSING:
            parsed_response = send_and_parse_response(output_sms)
            update_sms_state_from_response(output_sms, parsed_response)
            output_sms.save()
        return output_sms
    except SMSSendingError:
        change_and_save(output_sms, state=config.ATS_STATES.LOCAL_TO_SEND)
        raise
Example #8
0
    def test_subchoices_field(self):
        change_and_save(self.inst, state_reason=TestFieldsModel.STATE_REASON.SUB_OK_2)
        assert_equal(self.inst.state_reason, TestFieldsModel.STATE_REASON.SUB_OK_2)
        assert_raises(PersistenceException, change_and_save, self.inst,
                      state_reason=TestFieldsModel.STATE_REASON.SUB_NOT_OK_1)

        # Change state and substate should work
        change_and_save(self.inst, state=TestFieldsModel.STATE.NOT_OK,
                        state_reason=TestFieldsModel.STATE_REASON.SUB_NOT_OK_2)
        assert_equal(self.inst.state, TestFieldsModel.STATE.NOT_OK)
        assert_equal(self.inst.state_reason, TestFieldsModel.STATE_REASON.SUB_NOT_OK_2)
Example #9
0
    def test_prev_value_field(self):
        # In case of `add`, value of state is copied to state_prev
        change_and_save(self.inst, state=TestFieldsModel.STATE.OK,
                        state_reason=TestFieldsModel.STATE_REASON.SUB_OK_1)
        assert_equal(self.inst.state, TestFieldsModel.STATE.OK)
        assert_equal(self.inst.state_prev, TestFieldsModel.STATE.OK)

        # Later, old value of state is stored in state_prev
        change_and_save(self.inst, state=TestFieldsModel.STATE.NOT_OK,
                        state_reason=TestFieldsModel.STATE_REASON.SUB_NOT_OK_1)
        assert_equal(self.inst.state_prev, TestFieldsModel.STATE.OK)
Example #10
0
    def __call__(self, apps, schema_editor):
        directory = os.path.join(settings.EMAIL_HTML_DATA_DIRECTORY)

        with codecs.open(os.path.join(directory,
                                      '{}.html'.format(self.template_slug)),
                         'r',
                         encoding='utf-8-sig') as file:
            template_obj = apps.get_model(
                *settings.EMAIL_TEMPLATE_MODEL.split('.')).objects.get(
                    slug=self.template_slug)
            change_and_save(template_obj, body=file.read())
Example #11
0
    def test_prev_value_field(self):
        # In case of `add`, value of state is copied to state_prev
        change_and_save(self.inst,
                        state=TestFieldsModel.STATE.OK,
                        state_reason=TestFieldsModel.STATE_REASON.SUB_OK_1)
        assert_equal(self.inst.state, TestFieldsModel.STATE.OK)
        assert_equal(self.inst.state_prev, TestFieldsModel.STATE.OK)

        # Later, old value of state is stored in state_prev
        change_and_save(self.inst,
                        state=TestFieldsModel.STATE.NOT_OK,
                        state_reason=TestFieldsModel.STATE_REASON.SUB_NOT_OK_1)
        assert_equal(self.inst.state_prev, TestFieldsModel.STATE.OK)
Example #12
0
    def test_file_field_content_type(self):
        # These files can be saved because it has supported type
        for filename in ('all_fields_filled.csv', 'test.pdf'):
            with open('data/{}'.format(filename), 'rb') as f:
                assert_false(self.inst.file)
                self.inst.file.save(filename, File(f))
                assert_true(self.inst.file)
                change_and_save(self.inst, file=None)

        # Image file is not supported
        with open('data/test2.jpg', 'rb') as f:
            assert_false(self.inst.file)
            assert_raises(PersistenceException, self.inst.file.save, 'image.jpeg', File(f))
Example #13
0
    def test_file_field_content_type(self):
        # These files can be saved because it has supported type
        for filename in ('all_fields_filled.csv', 'test.pdf'):
            with open('data/{}'.format(filename), 'rb') as f:
                assert_false(self.inst.file)
                self.inst.file.save(filename, File(f))
                assert_true(self.inst.file)
                change_and_save(self.inst, file=None)

        # Image file is not supported
        with open('data/test2.jpg', 'rb') as f:
            assert_false(self.inst.file)
            assert_raises(PersistenceException, self.inst.file.save,
                          'image.jpeg', File(f))
Example #14
0
def send_multiple(*multiple_output_sms):
    mutiple_output_sms_to_sent = [
        output_sms for output_sms in multiple_output_sms if output_sms.state == config.ATS_STATES.PROCESSING
    ]
    try:
        parsed_response = send_and_parse_response(*mutiple_output_sms_to_sent)
        for output_sms in mutiple_output_sms_to_sent:
            try:
                update_sms_state_from_response(output_sms, parsed_response)
                output_sms.save()
            except SMSSendingError:
                change_and_save(output_sms, state = config.ATS_STATES.LOCAL_TO_SEND)
    except SMSSendingError:
        bulk_change_and_save(mutiple_output_sms_to_sent, config.ATS_STATES.LOCAL_TO_SEND)
Example #15
0
def update_sms_states(parsed_response):
    """
    Higher-level function performing serialization of ATS requests, parsing ATS server response and updating
    SMS messages state according the received response.
    """
    for uniq, state in parsed_response.items():
        sms = get_object_or_none(config.get_output_sms_model(), pk=uniq)
        if sms:
            change_and_save(
                sms, state=state if state in config.ATS_STATES.all else config.ATS_STATES.LOCAL_UNKNOWN_ATS_STATE,
                sent_at=timezone.now()
            )
        else:
            raise SMSValidationError(ugettext('SMS with uniq "{}" not found in DB.').format(uniq))
Example #16
0
def update_sms_states(parsed_response):
    """
    Higher-level function performing serialization of ATS requests, parsing ATS server response and updating
    SMS messages state according the received response.
    """
    for uniq, state in parsed_response.items():
        sms = get_object_or_none(get_output_sms_model(), pk=uniq)
        if sms:
            change_and_save(sms,
                            state=state if state in ATS_STATES.all else
                            ATS_STATES.LOCAL_UNKNOWN_ATS_STATE,
                            sent_at=timezone.now())
        else:
            raise SMSValidationError(
                ugettext('SMS with uniq "{}" not found in DB.').format(uniq))
Example #17
0
def send_multiple(*multiple_output_sms):
    mutiple_output_sms_to_sent = [
        output_sms for output_sms in multiple_output_sms
        if output_sms.state == ATS_STATES.PROCESSING
    ]
    try:
        parsed_response = send_and_parse_response(*mutiple_output_sms_to_sent)
        for output_sms in mutiple_output_sms_to_sent:
            try:
                update_sms_state_from_response(output_sms, parsed_response)
                output_sms.save()
            except SMSSendingError:
                change_and_save(output_sms, state=ATS_STATES.LOCAL_TO_SEND)
    except SMSSendingError:
        bulk_change_and_save(mutiple_output_sms_to_sent,
                             ATS_STATES.LOCAL_TO_SEND)
Example #18
0
    def test_image_field_max_upload_size(self):
        # File is can be stored
        with open('data/test2.jpg', 'rb') as f:
            assert_false(self.inst.image)
            self.inst.image.save('test2.jpg', File(f))
            assert_true(self.inst.image)
            change_and_save(self.inst, image=None)

        # File is too large to store
        with open('data/test.jpg', 'rb') as f:
            assert_false(self.inst.image)
            assert_raises(PersistenceException, self.inst.image.save, 'image.jpeg', File(f))

        # File has a wrong extension
        with open('data/test2.jpg', 'rb') as f:
            assert_raises(PersistenceException, self.inst.image.save, 'image.html', File(f))
Example #19
0
    def test_subchoices_field(self):
        change_and_save(self.inst,
                        state_reason=TestFieldsModel.STATE_REASON.SUB_OK_2)
        assert_equal(self.inst.state_reason,
                     TestFieldsModel.STATE_REASON.SUB_OK_2)
        assert_raises(PersistenceException,
                      change_and_save,
                      self.inst,
                      state_reason=TestFieldsModel.STATE_REASON.SUB_NOT_OK_1)

        # Change state and substate should work
        change_and_save(self.inst,
                        state=TestFieldsModel.STATE.NOT_OK,
                        state_reason=TestFieldsModel.STATE_REASON.SUB_NOT_OK_2)
        assert_equal(self.inst.state, TestFieldsModel.STATE.NOT_OK)
        assert_equal(self.inst.state_reason,
                     TestFieldsModel.STATE_REASON.SUB_NOT_OK_2)
Example #20
0
    def test_image_field_max_upload_size(self):
        # File is can be stored
        with open('data/test2.jpg', 'rb') as f:
            assert_false(self.inst.image)
            self.inst.image.save('test2.jpg', File(f))
            assert_true(self.inst.image)
            change_and_save(self.inst, image=None)

        # File is too large to store
        with open('data/test.jpg', 'rb') as f:
            assert_false(self.inst.image)
            assert_raises(PersistenceException, self.inst.image.save,
                          'image.jpeg', File(f))

        # File has a wrong extension
        with open('data/test2.jpg', 'rb') as f:
            assert_raises(PersistenceException, self.inst.image.save,
                          'image.html', File(f))
Example #21
0
 def test_decimal_field(self):
     change_and_save(self.inst, decimal=3)
     assert_equal(self.inst.decimal, 3)
     assert_raises(PersistenceException,
                   change_and_save,
                   self.inst,
                   decimal='2.99')
     assert_raises(PersistenceException,
                   change_and_save,
                   self.inst,
                   decimal='10.00001')
     try:
         change_and_save(self.inst, decimal='11.1')
         assert_true(
             False,
             'Previous `change_and_save` suppose to raise an exception')
     except PersistenceException as ex:
         assert_true(
             'decimal: ' in str(ex),
             'Exception message was supposed to contain a field name.')
Example #22
0
    def test_sequence_choices_num_enum(self):
        # Only the first state is valid when field is null because it is the initial state
        assert_is_none(self.inst.state_graph)
        assert_raises(PersistenceException,
                      change_and_save,
                      self.inst,
                      state_graph=TestFieldsModel.GRAPH.SECOND)
        assert_raises(PersistenceException,
                      change_and_save,
                      self.inst,
                      state_graph=TestFieldsModel.GRAPH.THIRD)
        change_and_save(self.inst, state_graph=TestFieldsModel.GRAPH.FIRST)
        assert_equal(self.inst.state_graph, TestFieldsModel.GRAPH.FIRST)

        # Than one can switch only to second state
        assert_raises(PersistenceException,
                      change_and_save,
                      self.inst,
                      state_graph=TestFieldsModel.GRAPH.THIRD)
        change_and_save(self.inst, state_graph=TestFieldsModel.GRAPH.SECOND)
        assert_equal(self.inst.state_graph, TestFieldsModel.GRAPH.SECOND)

        # We cannot go back to first but we can go to the third state
        assert_raises(PersistenceException,
                      change_and_save,
                      self.inst,
                      state_graph=TestFieldsModel.GRAPH.FIRST)
        change_and_save(self.inst, state_graph=TestFieldsModel.GRAPH.THIRD)
        assert_equal(self.inst.state_graph, TestFieldsModel.GRAPH.THIRD)
Example #23
0
    def test_state_dispatcher(self):
        m = TestDispatchersModel.objects.create()

        # Moving TestDispatcher model to SECOND state should create new TestSmartModel instance
        assert_equal(TestSmartModel.objects.count(), 0)
        change_and_save(m, state=TestDispatchersModel.STATE.SECOND)
        assert_equal(TestSmartModel.objects.count(), 1)

        # But subsequent saves should not create more instances
        change_and_save(m, state=TestDispatchersModel.STATE.SECOND)
        assert_equal(TestSmartModel.objects.count(), 1)

        # Moving back and forth between the states creates another instance
        change_and_save(m, state=TestDispatchersModel.STATE.FIRST)
        change_and_save(m, state=TestDispatchersModel.STATE.SECOND)
        assert_equal(TestSmartModel.objects.count(), 2)
Example #24
0
    def test_state_dispatcher(self):
        m = TestDispatchersModel.objects.create()

        # Moving TestDispatcher model to SECOND state should create new TestSmartModel instance
        assert_equal(TestSmartModel.objects.count(), 0)
        change_and_save(m, state=TestDispatchersModel.STATE.SECOND)
        assert_equal(TestSmartModel.objects.count(), 1)

        # But subsequent saves should not create more instances
        change_and_save(m, state=TestDispatchersModel.STATE.SECOND)
        assert_equal(TestSmartModel.objects.count(), 1)

        # Moving back and forth between the states creates another instance
        change_and_save(m, state=TestDispatchersModel.STATE.FIRST)
        change_and_save(m, state=TestDispatchersModel.STATE.SECOND)
        assert_equal(TestSmartModel.objects.count(), 2)
Example #25
0
    def _update_from_logger(self, logger, is_last, **extra_data):
        version = self._get_version(logger, is_last)
        data = logger.to_dict()
        del data['related_objects']

        with transaction.atomic():
            instance = logger.backend_logs.sql_instance.__class__.objects.select_for_update().get(id=logger.id)
            if instance.version < version:
                instance = change_and_save(
                    logger.backend_logs.sql_instance,
                    version=version,
                    **data,
                    **extra_data,
                    update_only_changed_fields=True,
                )
                self._set_related_objects(instance, logger)
            logger.backend_logs.sql_instance = instance
            logger.backend_logs.last_sql_version = version
Example #26
0
    def test_sequence_choices_num_enum(self):
        # Only the first state is valid when field is null because it is the initial state
        assert_is_none(self.inst.state_graph)
        assert_raises(PersistenceException, change_and_save, self.inst, state_graph=TestFieldsModel.GRAPH.SECOND)
        assert_raises(PersistenceException, change_and_save, self.inst, state_graph=TestFieldsModel.GRAPH.THIRD)
        change_and_save(self.inst, state_graph=TestFieldsModel.GRAPH.FIRST)
        assert_equal(self.inst.state_graph, TestFieldsModel.GRAPH.FIRST)

        # Than one can switch only to second state
        assert_raises(PersistenceException, change_and_save, self.inst, state_graph=TestFieldsModel.GRAPH.THIRD)
        change_and_save(self.inst, state_graph=TestFieldsModel.GRAPH.SECOND)
        assert_equal(self.inst.state_graph, TestFieldsModel.GRAPH.SECOND)

        # We cannot go back to first but we can go to the third state
        assert_raises(PersistenceException, change_and_save, self.inst, state_graph=TestFieldsModel.GRAPH.FIRST)
        change_and_save(self.inst, state_graph=TestFieldsModel.GRAPH.THIRD)
        assert_equal(self.inst.state_graph, TestFieldsModel.GRAPH.THIRD)
Example #27
0
 def test_created_dispatcher(self):
     assert_equal(CSVRecord.objects.count(), 0)
     m = TestDispatchersModel.objects.create()
     assert_equal(CSVRecord.objects.count(), 1)
     change_and_save(m, state=TestDispatchersModel.STATE.SECOND)
     assert_equal(CSVRecord.objects.count(), 1)
Example #28
0
 def set_stale_celery_task_log_state(self):
     processsing_stale_tasks = CeleryTaskInvocationLog.objects.filter_processing(
         stale_at__lt=now()
     ).order_by('stale_at')
     for task in processsing_stale_tasks[:settings.SET_STALE_CELERY_INVOCATIONS_LIMIT_PER_RUN]:
         task_last_run = task.last_run
         if task_last_run and task_last_run.state == CeleryTaskRunLogState.SUCCEEDED:
             change_and_save(
                 task,
                 state=CeleryTaskInvocationLogState.SUCCEEDED,
                 stop=task_last_run.stop,
                 time=(task_last_run.stop - task.start).total_seconds(),
                 version=MAX_VERSION,
                 update_only_changed_fields=True,
             )
         elif task_last_run and task_last_run.state == CeleryTaskRunLogState.FAILED:
             change_and_save(
                 task,
                 state=CeleryTaskInvocationLogState.FAILED,
                 stop=task_last_run.stop,
                 time=(task_last_run.stop - task.start).total_seconds(),
                 version=MAX_VERSION,
                 update_only_changed_fields=True
             )
         else:
             try:
                 current_app.tasks[task.name].expire_invocation(
                     task.id,
                     task.task_args,
                     task.task_kwargs,
                     dict(
                         slug=task.slug,
                         parent_log=task.parent_log,
                         related_objects=[
                             (
                                 router.db_for_write(related_object.object_ct.model_class()),
                                 related_object.object_ct_id,
                                 related_object.object_id
                             ) for related_object in task.related_objects.all()
                         ],
                         start=task.start,
                         celery_task_id=task.celery_task_id,
                         stop=task.stop,
                         name=task.name,
                         queue_name=task.queue_name,
                         applied_at=task.applied_at,
                         triggered_at=task.triggered_at,
                         is_unique=task.is_unique,
                         is_async=task.is_async,
                         is_on_commit=task.is_on_commit,
                         input=task.input,
                         task_args=task.task_args,
                         task_kwargs=task.task_kwargs,
                         estimated_time_of_first_arrival=task.estimated_time_of_first_arrival,
                         expires_at=task.expires_at,
                         stale_at=task.stale_at,
                     )
                 )
             except NotRegistered:
                 change_and_save(
                     task,
                     state=CeleryTaskInvocationLogState.EXPIRED,
                     stop=now(),
                     version=MAX_VERSION,
                     update_only_changed_fields=True
                 )
Example #29
0
 def test_created_dispatcher(self):
     assert_equal(CSVRecord.objects.count(), 0)
     m = TestDispatchersModel.objects.create()
     assert_equal(CSVRecord.objects.count(), 1)
     change_and_save(m, state=TestDispatchersModel.STATE.SECOND)
     assert_equal(CSVRecord.objects.count(), 1)
Example #30
0
 def test_change_and_save(self):
     obj = ShortcutsModel.objects.create(name='test1', datetime=timezone.now(), number=1)
     change_and_save(obj, name='modified')
     assert_equal(obj.name, 'modified')
     assert_equal(ShortcutsModel.objects.first().name, 'modified')  # instance is changed and saved to DB