rauhryan
3/9/2011 - 2:01 AM

NDjangoAttribute.cs

using System;
using System.IO;
using NDjango.Interfaces;

namespace NDjango
{
    public class TemplateLoader : ITemplateLoader
    {
        private readonly TextReader _reader;

        public TemplateLoader(TextReader reader)
        {
            _reader = reader;
        }

        public TextReader GetTemplate(string path)
        {
            return _reader;
        }

        public bool IsUpdated(string path, DateTime timestamp)
        {
            return true;
        }
    }
}
using System.Collections.Generic;
using System.Linq;
using FubuCore.Reflection;
using FubuMVC.Core.Registration;
using FubuMVC.Core.Registration.Nodes;

namespace NDjango
{
    public class NDjangoPolicy : IConfigurationAction
    {
        public void Configure(BehaviorGraph graph)
        {
            graph.Behaviors.Where(x => x.FirstCall().Method.HasAttribute<NDjangoAttribute>())
                .Each(x =>
                          {
                              ActionCall actionCall = x.FirstCall();
                              var attr = actionCall.Method.GetAttribute<NDjangoAttribute>();
                              x.AddToEnd(new NDjangoOutputNode(actionCall.OutputType(), attr.View));
                          });
        }
    }
}
using System;
using System.Collections.Generic;
using System.IO;
using FubuCore;
using FubuMVC.Core.Behaviors;
using FubuMVC.Core.Registration.Nodes;
using FubuMVC.Core.Registration.ObjectGraph;
using FubuMVC.Core.Runtime;
using FubuMVC.Core.UI.Tags;
using FubuMVC.Core.View;

namespace NDjango
{
    public class NDjangoOutputNode: OutputNode
    {
        private readonly Type _outputType;
        private readonly string _path;

        public NDjangoOutputNode(Type outputType, string path) : base(typeof(RenderNDjangoBehavior<>).MakeGenericType(outputType))
        {
            _outputType = outputType;
            _path = path;
        }

        public Type ModelType { get { return _outputType; } }
        public override string Description { get { return "NDjango"; } }

        public bool Equals(NDjangoOutputNode other)
        {
            if (ReferenceEquals(null, other)) return false;
            if (ReferenceEquals(this, other)) return true;
            return Equals(other._outputType, _outputType);
        }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (ReferenceEquals(this, obj)) return true;
            if (obj.GetType() != typeof(NDjangoOutputNode)) return false;
            return Equals((NDjangoOutputNode)obj);
        }

        protected override void configureObject(ObjectDef def)
        {
            def.Child(new ViewPath
            {
                ViewName = _path
            });
        }

        public override int GetHashCode()
        {
            return (_outputType != null ? _outputType.GetHashCode() : 0);
        }
    }

    public class RenderNDjangoBehavior<T> : BasicBehavior where T : class
    {
        private readonly IFubuRequest _request;
        private readonly IOutputWriter _outputWriter;
        private readonly ITagGenerator<T> _tagGenerator;
        private readonly ViewPath _path;

        public RenderNDjangoBehavior(IFubuRequest request, 
            IOutputWriter outputWriter, 
            ITagGenerator<T> tagGenerator,
            ViewPath path)
            : base(PartialBehavior.Executes)
        {
            _request = request;
            _outputWriter = outputWriter;
            _tagGenerator = tagGenerator;
            _path = path;
        }

        protected override FubuMVC.Core.DoNext performInvoke()
        {
            var fileStream = new FileStream(_path.ViewName.ToPhysicalPath(), FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

            var templateManagerProvider =
                   new TemplateManagerProvider()
                   .WithLoader(new TemplateLoader(new StreamReader(fileStream)));
            
            var templateManager =
                  templateManagerProvider.GetNewManager();

            var template =
                templateManager.GetTemplate(string.Empty);
            
            _tagGenerator.Model = _request.Get<T>();
           
            var context = new Dictionary<string, object> { { "Model", _request.Get<T>() }, {"Tags", _tagGenerator} };
           
            var reader = template.Walk(templateManager, context);

            _outputWriter.Write("text/html",reader.ReadToEnd());

            return base.performInvoke();
        }
    }
}
using System;

namespace NDjango
{
    public class NDjangoAttribute : Attribute
    {
        public string View { get; set; }

        public NDjangoAttribute(string view)
        {
            View = view;
        }
    }
}