Procházet zdrojové kódy

Using Django Datetime Widget

Marcos Dumay de Medeiros před 8 roky
rodič
revize
774ddf0d17

+ 14 - 0
src/rapid/rapidforms.py

@@ -8,10 +8,13 @@ from __future__ import unicode_literals
 import collections
 
 from django import forms
+from django.db import models
+from django.conf import settings
 from django.contrib.contenttypes.models import ContentType
 from django.core.exceptions import ValidationError
 from django.db import transaction
 from django.utils.encoding import force_text
+from datetimewidget import widgets as dtwidgets
 
 from rapid.widgets import RapidSelector, RapidRelationReadOnly, rapid_alternatives_widget, rapid_dependent_widget
 from rapid.wrappers import FieldData, ModelData
@@ -20,6 +23,8 @@ import gettext
 _ = gettext.gettext
 
 
+
+
 class RapidAlternativesField(forms.Field):
     """
     Form field for AlternativeData.
@@ -114,6 +119,8 @@ class RapidDependentField(forms.Field):
 
 
 def for_model(request, model, default_relations=()):
+    hasDtWidgets = 'datetimewidget' in settings.INSTALLED_APPS
+
     default_relations = list(default_relations)
     default_relations_request = request.GET.get('default')
     widgets = []
@@ -130,6 +137,13 @@ def for_model(request, model, default_relations=()):
         if f.is_relation() and force_text(f.bare_name()) not in default_relations_fields:
             if f.related_model().has_permission(request, 'select'):
                 widgets.append((f.bare_name(), RapidSelector(f)))
+        if type(f.field) == models.DateField:
+            widgets.append((f.bare_name(), dtwidgets.DateWidget(usel10n = True, bootstrap_version=3)))
+        if type(f.field) == models.DateTimeField:
+            widgets.append((f.bare_name(), dtwidgets.DateTimeWidget(usel10n = True, bootstrap_version=3)))
+        if type(f.field) == models.TimeField:
+            widgets.append((f.bare_name(), dtwidgets.TimeWidget(usel10n = True, bootstrap_version=3)))
+
     # ModelForm.Meta has attributes with the same names, thus I'll rename them
     form_model = model
     form_widgets = dict(widgets)

+ 1 - 0
src/rapid/templates/rapid/bare/create.html

@@ -1,3 +1,4 @@
+{{ form.media }}
 <h2>{{object_data.model_name.title}}</h2>
 
 <form method="POST" action="" enctype="multipart/form-data">

+ 1 - 0
src/rapid/templates/rapid/bare/update.html

@@ -1,3 +1,4 @@
+{{ form.media }}
 <h2>{{object_data.model_name.title}}</h2>
 
 <form method="POST" action=""  enctype="multipart/form-data">

+ 2 - 0
testproject/testproject/settings.py

@@ -40,6 +40,7 @@ INSTALLED_APPS = (
     'testproject',
     'rapid',
     'django_migration_fixture',
+    'datetimewidget',
 )
 
 MIDDLEWARE_CLASSES = (
@@ -51,6 +52,7 @@ MIDDLEWARE_CLASSES = (
     'django.contrib.messages.middleware.MessageMiddleware',
     'django.middleware.clickjacking.XFrameOptionsMiddleware',
     'django.middleware.security.SecurityMiddleware',
+    'django.middleware.locale.LocaleMiddleware',
 )
 
 ROOT_URLCONF = 'testproject.urls'

+ 2 - 1
testproject/testproject/templates/base.html

@@ -5,11 +5,12 @@
     <meta charset="utf-8">
     <meta name="viewport" content="width=device-width, initial-scale=1">
     <title>{% block title %}Rapid Test Site{% endblock %}</title>
+    <script src="{% static 'js/jquery.js' %}"></script>
     <link href="{% static 'css/bootstrap.css' %}" rel="stylesheet">
+    <script src="{% static 'js/bootstrap.js' %}"></script>
     <link href="{% static 'css/bootstrap-theme.css' %}" rel="stylesheet">
     <link href="{% static 'css/font-awesome.css' %}" rel="stylesheet">
     <link href="{% static 'css/base.css' %}" rel="stylesheet">
-    <script src="{% static 'js/jquery.js' %}"></script>
     <style>
         div#head{
             margin-top: 2em;