<commit_before># Copyright (c) 2010, <NAME> <<EMAIL>>
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

import datetime
import os
from django.db import models
from django.contrib.auth.models import User
from lib import feedparser, dateutil
from settings import SCREENSHOT_URL

# a blog for a project
class Blog(models.Model):
  # link to the blog, if it isn't hosted on dashboard
  url = models.URLField("Blog Web Address", max_length = 64)
  rss = models.URLField("Blog Feed", max_length = 64)
  
  # external (from an rss feed)? or hosted by dashboard?
  external = models.BooleanField()
  
  # fetches the posts from the rss feed, also saves the Blog to ensure it has
  # a key before attempting to add BlogPosts to it
  def fetch(self):
    self.save()
    
    # parse and iterate the feed
    for post in feedparser.parse(self.rss).entries:
      # time manipation is fun
      date = dateutil.parser.parse(post.date)
      date = (date - date.utcoffset()).replace(tzinfo=None)
      
      post = BlogPost(title = post.title,
                      content = post.description,
                      summary = post.description,
                      date = date,
                      external = True)
      post.blog = self
      post.save()
    self.save()
  
# a post in a blog
class BlogPost(models.Model):
  # title of the post
  title = models.CharField("Title", max_length = 128)
  
  # the text of the post
  markdown = models.TextField("Content")
  content = models.TextField()
  summary = models.TextField()
  
  # when the post was made
  date = models.DateTimeField(auto_now_add = True)
  
  # posts can be internal on external blogs (vice versa) if the blog switches
  external = models.BooleanField()
  
  # what blog the post is associated with
  blog = models.ForeignKey(Blog)
  
# a version control repository
class Repository(models.Model):
  # web access to the repository
  web_url = models.URLField("Repository Web Address", max_length = 128)
  
  # cloned repository fields
  clone_url = models.URLField("Repository Clone Address", max_length = 128)
  vcs = models.CharField("Version Control System", max_length = 3,
                         default = 'git',
                         choices = (('git', 'git'),
                                    ('svn', 'Subversion'),
                                    ('hg',  'Mercurial'),
                                    ('bzr', 'Bazaar')))
  
  # non-cloned repository fields
  repo_rss = models.URLField("Repository RSS Feed", max_length = 128)
  cmd = models.CharField("Clone Command", max_length = 128)
  
  # whether the repo uses cloning or just an rss feed
  cloned = models.BooleanField()
  
  def clone_cmd(self):
    if self.cloned:
      cmds = { 'git': 'clone', 'svn': 'co', 'hg': 'clone', 'bzr': 'branch' }
      return '{0} {1} {2}'.format(self.vcs, cmds[self.vcs], self.clone_url)
    else:
      return self.cmd
  
  def __unicode__(self):
    return self.url

# a commit in a repository
class Commit(models.Model):
  # the url to the commit (in cgit, etc.)
  url = models.URLField("Commit URL", max_length = 200)

  # the author of the commit, if he/she is in dashboard
  author = models.ForeignKey(User)

  # the author's name, if he/she isn't in dashboard
  author_name = models.CharField(max_length = 64)

  # the repository the commit is part of
  respository = models.ForeignKey(Repository)

# an RCOS project
class Project(models.Model):  
  # basic things
  title = models.CharField(max_length = 32)
  
  # a short description of the project
  description = models.TextField()
  <commit_msg>Fetch commits from RSS feeds.<commit_after># Copyright (c) 2010, <NAME> <<EMAIL>>
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

import datetime
import os
from django.db import models
from django.contrib.auth.models import User
from lib import feedparser, dateutil
from settings import SCREENSHOT_URL

# a blog for a project
class Blog(models.Model):
  # link to the blog, if it isn't hosted on dashboard
  url = models.URLField("Blog Web Address", max_length = 64)
  rss = models.URLField("Blog Feed", max_length = 64)
  
  # external (from an rss feed)? or hosted by dashboard?
  external = models.BooleanField()
  
  # fetches the posts from the rss feed
  def fetch(self):
    # don't fetch internally hosted blogs
    if not external: return
    
    # make sure we can add blogposts to the blog
    self.save()
    
    # parse and iterate the feed
    for post in feedparser.parse(self.rss).entries:
      # time manipation is fun
      date = dateutil.parser.parse(post.date)
      date = (date - date.utcoffset()).replace(tzinfo=None)
      
      post = BlogPost(title = post.title,
                      content = post.description,
                      summary = post.description,
                      date = date,
                      external = True)
      post.blog = self
      post.save()
    self.save()
  
# a post in a blog
class BlogPost(models.Model):
  # title of the post
  title = models.CharField("Title", max_length = 128)
  
  # the text of the post
  markdown = models.TextField("Content")
  content = models.TextField()
  summary = models.TextField()
  
  # when the post was made
  date = models.DateTimeField(auto_now_add = True)
  
  # posts can be internal on external blogs (vice versa) if the blog switches
  external = models.BooleanField()
  
  # what blog the post is associated with
  blog = models.ForeignKey(Blog)
  
# a version control repository
class Repository(models.Model):
  # web access to the repository
  web_url = models.URLField("Repository Web Address", max_length = 128)
  
  # cloned repository fields
  clone_url = models.URLField("Repository Clone Address", max_length = 128)
  vcs = models.CharField("Version Control System", max_length = 3,
                         default = 'git',
                         choices = (('git', 'git'),
                                    ('svn', 'Subversion'),
                                    ('hg',  'Mercurial'),
                                    ('bzr', 'Bazaar')))
  
  # non-cloned repository fields
  repo_rss = models.URLField("Repository RSS Feed", max_length = 128)
  cmd = models.CharField("Clone Command", max_length = 128)
  
  # whether the repo uses cloning or just an rss feed
  cloned = models.BooleanField()
  
  def fetch(self):
    if self.cloned:
      pass
    else:
      for commit in feedparser.parse(self.repo_rss).entries:
        date = dateutil.parser.parse(commit.date)
        date = (date - date.utcoffset()).replace(tzinfo=None)
        
        # can we find an author for this commit?
        author_name = commit.author_detail['name']
        print author_name
        try:
          author_firstlast = author_name.split(' ')
          print author_firstlast
          authors = User.objects.filter(first_name = author_firstlast[0],
                                        last_name = author_firstlast[1])
          print authors
          if len(authors) is 1:
            author = authors[0]
          else:
            author = None
        except:
          author = None
        
        # create and save the commit object
        commit = Commit(author_name = author_name,
                        title = commit.title,
                        log = commit.description,
                        url = commit.link,
                        date = date)
        commit.repository = self
        if author is not None:
          commit.author = author
        commit.save()
  
  def clone_cmd(self):
    if self.cloned:
      cmds = { 'git': 'clone', 'svn': 'co', 'hg': 'clone', 'bzr': 'branch' }
      return '{0} {1} {2}'.format(self.vcs, cmds[self.vcs], self.clone_url)
    else:
      return self.cmd
  
  def __unicode__(self):
    return self.web_url

# a commit in a repository
class Commit(models.Model):
  # the url to the commit (in cgit, etc.)
  url = models.URLField("Commit URL", max_length = 200, blank = True)

  # the author of the commit, if he/she is in dashboard
  author = models.ForeignKey(User, blank = True, null = True)

  # the author's name, if he/she isn't in dashboard
  author_name = models.CharField(max_length = 64, blank = True, null = True)
  
  # the title of the commit
  title = models.TextField()
  
  # the log of the commit
  log = models.TextField()
  
  # the diff for the commit. This won't exist for RSS commits
  diff = models.TextField(blank = True, null = True)

  # the repository the commit is part of
  repository = models.ForeignKey(Repository)
  
  # when the commit was made
  date = models.DateTimeField()

# an RCOS project
class Project(models.Model):  
  # basic things
  title = models.CharField(max_length = 32)
  
  # a short description of the project
  description = models.TextField()
  
{'fraction_non_alphanumeric': 0.08935455174458831, 'fraction_numerical': 0.007116734209745972, 'mean_word_length': 2.6148624508753127, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 7, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5426526', 'n_tokens_mistral': 2996, 'n_tokens_neox': 2805, 'n_words': 1050}