我们从Python开源项目中,提取了以下26个代码示例,用于说明如何使用django.contrib.admin.models.ADDITION。
def test_admin_log_includes_schema(self): Schema.objects.mass_create('a') schema = Schema.objects.get(name='a') schema.activate() aware = AwareModel.objects.create(name='foo') user = User.objects.create_user(username='test', password='test') LogEntry.objects.log_action( user_id=user.pk, content_type_id=ContentType.objects.get_for_model(aware).pk, object_id=aware.pk, object_repr=six.text_type(aware), change_message='test', action_flag=ADDITION, ) entry = LogEntry.objects.get() self.assertEqual('a', entry.object_schema.pk) self.assertEqual(2, len(entry.get_admin_url().split('?'))) self.assertEqual('__schema=a', entry.get_admin_url().split('?')[1])
def test_admin_log_naive_object_no_schema(self): Schema.objects.mass_create('a') schema = Schema.objects.get(name='a') schema.activate() naive = NaiveModel.objects.create(name='foo') user = User.objects.create_user(username='test', password='test') LogEntry.objects.log_action( user_id=user.pk, content_type_id=ContentType.objects.get_for_model(naive).pk, object_id=naive.pk, object_repr=six.text_type(naive), change_message='test', action_flag=ADDITION, ) entry = LogEntry.objects.get() self.assertEqual(None, entry.object_schema_id) self.assertEqual(1, len(entry.get_admin_url().split('?')))
def log_addition(self, request, object, message): """ Log that an object has been successfully added. The default implementation creates an admin LogEntry object. """ from django.contrib.admin.models import LogEntry, ADDITION LogEntry.objects.log_action( user_id=request.user.pk, content_type_id=get_content_type_for_model(object).pk, object_id=object.pk, object_repr=force_text(object), action_flag=ADDITION, change_message=message, )
def log_addition(self, request, object, message): """ Log that an object has been successfully added. The default implementation creates an admin LogEntry object. """ from django.contrib.admin.models import LogEntry, ADDITION return LogEntry.objects.log_action( user_id=request.user.pk, content_type_id=get_content_type_for_model(object).pk, object_id=object.pk, object_repr=force_text(object), action_flag=ADDITION, change_message=message, )
def log_action(obj, user_pk, message=None, action=ADDITION): LogEntry.objects.log_action( user_id=user_pk, content_type_id=ContentType.objects.get_for_model(obj).pk, object_id=obj.pk, object_repr=force_text(obj), action_flag=action, change_message=message )
def process_response(self, request, response): if not self.is_cms_request(request): return response from django.utils.cache import add_never_cache_headers if ((hasattr(request, 'toolbar') and request.toolbar.edit_mode) or not all(ph.cache_placeholder for ph in getattr(request, 'placeholders', ()))): add_never_cache_headers(response) if hasattr(request, 'user') and request.user.is_staff and response.status_code != 500: try: pk = LogEntry.objects.filter( user=request.user, action_flag__in=(ADDITION, CHANGE) ).only('pk').order_by('-pk')[0].pk if hasattr(request, 'cms_latest_entry') and request.cms_latest_entry != pk: log = LogEntry.objects.filter(user=request.user, action_flag__in=(ADDITION, CHANGE))[0] request.session['cms_log_latest'] = log.pk # If there were no LogEntries, just don't touch the session. # Note that in the case of a user logging-in as another user, # request may have a cms_latest_entry attribute, but there are no # LogEntries for request.user. except IndexError: pass return response
def process_request(self, request): """ If we should show the toolbar for this request, put it on request.toolbar. Then call the request_hook on the toolbar. """ if not self.is_cms_request(request): return edit_on = get_cms_setting('CMS_TOOLBAR_URL__EDIT_ON') edit_off = get_cms_setting('CMS_TOOLBAR_URL__EDIT_OFF') build = get_cms_setting('CMS_TOOLBAR_URL__BUILD') disable = get_cms_setting('CMS_TOOLBAR_URL__DISABLE') anonymous_on = get_cms_setting('TOOLBAR_ANONYMOUS_ON') if disable in request.GET: request.session['cms_toolbar_disabled'] = True if edit_on in request.GET: # If we actively enter edit mode, we should show the toolbar in any case request.session['cms_toolbar_disabled'] = False if not request.session.get('cms_toolbar_disabled', False) and ( request.user.is_staff or (anonymous_on and request.user.is_anonymous()) ): if edit_on in request.GET and not request.session.get('cms_edit', False): if not request.session.get('cms_edit', False): menu_pool.clear() request.session['cms_edit'] = True if request.session.get('cms_build', False): request.session['cms_build'] = False if edit_off in request.GET and request.session.get('cms_edit', True): if request.session.get('cms_edit', True): menu_pool.clear() request.session['cms_edit'] = False if request.session.get('cms_build', False): request.session['cms_build'] = False if build in request.GET and not request.session.get('cms_build', False): request.session['cms_build'] = True else: request.session['cms_build'] = False request.session['cms_edit'] = False if request.user.is_staff: try: request.cms_latest_entry = LogEntry.objects.filter( user=request.user, action_flag__in=(ADDITION, CHANGE) ).only('pk').order_by('-pk')[0].pk except IndexError: request.cms_latest_entry = -1 request.toolbar = CMSToolbar(request)