mirror of
https://github.com/DarrenOfficial/dpaste.git
synced 2024-11-23 11:56:36 +11:00
Removed MAX_SNIPPETS_PER_USER
No need to purge them from the session, they are purged from the db anyway.
This commit is contained in:
parent
f6b69b1209
commit
053808f2bf
4 changed files with 3 additions and 43 deletions
|
@ -18,6 +18,7 @@ EXPIRE_CHOICES = getattr(settings, 'DPASTE_EXPIRE_CHOICES', (
|
||||||
EXPIRE_DEFAULT = getattr(settings, 'DPASTE_EXPIRE_DEFAULT', 3600)
|
EXPIRE_DEFAULT = getattr(settings, 'DPASTE_EXPIRE_DEFAULT', 3600)
|
||||||
MAX_CONTENT_LENGTH = getattr(settings, 'DPASTE_MAX_CONTENT_LENGTH', 250*1024*1024)
|
MAX_CONTENT_LENGTH = getattr(settings, 'DPASTE_MAX_CONTENT_LENGTH', 250*1024*1024)
|
||||||
|
|
||||||
|
|
||||||
def get_expire_values(expires):
|
def get_expire_values(expires):
|
||||||
if expires == 'never':
|
if expires == 'never':
|
||||||
expire_type = Snippet.EXPIRE_KEEP
|
expire_type = Snippet.EXPIRE_KEEP
|
||||||
|
@ -104,8 +105,6 @@ class SnippetForm(forms.ModelForm):
|
||||||
return self.cleaned_data
|
return self.cleaned_data
|
||||||
|
|
||||||
def save(self, parent=None, *args, **kwargs):
|
def save(self, parent=None, *args, **kwargs):
|
||||||
MAX_SNIPPETS_PER_USER = getattr(settings, 'DPASTE_MAX_SNIPPETS_PER_USER', 100)
|
|
||||||
|
|
||||||
# Set parent snippet
|
# Set parent snippet
|
||||||
self.instance.parent = parent
|
self.instance.parent = parent
|
||||||
|
|
||||||
|
@ -118,12 +117,8 @@ class SnippetForm(forms.ModelForm):
|
||||||
super(SnippetForm, self).save(*args, **kwargs)
|
super(SnippetForm, self).save(*args, **kwargs)
|
||||||
|
|
||||||
# Add the snippet to the user session list
|
# Add the snippet to the user session list
|
||||||
if self.request.session.get('snippet_list', False):
|
self.request.session.setdefault('snippet_list', [])
|
||||||
if len(self.request.session['snippet_list']) >= MAX_SNIPPETS_PER_USER:
|
self.request.session['snippet_list'] += [self.instance.pk]
|
||||||
self.request.session['snippet_list'].pop(0)
|
|
||||||
self.request.session['snippet_list'] += [self.instance.pk]
|
|
||||||
else:
|
|
||||||
self.request.session['snippet_list'] = [self.instance.pk]
|
|
||||||
|
|
||||||
# Save the lexer in the session so we can use it later again
|
# Save the lexer in the session so we can use it later again
|
||||||
self.request.session['lexer'] = self.cleaned_data['lexer']
|
self.request.session['lexer'] = self.cleaned_data['lexer']
|
||||||
|
|
|
@ -136,10 +136,6 @@ DATABASES = {
|
||||||
# App specific settings
|
# App specific settings
|
||||||
#==============================================================================
|
#==============================================================================
|
||||||
|
|
||||||
# How many recent snippets to save for every user? IDs of this snippets are
|
|
||||||
# stored in the user session.
|
|
||||||
MAX_SNIPPETS_PER_USER = 25
|
|
||||||
|
|
||||||
SESSION_COOKIE_SECURE = True
|
SESSION_COOKIE_SECURE = True
|
||||||
CSRF_COOKIE_SECURE = True
|
CSRF_COOKIE_SECURE = True
|
||||||
|
|
||||||
|
|
|
@ -268,35 +268,6 @@ class SnippetTestCase(TestCase):
|
||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, 200)
|
||||||
self.assertEqual(Snippet.objects.count(), 0)
|
self.assertEqual(Snippet.objects.count(), 0)
|
||||||
|
|
||||||
@override_settings(DPASTE_MAX_SNIPPETS_PER_USER=2)
|
|
||||||
def test_snippet_that_exceed_history_limit_get_trashed(self):
|
|
||||||
"""
|
|
||||||
The maximum number of snippets a user can save in the session are
|
|
||||||
defined by `DPASTE_MAX_SNIPPETS_PER_USER`. Exceed that number will
|
|
||||||
remove the oldest snippet from the list.
|
|
||||||
"""
|
|
||||||
# Create three snippets but since the setting is 2 only the latest two
|
|
||||||
# will displayed on the history.
|
|
||||||
data = self.valid_form_data()
|
|
||||||
self.client.post(self.new_url, data, follow=True)
|
|
||||||
self.client.post(self.new_url, data, follow=True)
|
|
||||||
self.client.post(self.new_url, data, follow=True)
|
|
||||||
|
|
||||||
response = self.client.get(reverse('snippet_history'), follow=True)
|
|
||||||
one, two, three = Snippet.objects.order_by('published')
|
|
||||||
|
|
||||||
# Only the last two are saved in the session
|
|
||||||
self.assertEqual(len(self.client.session['snippet_list']), 2)
|
|
||||||
self.assertFalse(one.id in self.client.session['snippet_list'])
|
|
||||||
self.assertTrue(two.id in self.client.session['snippet_list'])
|
|
||||||
self.assertTrue(three.id in self.client.session['snippet_list'])
|
|
||||||
|
|
||||||
# And only the last two are displayed on the history page
|
|
||||||
self.assertNotContains(response, one.secret_id)
|
|
||||||
self.assertContains(response, two.secret_id)
|
|
||||||
self.assertContains(response, three.secret_id)
|
|
||||||
|
|
||||||
|
|
||||||
# -------------------------------------------------------------------------
|
# -------------------------------------------------------------------------
|
||||||
# Management Command
|
# Management Command
|
||||||
# -------------------------------------------------------------------------
|
# -------------------------------------------------------------------------
|
||||||
|
|
|
@ -166,11 +166,9 @@ class SnippetHistory(TemplateView):
|
||||||
url = '{0}#'.format(reverse('snippet_history'))
|
url = '{0}#'.format(reverse('snippet_history'))
|
||||||
return HttpResponseRedirect(url)
|
return HttpResponseRedirect(url)
|
||||||
|
|
||||||
|
|
||||||
def get_context_data(self, **kwargs):
|
def get_context_data(self, **kwargs):
|
||||||
ctx = super(SnippetHistory, self).get_context_data(**kwargs)
|
ctx = super(SnippetHistory, self).get_context_data(**kwargs)
|
||||||
ctx.update({
|
ctx.update({
|
||||||
'snippets_max': getattr(settings, 'DPASTE_MAX_SNIPPETS_PER_USER', 10),
|
|
||||||
'snippet_list': self.get_user_snippets(),
|
'snippet_list': self.get_user_snippets(),
|
||||||
})
|
})
|
||||||
return ctx
|
return ctx
|
||||||
|
|
Loading…
Reference in a new issue