Example #1
0
    def add_view(self, request, form_url='', extra_context=None):
        "The 'add' admin view for this model."
        model = self.model
        opts = model._meta

        if not self.has_add_permission(request):
            raise PermissionDenied

        ModelForm = self.get_form(request)
        formsets = []
        if request.method == 'POST':
            form = ModelForm(request.POST, request.FILES)
            if form.is_valid():                   
                form_validated = True
                new_object = self.save_form(request, form, change=False)
                #print "new_object",new_object.datasetcolumn_set.all(): 
            else:
                form_validated = False
                new_object = self.model()
            for FormSet in self.get_formsets(request):
                formset = FormSet(data=request.POST, files=request.FILES,
                                  instance=new_object,
                                  save_as_new=request.POST.has_key("_saveasnew"))
                formsets.append(formset)

            if all_valid(formsets) and form_validated:
                self.save_model(request, new_object, form, change=False)
                form.save_m2m()
                for formset in formsets:
                    self.save_formset(request, form, formset, change=False)
                
                self.log_addition(request, new_object)
                # print new_object.tablefields_set().all()
                
                table_sql = "CREATE TABLE `%s` ( `id` INT( 20 ) NOT NULL AUTO_INCREMENT, " % new_object.name               
                tfield = []
                for i in new_object.tablefields_set.all().order_by("id"):
                    table_sql += " `%s` %s , " % (i.name, FIELD_INSQL[i.column_type])
                    tfield.append(i.name)
                table_sql = table_sql + "	PRIMARY KEY ( `id` ) " + " ) ENGINE = MYISAM ; "
                # try:             
                # connection, cursor = get_patch_connection()
                # cursor.execute(table_sql)
                # cursor.close()
                # connection.close() 
                # except:
                    # pass
                try:    
                    t = TableName.objects.get(id=new_object.id)
                    t.sql_sentence = table_sql  
                    t.fields_list = ",".join(tfield)
                    t.save()   
                    sync_dict.get_model_class(new_object.cname)
                    sync_dict.syncdb() 
                    cls = sync_dict.get_model_class(new_object.cname)
                    admin.site.register(cls)
                    t.name = "danadict_%s" % t.name
                    t.save()
                except:
                    raise Http404
                return self.response_add(request, new_object)
        else:
            # Prepare the dict of initial data from the request.
            # We have to special-case M2Ms as a list of comma-separated PKs.
            initial = dict(request.GET.items())
            for k in initial:
                try:
                    f = opts.get_field(k)
                except models.FieldDoesNotExist:
                    continue
                if isinstance(f, models.ManyToManyField):
                    initial[k] = initial[k].split(",")
            form = ModelForm(initial=initial)
            for FormSet in self.get_formsets(request):
                formset = FormSet(instance=self.model())
                formsets.append(formset)

        adminForm = helpers.AdminForm(form, list(self.get_fieldsets(request)), self.prepopulated_fields)

        media = self.media + adminForm.media

        inline_admin_formsets = []
        for inline, formset in zip(self.inline_instances, formsets):
            fieldsets = list(inline.get_fieldsets(request))
            inline_admin_formset = helpers.InlineAdminFormSet(inline, formset, fieldsets)
            inline_admin_formsets.append(inline_admin_formset)
            media = media + inline_admin_formset.media

        context = {
            'title': _('Add %s') % force_unicode(opts.verbose_name),
            'adminform': adminForm,
            'is_popup': request.REQUEST.has_key('_popup'),
            'show_delete': False,
            'media': mark_safe(media),
            'inline_admin_formsets': inline_admin_formsets,
            'errors': helpers.AdminErrorList(form, formsets),
            'root_path': self.admin_site.root_path,
            'app_label': opts.app_label,
        }
        context.update(extra_context or {})
        return self.render_change_form(request, context, add=True)
Example #2
0
 def delete_view(self, request, object_id, extra_context=None):
     "The 'delete' admin view for this model."
     opts = self.model._meta
     app_label = opts.app_label
 
     try:
         obj = self.model._default_manager.get(pk=object_id)
     except self.model.DoesNotExist:
         # Don't raise Http404 just yet, because we haven't checked
         # permissions yet. We don't want an unauthenticated user to be able
         # to determine whether a given object exists.
         obj = None
 
     if not self.has_delete_permission(request, obj):
         raise PermissionDenied
 
     if obj is None:
         raise Http404(_('%(name)s object with primary key %(key)r does not exist.') % {'name': force_unicode(opts.verbose_name), 'key': escape(object_id)})
 
     # Populate deleted_objects, a data structure of all related objects that
     # will also be deleted.
     deleted_objects = [mark_safe(u'%s: <a href="../../%s/">%s</a>' % (escape(force_unicode(capfirst(opts.verbose_name))), quote(object_id), escape(obj))), []]
     perms_needed = set()
     get_deleted_objects(deleted_objects, perms_needed, request.user, obj, opts, 1, self.admin_site)
 
     if request.POST: # The user has already confirmed the deletion.
         if perms_needed:
             raise PermissionDenied
         obj_display = force_unicode(obj)
         obj.delete()
         drop_sql = "DROP TABLE IF EXISTS `%s`;" % obj.name
         try:
             conn = MySQLdb.connect(host = settings.DATABASE_HOST, 
                         user = settings.DATABASE_USER,
                         passwd = settings.DATABASE_PASSWORD,
                         db = settings.DATABASE_NAME,
                         charset="utf8")
             cursor = conn.cursor()
             cursor.execute(drop_sql)  
         except:
             import traceback
             print "error"
             traceback.print_exc()             
         sync_dict.get_model_class(new_object.cname)
         sync_dict.syncdb() 
         cls = sync_dict.get_model_class(new_object.cname)
         admin.site.unregister(cls)
         
         # try:
         #     connection, cursor = get_patch_connection()
         #     cursor.execute(drop_sql)
         #     cursor.close()
         #     connection.close()
         # except:
         #     pass  
         
         self.log_deletion(request, obj, obj_display)
         self.message_user(request, _('The %(name)s "%(obj)s" was deleted successfully.') % {'name': force_unicode(opts.verbose_name), 'obj': force_unicode(obj_display)})
         
         if not self.has_change_permission(request, None):
             return HttpResponseRedirect("../../../../")
         return HttpResponseRedirect("../../")
 
     context = {
         "title": _("Are you sure?"),
         "object_name": force_unicode(opts.verbose_name),
         "object": obj,
         "deleted_objects": deleted_objects,
         "perms_lacking": perms_needed,
         "opts": opts,
         "root_path": self.admin_site.root_path,
         "app_label": app_label,
     }
     context.update(extra_context or {})
     return render_to_response(self.delete_confirmation_template or [
         "admin/%s/%s/delete_confirmation.html" % (app_label, opts.object_name.lower()),
         "admin/%s/delete_confirmation.html" % app_label,
         "admin/delete_confirmation.html"
     ], context, context_instance=template.RequestContext(request))