[Xfce4-commits] <moka:master> Make the datamapper code work.

Nick Schermer noreply at xfce.org
Sun Nov 20 14:30:12 CET 2011


Updating branch refs/heads/master
         to 20b2a186e2bd51b8d296688894232c173c0d4c3e (commit)
       from 571624237724b9c31e6fffdba31b9fb474192957 (commit)

commit 20b2a186e2bd51b8d296688894232c173c0d4c3e
Author: Nick Schermer <nick at xfce.org>
Date:   Mon Apr 4 21:40:12 2011 +0200

    Make the datamapper code work.

 examples/one-man-one-project/config.ru             |   49 +++++--
 .../one-man-one-project/project_release_mail.erb   |    3 +-
 lib/models/archive.rb                              |  152 ++++++++++----------
 lib/models/collection.rb                           |   24 ++-
 lib/models/maintainer.rb                           |    7 +-
 lib/models/project.rb                              |   26 +++--
 lib/models/role.rb                                 |    2 +
 lib/views/collection.erb                           |    2 +-
 lib/views/head.erb                                 |    4 +-
 lib/views/index.erb                                |    2 +-
 lib/views/manage_releases.erb                      |    4 +-
 lib/views/project.erb                              |    2 +-
 moka.gemspec                                       |    1 -
 13 files changed, 157 insertions(+), 121 deletions(-)

diff --git a/examples/one-man-one-project/config.ru b/examples/one-man-one-project/config.ru
index f0b68ad..971e910 100755
--- a/examples/one-man-one-project/config.ru
+++ b/examples/one-man-one-project/config.ru
@@ -24,47 +24,66 @@ Moka::Models::Configuration.load do |conf|
   conf.set :moka_url, 'https://releases.xfce.org'
   conf.set :archive_dir, '/home/nick/websites/archive.xfce.org/'
   conf.set :archive_url, 'http://archive.xfce.org'
+  conf.set :mirror, 'http://archive.xfce.org'
   conf.set :collection_release_pattern, /^([0-9]).([0-9]+)(pre[0-9])?$/
 end
 
 # Uncheck for production environment
 DataMapper.auto_migrate!
+DataMapper.finalize
 
 # create dummy roles
 admin = Moka::Models::Role.first_or_create(:name => 'admin')
 admin.save
 
+goodies = Moka::Models::Role.first_or_create(:name => 'goodies')
+goodies.save
+
 # create dummy user
-user = Moka::Models::Maintainer.first_or_create(
+nick = Moka::Models::Maintainer.first_or_create(
   { :username => 'nick' },
   { :realname => 'Nick Schermer',
     :password => Digest::SHA1.hexdigest('test'),
     :email => 'nick at xfce.org' }
 )
-user.roles << admin
-user.save
-user = Moka::Models::Maintainer.first_or_create(
+nick.roles << admin
+nick.roles << goodies
+nick.save
+
+jannis = Moka::Models::Maintainer.first_or_create(
   { :username => 'jannis' },
   { :realname => 'Jannis Pohlmann',
     :password => Digest::SHA1.hexdigest('test'),
     :email => 'jannis at xfce.org' }
 )
-user.save
-user = Moka::Models::Maintainer.first_or_create(
+jannis.roles << goodies
+jannis.save
+
+jeromeg = Moka::Models::Maintainer.first_or_create(
   { :username => 'jeromeg' },
   { :realname => 'Jérôme Guelfuccin',
     :password => Digest::SHA1.hexdigest('test'),
     :email => 'jeromeg at xfce.org' }
 )
-user.save
+jeromeg.roles << goodies
+jeromeg.save
+
+panel = Moka::Models::Project.first_or_create(
+  { :name =>           'xfce4-panel' },
+  { :website =>        'http://www.xfce.org',
+    :description =>    'Xfce\'s Panel' }
+)
+panel.maintainers << nick
+panel.save
 
-project = Moka::Models::Project.first_or_create(
-  { :name =>        'xfce4-panel' },
-  { :website =>     'http://www.xfce.org',
-    :description => 'Xfce\'s Panel' }
+thunar = Moka::Models::Project.first_or_create(
+  { :name =>           'thunar' },
+  { :website =>        'http://thunar.xfce.org',
+    :description =>    'Xfce\'s File Manager' }
 )
-project.maintainers << user
-project.save
+thunar.maintainers << nick
+thunar.maintainers << jannis
+thunar.save
 
 # create dummy classification
 collection = Moka::Models::Collection.first_or_create(
@@ -72,7 +91,9 @@ collection = Moka::Models::Collection.first_or_create(
   { :display_name => 'Xfce',
     :website => 'http://www.xfce.org' }
 )
-collection.maintainers << user
+collection.maintainers << nick
+collection.maintainers << jannis
+collection.maintainers << jeromeg
 collection.save
 
 run Moka::Application
diff --git a/examples/one-man-one-project/project_release_mail.erb b/examples/one-man-one-project/project_release_mail.erb
index 4c0ca41..e6a1d8d 100644
--- a/examples/one-man-one-project/project_release_mail.erb
+++ b/examples/one-man-one-project/project_release_mail.erb
@@ -7,6 +7,5 @@
 
 from the following mirrors:
 
-<% for mirror in Moka::Models::Mirror.find_all %>  <%= mirror.download_url(release) %>
-<% end %>
+<%= release.download_url %>
 <%= message %>
diff --git a/lib/models/archive.rb b/lib/models/archive.rb
index af5841b..4d1cd97 100755
--- a/lib/models/archive.rb
+++ b/lib/models/archive.rb
@@ -4,29 +4,29 @@ require 'singleton'
 module Moka
   module Models
     class Archive
-    
+
       include Singleton
-    
+
       def root_dir
         Configuration.get(:archive_dir)
       end
-    
+
       def excluded_classifications
         begin
           Configuration.get(:excluded_classifications)
-        rescue 
+        rescue
           []
         end
       end
-    
+
       def collection_dir(collection)
         File.join(root_dir, collection.name)
       end
-    
+
       def collection_release_dir(release)
         File.join(collection_dir(release.collection), release.version)
       end
-    
+
       def collection_source_dir(release)
         File.join(collection_release_dir(release), 'src')
       end
@@ -34,11 +34,11 @@ module Moka
       def collection_fat_tarball_dir(release)
         File.join(collection_release_dir(release), 'fat_tarballs')
       end
-    
+
       def collection_installer_dir(release)
         File.join(collection_release_dir(release), 'installers')
       end
-    
+
       def collection_fat_tarball_basename(release)
         "#{release.collection.name}-#{release.version}.tar.bz2"
       end
@@ -51,31 +51,31 @@ module Moka
 
       def collection_releases(collection)
         releases = []
-    
+
         if File.directory?(collection_dir(collection))
           dir = Dir.new(collection_dir(collection))
-          
+
           versions = dir.entries.select do |entry|
             entry != '.' and entry != '..' \
               and File.directory?(File.join(collection_dir(collection), entry))
           end
-    
+
           releases += versions.collect do |version|
             Collection::Release.new(collection, version)
           end
         end
-    
+
         releases
       end
-    
+
       def collection_release_add_project_release(release, project_release)
         source_dir = collection_source_dir(release)
-    
+
         File.makedirs(source_dir) unless File.directory?(source_dir)
-    
+
         link_target = project_release_tarball_filename(project_release)
         link_filename = File.join(source_dir, File.basename(link_target))
-    
+
         File.delete(link_filename) if File.file?(link_filename)
         File.link(link_target, link_filename)
 
@@ -88,31 +88,31 @@ module Moka
           File.link(link_target, link_filename)
         end
       end
-    
+
       def collection_release_remove_project_release(release, project_release)
         source_dir = collection_source_dir(release)
         tarball_basename = project_release_tarball_basename(project_release)
-    
+
         filename = File.join(source_dir, tarball_basename)
         File.delete(filename) if File.file?(filename)
       end
-    
+
       def collection_release_project_release_included?(release, project_release)
         source_dir = collection_source_dir(release)
-    
+
         if File.directory?(source_dir)
           dir = Dir.new(source_dir)
-    
+
           tarball = dir.entries.find do |entry|
             entry =~ project_release_tarball_pattern(project_release)
           end
-    
+
           return !tarball.nil?
         end
-    
+
         false
       end
-    
+
       def collection_release_delete(release)
         if File.directory?(collection_release_dir(release))
           FileUtils.rm_rf(collection_release_dir(release))
@@ -142,33 +142,33 @@ module Moka
         fat_tarball = collection_fat_tarball_filename(release)
         update_tarball_checksums(fat_tarball)
       end
-    
+
       def classification_dir(classification)
         File.join(root_dir, 'src', classification.name)
       end
-    
+
       def classifications
         return @classifications unless @classifications.nil?
-    
+
         @classifications = []
-    
+
         if File.directory?(File.join(root_dir, 'src'))
           dir = Dir.new(File.join(root_dir, 'src'))
-    
+
           names = dir.entries.select do |entry|
             entry != '.' and entry != '..' \
               and not excluded_classifications.include?(entry) \
               and File.directory?(File.join(root_dir, 'src', entry))
           end
-    
+
           @classifications += names.collect do |name|
             Classification.new(name, File.join(root_dir, 'src', name))
           end
-    
+
           @classifications.each do |classification|
             if File.directory?(classification_dir(classification))
               cdir = Dir.new(classification_dir(classification))
-    
+
               classification.project_names = cdir.entries.select do |entry|
                 entry != '.' and entry != '.' \
                   and File.directory?(File.join(classification_dir(classification), entry))
@@ -176,48 +176,48 @@ module Moka
             end
           end
         end
-    
+
         @classifications
       end
-    
+
       def project_dir(project)
         File.join(classification_dir(project.classification), project.name)
       end
-    
+
       def project_branch_dir(project, branch)
         File.join(project_dir(project), branch.name)
       end
-    
+
       def project_branches(project)
         branches = []
-    
+
         begin
           if File.directory?(project_dir(project))
             dir = Dir.new(project_dir(project))
-    
+
             names = dir.entries.select do |entry|
               entry != '.' and entry != '..' \
                 and File.directory?(File.join(project_dir(project), entry))
             end
-    
+
             branches += names.collect do |name|
               Project::Branch.new(project, name)
             end
           end
         rescue NoMethodError
         end
-    
+
         branches
       end
-    
+
       def project_branch_add_tarball(branch, file, basename)
         dir = project_branch_dir(branch.project, branch)
-    
+
         File.makedirs(dir) unless File.directory?(dir)
-    
+
         source_file = file.path
         target_file = File.join(dir, basename)
-    
+
         if File.file?(target_file)
           begin
             lockfile = File.new(target_file)
@@ -232,7 +232,7 @@ module Moka
           File.move(source_file, target_file)
           File.chmod(0664, target_file)
         end
-        
+
         update_tarball_checksums(target_file)
 
         project_branch_update(branch)
@@ -249,7 +249,7 @@ module Moka
             file.flock(File::LOCK_UN)
           end
         end
-    
+
         open("#{tarball}.sha1", File::CREAT|File::TRUNC|File::RDWR) do |file|
           begin
             file.flock(File::LOCK_EX)
@@ -259,18 +259,18 @@ module Moka
           end
         end
       end
-    
+
       def project_release_from_tarball(project, tarball)
         branch = project_tarball_branch(project, tarball)
         version = project_tarball_version(project, tarball)
         Project::Release.new(branch.project, branch, version)
       end
-      
+
       def project_branch_update(branch)
         dirname = project_branch_dir(branch.project, branch)
         begin Dir.rmdir(dirname) rescue SystemCallError end
       end
-    
+
       def project_tarball_pattern(project)
         /^(#{project.name})-([0-9\.]+[a-zA-Z0-9\-_]+)\.tar\.(bz2|gz)$/i
       end
@@ -288,11 +288,11 @@ module Moka
         version = tarball.gsub(project_tarball_upload_pattern(project), '\2')
         Project::Branch.new(project, version)
       end
-    
+
       def project_tarball_version(project, tarball)
         tarball.gsub(project_tarball_pattern(project), '\2')
       end
-    
+
       def project_release_tarball_basename(release)
         source_dir = project_branch_dir(release.project, release.branch)
 
@@ -308,7 +308,7 @@ module Moka
 
         "#{release.project.name}-#{release.version}.tar.bz2"
       end
-    
+
       def project_release_tarball_filename(release)
         File.join(project_branch_dir(release.project, release.branch), \
                   project_release_tarball_basename(release))
@@ -318,36 +318,36 @@ module Moka
         tarball_basename = project_release_tarball_basename(release)
         dirname = project_branch_dir(release.project, release.branch)
 
-        filenames = [ 
-          File.join(dirname, "#{tarball_basename}.md5"), 
+        filenames = [
+          File.join(dirname, "#{tarball_basename}.md5"),
           File.join(dirname, "#{tarball_basename}.sha1")
         ]
       end
-    
+
       def project_release_delete(release)
         filename = project_release_tarball_filename(release)
 
         File.delete(filename) if File.file?(filename)
 
         checksum_filenames = project_release_checksum_filenames(release)
-        for file in checksum_filenames 
+        for file in checksum_filenames
           File.delete(file) if File.file?(file)
         end
 
         project_branch_update(release.branch)
       end
-    
+
       def project_release_checksum(release, type)
         result = nil
-    
+
         tarball_basename = project_release_tarball_basename(release)
         branch_dir = project_branch_dir(release.project, release.branch)
-        basename = if type == Digest::MD5 then 
-          "#{tarball_basename}.md5" 
-        else 
+        basename = if type == Digest::MD5 then
+          "#{tarball_basename}.md5"
+        else
           "#{tarball_basename}.sha1"
         end
-    
+
         open(File.join(branch_dir, basename)) do |checksum_file|
           begin
             checksum_file.flock(File::LOCK_SH)
@@ -362,46 +362,46 @@ module Moka
             checksum_file.flock(File::LOCK_UN)
           end
         end
-    
+
         result
       end
-    
+
       def project_releases(project)
         releases = []
-    
+
         for branch in project.branches
           if File.directory?(project_branch_dir(project, branch))
             dir = Dir.new(project_branch_dir(project, branch))
-    
+
             tarballs = dir.entries.select do |entry|
               entry =~ project_tarball_pattern(project)
             end
-    
+
             releases += tarballs.collect do |tarball|
               version = project_tarball_version(project, tarball)
               Project::Release.new(project, branch, version)
             end
           end
         end
-    
+
         releases
       end
-    
+
       def project_change_classification(project, new_classification)
         old_classification = project.classification
         old_dir = project_dir(project)
-        
+
         project.classification = new_classification
         new_dir = project_dir(project)
-    
+
         path = Pathname.new(new_dir)
         parent = path.parent
-    
+
         File.makedirs(parent) unless File.directory?(parent)
-    
+
         project.classification = old_classifcation unless File.move(old_dir, new_dir)
-    
-        begin 
+
+        begin
           Dir.delete(old_dir)
         rescue SystemCallError
         end
diff --git a/lib/models/collection.rb b/lib/models/collection.rb
index c808cd6..78fa458 100755
--- a/lib/models/collection.rb
+++ b/lib/models/collection.rb
@@ -60,6 +60,21 @@ module Moka
         def update_checksums
           Archive.instance.collection_release_update_checksums(self)
         end
+        
+        def release_url
+          dir = Archive.instance.collection_release_dir(self)
+          dir.gsub(Archive.instance.root_dir, Configuration.get(:mirror))
+        end
+        
+        def download_url
+          dir  = Archive.instance.collection_source_dir(self)
+          dir.gsub(Archive.instance.root_dir, Configuration.get(:mirror))
+        end
+        
+        def fat_tarball_url
+          dir = Archive.instance.collection_fat_tarball_dir(self)
+          dir.gsub(Archive.instance.root_dir, Configuration.get(:mirror))
+        end
 
         def template_name
           'mailinglist_collection_announcement'
@@ -71,10 +86,7 @@ module Moka
       property :display_name, String
       property :website,      String
 
-      has n, :maintainers, :through => Resource
-      #has n, :maintainers, :through => Resource
-      #has n, :mailinglists, :through => Resource
-      #belongs_to :maintainer
+      has n,   :maintainers, :through => Resource
 
       def ==(other)
         other.is_a?(self.class) and other.name == name
@@ -83,10 +95,6 @@ module Moka
       def releases
         Archive.instance.collection_releases(self).sort
       end
-
-      def self.find_all_by_maintainer(maintainer)
-        all()
-      end
     end
   end
 end
diff --git a/lib/models/maintainer.rb b/lib/models/maintainer.rb
index 827689d..1815e07 100755
--- a/lib/models/maintainer.rb
+++ b/lib/models/maintainer.rb
@@ -11,10 +11,9 @@ module Moka
       property :email,    String
       property :pubkeys,  Text
 
-      has n, :roles, :through => Resource
-
-      #has n, :collections, :through => Resource
-      #has n, :projects, :through => Resource
+      has n,   :roles,       :through => Resource
+      has n,   :collections, :through => Resource
+      has n,   :projects,    :through => Resource
 
       def self.authenticate(username, password)
         encrypted_password = Digest::SHA1.hexdigest(password)
diff --git a/lib/models/project.rb b/lib/models/project.rb
index 37058a4..a3d790d 100755
--- a/lib/models/project.rb
+++ b/lib/models/project.rb
@@ -68,22 +68,34 @@ module Moka
           Archive.instance.project_release_delete(self)
         end
 
+        def release_url
+          dir = Archive.instance.project_branch_dir(self.project, self.branch)
+          dir.gsub(Archive.instance.root_dir, Configuration.get(:mirror))
+        end
+
+        def download_url
+          dir = Archive.instance.project_branch_dir(self.project, self.branch)
+          dir.gsub(Archive.instance.root_dir, Configuration.get(:mirror))
+        end
+
         def template_name
           'mailinglist_project_announcement'
         end
 
       end
 
-      property :name,           String, :key => true
-      property :website,        String
-      property :classification, String
-      property :description,    Text
+      property :name,        String, :key => true
+      property :website,     String
+      property :description, Text
 
       has n,   :maintainers, :through => Resource
 
-      # classification
       # mailinglists
 
+      def classification
+        Classification.find_by_project(self)
+      end
+
       def ==(other)
         other.is_a?(self.class) and other.name == name
       end
@@ -118,10 +130,6 @@ module Moka
       def release_from_tarball(tarball)
         Archive.instance.project_release_from_tarball(self, tarball)
       end
-
-      def self.find_all_by_maintainer(maintainer)
-        all()
-      end
     end
   end
 end
diff --git a/lib/models/role.rb b/lib/models/role.rb
index 877a6ec..c736974 100644
--- a/lib/models/role.rb
+++ b/lib/models/role.rb
@@ -4,6 +4,8 @@ module Moka
       include DataMapper::Resource
 
       property :name, String, :key => true
+
+      has n,   :maintainers, :through => Resource
     end
   end
 end
diff --git a/lib/views/collection.erb b/lib/views/collection.erb
index 152d451..09acb5a 100755
--- a/lib/views/collection.erb
+++ b/lib/views/collection.erb
@@ -3,7 +3,7 @@
 <table cellspacing="0" cellpadding="0">
   <% for release in @collection.releases.sort.reverse %>
     <tr>
-      <th><a href="<%= Mirror.find_all.first.release_url(release) %>"><%= release.version %></a></th>
+      <th><a href="<%= release.release_url %>"><%= release.version %></a></th>
       <td>
         <a href="/collection/<%= @collection.name %>/release/<%= release.version %>">Update</a>
         <a href="/collection/<%= @collection.name %>/release/<%= release.version %>/delete">Delete</a>
diff --git a/lib/views/head.erb b/lib/views/head.erb
index ef32520..dc858e9 100755
--- a/lib/views/head.erb
+++ b/lib/views/head.erb
@@ -8,13 +8,13 @@
   <body>
     <h1><%= if params.has_key?(:title) then params[:title] else 'Xfce Release Manager' end %></h1>
     <p id="main-toolbar">
-      <% if env['warden'].authenticated? %>
+      <% if authentication_finished? %>
         <a href="/">Manage Releases</a>
       <% else %>
         <a href="/login">Login</a>
         <a href="/request">Request Account</a>
       <% end %>
-      <% if env['warden'].authenticated? and authentication_user %>
+      <% if authentication_finished? and authentication_user %>
         <% if authentication_user.is_admin %>
           <a href="/maintainer">Maintainers</a>
         <% end %>
diff --git a/lib/views/index.erb b/lib/views/index.erb
index 8af75ab..22d3b96 100755
--- a/lib/views/index.erb
+++ b/lib/views/index.erb
@@ -3,7 +3,7 @@
 <ul>
   <li><a href="/feed">View the release news feed</a></li>
   <li><a href="http://identi.ca/xfce">Visit the identi.ca release news channel</a></li>
-  <% if env['warden'].authenticated? %>
+  <% if authentication_finished? %>
     <li><a href="/">Manage releases of your projects</a></li>
   <% else %>
     <li><a href="/login">Manage releases of your projects</a></li>
diff --git a/lib/views/manage_releases.erb b/lib/views/manage_releases.erb
index 5240e36..430cff7 100644
--- a/lib/views/manage_releases.erb
+++ b/lib/views/manage_releases.erb
@@ -3,7 +3,7 @@
 <p><a href="/announcements">Publish announcements</a></p>
 <h2>Collections</h2>
 <table cellspacing="0" cellpadding="0">
-  <% for collection in Collection.find_all_by_maintainer(env['warden'].user).sort %>
+  <% for collection in authentication_user.collections.sort %>
     <tr>
       <th><a href="/collection/<%= collection.name %>"><%= collection.display_name %></a></th>
       <td>
@@ -14,7 +14,7 @@
 </table>
 <h2>Projects</h2>
 <table cellspacing="0" cellpadding="0">
-  <% for project in Project.find_all_by_maintainer(env['warden'].user).sort %>
+  <% for project in authentication_user.projects.sort %>
     <tr>
       <th><a href="/project/<%= project.name %>"><%= project.name %></a></th>
       <td>
diff --git a/lib/views/project.erb b/lib/views/project.erb
index 952276e..e8d3fae 100755
--- a/lib/views/project.erb
+++ b/lib/views/project.erb
@@ -29,7 +29,7 @@
     <% for release in releases %>
       <tr>
         <th><%= "#{branch.name} Branch" if release == releases.first %></th>
-        <th><a href="<%= Mirror.find_all.first.download_url(release) %>"><%= release.version %></a></th>
+        <th><a href="<%= release.download_url %>"><%= release.version %></a></th>
         <td>
           <a href="/project/<%= @project.name %>/branch/<%= branch.name %>/release/<%= release.version %>/update">Update</a>
           <a href="/project/<%= @project.name %>/branch/<%= branch.name %>/release/<%= release.version %>/delete">Delete</a>
diff --git a/moka.gemspec b/moka.gemspec
index 6e4b147..efae094 100644
--- a/moka.gemspec
+++ b/moka.gemspec
@@ -33,7 +33,6 @@ Gem::Specification.new do |s|
     "lib/models/collection.rb",
     "lib/models/configuration.rb",
     "lib/models/maintainer.rb",
-    "lib/models/mirror.rb",
     "lib/models/project.rb",
     "lib/models/role.rb",
     "lib/moka.rb",


More information about the Xfce4-commits mailing list