##// END OF EJS Templates
Removed old users autoremoval
neko259 -
r657:cb9d10de default
parent child Browse files
Show More
@@ -1,126 +1,127 b''
1 from datetime import datetime, timedelta
1 from datetime import datetime, timedelta
2 import hashlib
2 import hashlib
3 from django.db import transaction
3 from django.db import transaction
4 from django.db.models import Count
4 from django.db.models import Count
5 from django.template import RequestContext
5 from django.template import RequestContext
6 from django.utils import timezone
6 from django.utils import timezone
7 from django.views.generic import View
7 from django.views.generic import View
8 from boards import utils
8 from boards import utils
9 from boards.models import User, Post
9 from boards.models import User, Post
10 from boards.models.post import SETTING_MODERATE
10 from boards.models.post import SETTING_MODERATE
11 from boards.models.user import RANK_USER, Ban
11 from boards.models.user import RANK_USER, Ban
12 import neboard
12 import neboard
13
13
14 BAN_REASON_SPAM = 'Autoban: spam bot'
14 BAN_REASON_SPAM = 'Autoban: spam bot'
15
15
16 OLD_USER_AGE_DAYS = 90
16 OLD_USER_AGE_DAYS = 90
17
17
18 PARAMETER_FORM = 'form'
18 PARAMETER_FORM = 'form'
19
19
20
20
21 class BaseBoardView(View):
21 class BaseBoardView(View):
22
22
23 def get_context_data(self, **kwargs):
23 def get_context_data(self, **kwargs):
24 request = kwargs['request']
24 request = kwargs['request']
25 context = self._default_context(request)
25 context = self._default_context(request)
26
26
27 context['version'] = neboard.settings.VERSION
27 context['version'] = neboard.settings.VERSION
28 context['site_name'] = neboard.settings.SITE_NAME
28 context['site_name'] = neboard.settings.SITE_NAME
29
29
30 return context
30 return context
31
31
32 def _default_context(self, request):
32 def _default_context(self, request):
33 """Create context with default values that are used in most views"""
33 """Create context with default values that are used in most views"""
34
34
35 context = RequestContext(request)
35 context = RequestContext(request)
36
36
37 user = self._get_user(request)
37 user = self._get_user(request)
38 context['user'] = user
38 context['user'] = user
39 context['tags'] = user.fav_tags.all()
39 context['tags'] = user.fav_tags.all()
40 context['posts_per_day'] = float(Post.objects.get_posts_per_day())
40 context['posts_per_day'] = float(Post.objects.get_posts_per_day())
41
41
42 theme = self._get_theme(request, user)
42 theme = self._get_theme(request, user)
43 context['theme'] = theme
43 context['theme'] = theme
44 context['theme_css'] = 'css/' + theme + '/base_page.css'
44 context['theme_css'] = 'css/' + theme + '/base_page.css'
45
45
46 # This shows the moderator panel
46 # This shows the moderator panel
47 moderate = user.get_setting(SETTING_MODERATE)
47 moderate = user.get_setting(SETTING_MODERATE)
48 if moderate == 'True':
48 if moderate == 'True':
49 context['moderator'] = user.is_moderator()
49 context['moderator'] = user.is_moderator()
50 else:
50 else:
51 context['moderator'] = False
51 context['moderator'] = False
52
52
53 return context
53 return context
54
54
55 def _get_user(self, request):
55 def _get_user(self, request):
56 """
56 """
57 Get current user from the session. If the user does not exist, create
57 Get current user from the session. If the user does not exist, create
58 a new one.
58 a new one.
59 """
59 """
60
60
61 session = request.session
61 session = request.session
62 if not 'user_id' in session:
62 if not 'user_id' in session:
63 request.session.save()
63 request.session.save()
64
64
65 md5 = hashlib.md5()
65 md5 = hashlib.md5()
66 md5.update(session.session_key)
66 md5.update(session.session_key)
67 new_id = md5.hexdigest()
67 new_id = md5.hexdigest()
68
68
69 while User.objects.filter(user_id=new_id).exists():
69 while User.objects.filter(user_id=new_id).exists():
70 md5.update(str(timezone.now()))
70 md5.update(str(timezone.now()))
71 new_id = md5.hexdigest()
71 new_id = md5.hexdigest()
72
72
73 time_now = timezone.now()
73 time_now = timezone.now()
74 user = User.objects.create(user_id=new_id, rank=RANK_USER,
74 user = User.objects.create(user_id=new_id, rank=RANK_USER,
75 registration_time=time_now)
75 registration_time=time_now)
76
76
77 self._delete_old_users()
77 # TODO Move this to manage.py commands
78 #self._delete_old_users()
78
79
79 session['user_id'] = user.id
80 session['user_id'] = user.id
80 else:
81 else:
81 user = User.objects.select_related('fav_tags').get(
82 user = User.objects.select_related('fav_tags').get(
82 id=session['user_id'])
83 id=session['user_id'])
83
84
84 return user
85 return user
85
86
86 def _get_theme(self, request, user=None):
87 def _get_theme(self, request, user=None):
87 """
88 """
88 Get user's CSS theme
89 Get user's CSS theme
89 """
90 """
90
91
91 if not user:
92 if not user:
92 user = self._get_user(request)
93 user = self._get_user(request)
93 theme = user.get_setting('theme')
94 theme = user.get_setting('theme')
94 if not theme:
95 if not theme:
95 theme = neboard.settings.DEFAULT_THEME
96 theme = neboard.settings.DEFAULT_THEME
96
97
97 return theme
98 return theme
98
99
99 def _delete_old_users(self):
100 def _delete_old_users(self):
100 """
101 """
101 Delete users with no favorite tags and posted messages. These can be spam
102 Delete users with no favorite tags and posted messages. These can be spam
102 bots or just old user accounts
103 bots or just old user accounts
103 """
104 """
104
105
105 old_registration_date = datetime.now().date() - timedelta(
106 old_registration_date = datetime.now().date() - timedelta(
106 OLD_USER_AGE_DAYS)
107 OLD_USER_AGE_DAYS)
107
108
108 for user in User.objects.annotate(tags_count=Count('fav_tags')).filter(
109 for user in User.objects.annotate(tags_count=Count('fav_tags')).filter(
109 tags_count=0).filter(
110 tags_count=0).filter(
110 registration_time__lt=old_registration_date):
111 registration_time__lt=old_registration_date):
111 if not Post.objects.filter(user=user).exists():
112 if not Post.objects.filter(user=user).exists():
112 user.delete()
113 user.delete()
113
114
114 @transaction.atomic
115 @transaction.atomic
115 def _ban_current_user(self, request):
116 def _ban_current_user(self, request):
116 """
117 """
117 Add current user to the IP ban list
118 Add current user to the IP ban list
118 """
119 """
119
120
120 ip = utils.get_client_ip(request)
121 ip = utils.get_client_ip(request)
121 ban, created = Ban.objects.get_or_create(ip=ip)
122 ban, created = Ban.objects.get_or_create(ip=ip)
122 if created:
123 if created:
123 ban.can_read = False
124 ban.can_read = False
124 ban.reason = BAN_REASON_SPAM
125 ban.reason = BAN_REASON_SPAM
125 ban.save()
126 ban.save()
126
127
General Comments 0
You need to be logged in to leave comments. Login now