Commits

Alexander Popelnukhin  committed 618b66b

Added SambaGroups views

  • Participants
  • Parent commits fca82dd

Comments (0)

Files changed (11)

File mailusers/forms.py

         for item in items:
             item = item.strip()
             if item:
-                validators.validate_email(item)
+#                validators.validate_email(item)
                 result.append(item)
         return result
 

File media/css/custom.css

     text-align: center;
 }
 
-
+td.centered  {
+    text-align: center;
+}
 
 code {
     color: #333;

File sambausers/forms.py

 from crispy_forms.layout import Layout, Submit, HTML, Field, Fieldset
 from crispy_forms.bootstrap import FormActions
 
+from mailusers.forms import ListField
+
 DEPARTMENTS = (
     (u'OSP', u'Отдел сопровождения проектов'),
     (u'ORGOUO', u'Отдел развития государственно-общественного управления образованием'),
         )
 
 
-class SambaGroupForm(forms.Form):
-    cn = forms.CharField(label=u'Название')
-    description = forms.CharField(label=u'Описание')
-    members = forms.CharField(label=u'Члены группы')
+class SambaGroupForm(forms.ModelForm):
+    cn = forms.CharField(label=u'Название', help_text=u'Название')
+    description = forms.CharField(label=u'Описание', help_text=u'Описание')
+    members = ListField(label=u'Члены группы', help_text=u'Каждый логин пользователя на новой строке')
 
     def __init__(self, *args, **kwargs):
 #        print kwargs.pop('group_gid')
             )
 
         )
-        #!EOF!
 
-  
+    class Meta:
+        model = SambaGroup
+        fields = (u'cn',
+                  u'description',
+                  u'members'
+            )
+        exclude = (u'dn', u'display_name',
+                   u'gid_number',
+                   u'samba_group_type',
+                   u'samba_sid',
+            )

File sambausers/models.py

             return True
         return False
 
+
+    @classmethod
+    def get_last_gid(cls):
+        return cls.objects.order_by('-gid_number')[0].gid_number
+
     def __unicode__(self):
         return '%s: %s' % (self.gid_number, self.description)
 

File sambausers/urls.py

        url(r'^$', 'index', name='sambausers_index'),
        url(r'^add/$', 'sambauser_add', name='sambauser_add'),
        url(r'^edit/(\w+)/$', 'sambauser_add', name='sambauser_edit'),
+       url(r'^groups/$', 'group_index', name='sambausers_groups'),
+       url(r'^add_group/$', 'group_add', name='sambausers_add_group'),
+       url(r'^edit_group/(\w+)/$', 'group_add', name='sambauser_edit_group'),
 
        #AJAX views
        url(r'^change_password/$', 'change_user_password', name='sambausers_change_password'),

File sambausers/views.py

 # coding=utf-8
 from annoying.decorators import render_to, ajax_request
-from sambausers.models import SambaGroup, SambaUser, create_or_change_user
+from sambausers.models import SambaGroup, SambaUser, create_or_change_user, get_domain_info
 from django.shortcuts import get_object_or_404
 from django.http import Http404
 
+
 from django.views.generic import ListView, FormView
 from django.utils import simplejson
 
 from sambausers.forms import SambaUserForm, SambaGroupForm
 
 
-class SambaUsersView(ListView):
-    template_name = 'sambausers/index.html'
-    model = SambaUser
-    context_object_name = 'users'
-
-    def get_queryset(self):
-        return SambaUser.objects.exclude(username__in=['root', 'nobody']).all()
-
-
-class AddSambaGroupView(FormView):
-    model = SambaGroup
-    form_class = SambaGroupForm
-    template_name = u'sambausers/group_add.html'
-
-
-    def get_form_kwargs(self):
-        kwargs = super(AddSambaGroupView, self).get_form_kwargs()
-        kwargs[u'group_gid'] = self.kwargs.get(u'group_gid', 0)
-        return kwargs
 
+@render_to('sambausers/group_add.html')
+def group_add(request, group_gid=None):
+    group = get_object_or_404(SambaGroup, gid_number=group_gid) if group_gid else None
+    title = u'Редактирование группы'
+    if request.method == 'POST':
+        form = SambaGroupForm(request.POST, instance=group)
+        if form.is_valid():
+            group = form.save(commit=False)
+            if not group_gid:
+                new_gid_number = SambaGroup.get_last_gid() + 1
+                _, domain_sid, _, _ = get_domain_info()
+                group.display_name = group.cn
+                group.samba_group_type = 2
+                group.gid_number = new_gid_number
+                group.samba_sid = u'%s-%i' % (domain_sid, new_gid_number)
+                group.save()
+            else:
+                group.display_name = group.cn
+                group.save()
+            title = u'Группа обновлена' if group_gid else u'Группа добавлена'
+            return dict(group=group, title=title, TEMPLATE='sambausers/group_edit_complete.html')
+    else:
+        form = SambaGroupForm(instance=group)
 
-    def get_context_data(self, **kwargs):
-        context = super(AddSambaGroupView, self).get_context_data(**kwargs)
-        context['title'] = u'Добавить новую группу'
-        return context
-
+    return dict(form=form, title=title)
 
-class SambaGroupsViews(ListView):
-    template_name = 'sambausers/index_group.html'
-    model = SambaGroup
-    context_object_name = 'groups'
 
-    def get_queryset(self):
-        return SambaGroup.objects.exclude(gid_number__in=[513, 515]).all()
 
+@render_to('sambausers/group_index.html')
+def group_index(request):
+    groups = SambaGroup.objects.exclude(gid_number__in=[513, 515]).all()
+    title = u'Группы домена'
+    return dict(groups=groups, title=title)
 
 @render_to('sambausers/index.html')
 def index(request):
         if username:
             user = SambaUser.objects.get(username=username)
             message = u'Пользователь <strong>%s</strong> был удален' % username
-#            user.delete()
+            user.delete()
             return {'message': message}
     raise Http404
 

File templates/_base.html

                     {% block menu %}
                     <li><a href="{% url mailusers_index %}">Пользователи почты</a></li>
                     <li><a href="{% url sambausers_index %}">Пользователи домена</a></li>
-                    <li><a href="#">Группы домена</a></li>
+                    <li><a href="{% url sambausers_groups %}">Группы домена</a></li>
                     {% endblock %}
                 </ul>
             </div>

File templates/sambausers/group_add.html

+{% extends '_base.html' %}
+{% block title %}
+    {{ title }}
+{% endblock %}
+
+{% load crispy_forms_tags %}
+{% block content %}
+    <div class="row form">
+        <div class="span8 offset2">
+            {% crispy form %}
+        </div>
+    </div>
+{% endblock %}

File templates/sambausers/group_edit_complete.html

+{% extends '_base.html' %}
+{% block title %}{{ title }}{% endblock %}
+{% block content %}
+    <script type="text/javascript">
+        $(function () {
+            setTimeout(function () {
+                //noinspection JSCheckFunctionSignatures
+                window.location.replace('{% url sambausers_groups %}');
+            }, 5000)
+        })
+    </script>
+    <div class="row edit-complete">
+        <div class="span12">
+            <div class="hero-unit">
+                <h1>{{ title }}</h1>
+
+                <p>{{ group.dn }}</p>
+
+                <p>
+                    <a href="{% url sambausers_groups %}" class="btn btn-primary btn-large">OK</a>
+                </p>
+            </div>
+        </div>
+    </div>
+
+{% endblock %}

File templates/sambausers/group_index.html

+{% extends '_base.html' %}
+{% block title %}{{ title }}{% endblock %}
+
+{% block js %}
+    <script type="text/javascript">
+        $(function () {
+            $("#quickfind").keyup(function (event) {
+                if (event.keyCode === 27) {
+                    this.value = '';
+                }
+                $.uiTableFilter($('#groups_table'), this.value);
+            });
+        })
+    </script>
+{% endblock %}
+
+{% block content %}
+
+    <div class="row">
+        <div class="span12">
+            <div class="page-header">
+                <h1>{{ title }}:</h1>
+            </div>
+        </div>
+    </div>
+    <div class="row">
+        <div class="span12">
+            <div class="actions">
+                <p class="modify">
+                    <a class="btn btn-info" href="{% url sambausers_add_group %}">Добавить новую</a>
+                </p>
+            </div>
+        </div>
+    </div>
+    <div class="row">
+        <div id="alert-area" class="span12"></div>
+    </div>
+    <div class="row">
+        <div class="span12">
+            <label class="quickfind control-label" for="quickfind"></label>
+            <input class="span7" type="text" placeholder="Фильтр" id="quickfind"/>
+
+            <div id="sambausers_table">
+                <table class="table table-striped" id="groups_table">
+                    <thead>
+                    <tr>
+                        <th class="centered">Номер группы</th>
+                        <th>Название</th>
+                        <th>Описание</th>
+                        <th class="centered">Пользователей в группе</th>
+                    </tr>
+                    </thead>
+
+                    <tbody>
+                    {% for group in groups %}
+                        <tr>
+                            <td class="centered">{{ group.gid_number }}</td>
+                            <td><a href="{% url sambauser_edit_group group.gid_number %}"><strong>{{ group.cn }}</strong></a></td>
+                            <td>{{ group.description }}</td>
+                            <td class="centered">{{ group.members|length }}</td>
+                        </tr>
+                    {% endfor %}
+                    </tbody>
+                </table>
+            </div>
+        </div>
+    </div>
+{% endblock %}

File templates/sambausers/index.html

                 event.preventDefault();
                 var row = $(this).parents('tr');
                 var username = row.find('td.login a').html();
-                sambausersModal.find('.modal-header h3').html('Отключение пользователя:');
-                sambausersModal.find('.modal-body p').html('Вы действительно хотите отключить ' +
+                sambausersModal.find('.modal-header h3').html('Смена статуса пользователя:');
+                sambausersModal.find('.modal-body p').html('Вы действительно хотите сменить статус ' +
                                                                    'пользователя <code class="username">' + username + '</code>?');
                 sambausersModal.modal('show');
                 sambausersModal.find('a.btn-primary').html('Отключить').unbind('click').click(function () {