def test_suggestion_tag_reverse(self): # Create a suggestion and tags but with a missing reverse reference. Reminder(key_name='a-b', title='a b', tags='a b'.split()).put() Reminder(key_name='b-c', title='b c', tags='b'.split()).put() Tag(key_name='a', suggestions='a-b'.split(), count=0).put() Tag(key_name='b', suggestions='b-c'.split(), count=0).put() self.assertEqual(Reminder.all().count(), 2) self.assertEqual(Tag.all().count(), 2) self.assertEqual(len(Tag.get_by_key_name('b').suggestions), 1) # Check that the missing tag-reminder reference is detected. response = self.client.get('/consistency/') self.assertTrue('suggestion_tag_reverse' in response.context['problems']) self.assertTrue("Suggestion a-b references b but not reverse." in response.content) # Simulate button click to fix this problem. response = self.client.post('/consistency/', { 'suggestion_tag_reverse': "Create reverse references"}) self.assertRedirects(response, '/consistency/') # Check that the tags are now existing. self.assertEqual(Reminder.all().count(), 2) self.assertEqual(Tag.all().count(), 2) self.assertEqual(len(Tag.get_by_key_name('b').suggestions), 2) response = self.client.get('/consistency/') self.assertFalse('suggestion_tag_reverse' in response.context['problems'])
def dump_app(request, app_name, format): if app_name == 'reminders': reminder_list = Reminder.all() elif app_name == 'tags': tag_list = Tag.all() template = 'dumpdata/%s.%s' % (app_name, format) return render_to_response(request, template, locals())
def index(request): """ Display the current system status. """ # Simple form to add new suggestions. suggestion_form = SuggestionForm(request.POST or None) if suggestion_form.is_valid(): return submit_suggestion(request, suggestion_form) # Recent time intervals. day = datetime.now() - timedelta(hours=24) week = datetime.now() - timedelta(days=7) # Show newest suggestions. suggestion_count = Reminder.all().filter('owner', None).count() suggestion_count_24h = (Reminder.all().filter('owner', None) .filter('created >', day).count()) suggestion_count_7d = (Reminder.all().filter('owner', None) .filter('created >', week).count()) suggestion_list = (Reminder.all().filter('owner', None) .order('-created').fetch(RECENT_LIMIT)) # Show newest tags. tag_count = Tag.all().count() tag_count_24h = Tag.all().filter('created >', day).count() tag_count_7d = Tag.all().filter('created >', week).count() tag_list = Tag.all().order('-created').fetch(RECENT_LIMIT * 4) # Registered user accounts. user_count = User.all().count() user_count_24h = User.all().filter('date_joined >', day).count() user_count_7d = User.all().filter('date_joined >', week).count() user_list = User.all().order('-date_joined').fetch(RECENT_LIMIT) # Show newest feedback. # feedback_count = Feedback.all().count() # feedback_count_24h = Feedback.all().filter('submitted >', day).count() # feedback_count_7d = Feedback.all().filter('submitted >', week).count() # feedback_list = Feedback.all().order('-submitted').fetch(RECENT_LIMIT) return render_to_response(request, 'dashboard/index.html', locals())
from django.conf.urls.defaults import * from django.views.generic.list_detail import object_list, object_detail from reminders.models import Reminder info_dict = { 'queryset': Reminder.all().filter('owner', None), 'template_object_name': 'suggestion', } urlpatterns = patterns('suggestions.views', url(r'^$', object_list, dict(info_dict, template_name='suggestions/index.html'), name='suggestion_list'), url(r'^(?P<key_name>[a-z0-9-]+)/$', 'detail'), )
def index(request): """ List all reminders for a registered user. """ reminder_list = Reminder.all().filter('owner', request.user) return render_to_response(request, 'reminders/index.html', locals())
def index(request): """ Check reminders and tags for consistency. """ if (request.META.get('HTTP_X_APPENGINE_CRON', '') != 'true' and not request.user.is_staff): return HttpResponseRedirect('/accounts/login/?next=/consistency/') # Get all tags and suggestions from the datastore. tag_dict = dict((tag.key().name(), tag) for tag in Tag.all()) suggestion_dict = dict((suggestion.key().name(), suggestion) for suggestion in Reminder.all().filter('owner', None)) # Initialize empty problems dict. problems = dict((problem, []) for problem in PROBLEM_MESSAGES) # Check all tags. for tag_key, tag in tag_dict.items(): if tag.count != len(tag.suggestions): problems['tag_count'].append( (tag, tag.count, len(tag.suggestions))) elif tag.count == 0: problems['tag_empty'].append((tag, )) oldest = None for suggestion_key in tag.suggestions: if tag.suggestions.count(suggestion_key) > 1: problems['tag_suggestion_duplicate'].append( (tag, tag.suggestions.count(suggestion_key), suggestion_key)) if suggestion_key not in suggestion_dict: problems['tag_suggestion_missing'].append( (tag, suggestion_key)) continue suggestion = suggestion_dict[suggestion_key] if tag.key().name() not in suggestion.tags: problems['tag_suggestion_reverse'].append((tag, suggestion)) if oldest is None or suggestion.created < oldest.created: oldest = suggestion if oldest: if tag.created is None: problems['tag_created_none'].append((tag, oldest)) elif tag.created > oldest.created: problems['tag_created_later'].append((tag, oldest)) # Check all suggestions. for suggestion_key, suggestion in suggestion_dict.items(): for tag_key in suggestion.tags: if tag_key not in tag_dict: problems['suggestion_tag_missing'].append( (suggestion, tag_key)) continue tag = tag_dict[tag_key] if suggestion.key().name() not in tag.suggestions: problems['suggestion_tag_reverse'].append((suggestion, tag)) # Check all feedback submitters. for feedback in Feedback.all().filter('submitter !=', None): try: submitter = feedback.submitter # Attempt to dereference. except datastore_errors.Error: problems['feedback_submitter'].append((feedback, request.user)) # Check all reminders. for reminder in Reminder.all().filter('owner !=', None): try: owner = reminder.owner # Attempt to dereference. except datastore_errors.Error: problems['reminder_owner'].append((reminder, request.user)) # Remove empty problem sections. for problem in PROBLEM_MESSAGES: if not problems[problem]: assert problems.pop(problem) == [] # Return plain-text summary if cron is calling, development test with: # curl --header "X-AppEngine-Cron: true" http://localhost:8000/consistency/ if request.META.get('HTTP_X_APPENGINE_CRON', '') == 'true': message = [] for problem in problems: message.append(PROBLEM_HEADLINES[problem].rstrip('.') + ':') for data in problems[problem]: message.append("* " + format_problem(problem, data)) message.append('') if not message: message.append("No problems found.") message.append('') message.append('http://www.minderbot.com/consistency/') message.append('') message = '\n'.join(message) if problems: if request.META.get('HTTP_USER_AGENT', '') != 'django.test.Client': logging.error(message) mail_admins('Consistency check found problems', message, fail_silently=True) return HttpResponse(message, mimetype="text/plain") # Fix inconsistencies if admin clicked one of the buttons. if request.user.is_staff: for problem in problems: if problems[problem] and problem in request.POST: func = getattr(repair, problem) assert callable(func) for item in problems[problem]: func(*item) return HttpResponseRedirect(request.path) # Collect errors and remove sections without problems. consistency_results = [] for problem in problems: consistency_results.append( (problem, PROBLEM_HEADLINES[problem], [format_problem(problem, item) for item in problems[problem]], PROBLEM_BUTTONS[problem])) consistency_results.sort() return render_to_response(request, 'consistency/index.html', locals())