class LogEntry(models.Model): action_time = models.DateTimeField(_('action time'), auto_now=True) user = models.ForeignKey(User) content_type = models.ForeignKey(ContentType, blank=True, null=True) object_id = models.TextField(_('object id'), blank=True, null=True) object_repr = models.CharField(_('object repr'), max_length=200) action_flag = models.PositiveSmallIntegerField(_('action flag')) change_message = models.TextField(_('change message'), blank=True) objects = LogEntryManager() class Meta: verbose_name = _('log entry') verbose_name_plural = _('log entries') db_table = 'django_admin_log' ordering = ('-action_time', ) def __repr__(self): return smart_unicode(self.action_time) def __unicode__(self): if self.action_flag == ADDITION: return _('Added "%(object)s".') % {'object': self.object_repr} elif self.action_flag == CHANGE: return _('Changed "%(object)s" - %(changes)s') % { 'object': self.object_repr, 'changes': self.change_message } elif self.action_flag == DELETION: return _('Deleted "%(object)s."') % {'object': self.object_repr} return _('LogEntry Object') def is_addition(self): return self.action_flag == ADDITION def is_change(self): return self.action_flag == CHANGE def is_deletion(self): return self.action_flag == DELETION def get_edited_object(self): "Returns the edited object represented by this log entry" return self.content_type.get_object_for_this_type(pk=self.object_id) def get_admin_url(self): """ Returns the admin URL to edit the object represented by this log entry. This is relative to the Django admin index page. """ if self.content_type and self.object_id: return mark_safe(u"%s/%s/%s/" % (self.content_type.app_label, self.content_type.model, quote(self.object_id))) return None
class BaseCommentAbstractModel(models.Model): """ An abstract base class that any custom comment models probably should subclass. """ # Content-object field content_type = models.ForeignKey( ContentType, verbose_name=_('content type'), related_name="content_type_set_for_%(class)s") object_pk = models.TextField(_('object ID')) content_object = generic.GenericForeignKey(ct_field="content_type", fk_field="object_pk") # Metadata about the comment site = models.ForeignKey(Site) class Meta: abstract = True def get_content_object_url(self): """ Get a URL suitable for redirecting to the content object. """ return urlresolvers.reverse("comments-url-redirect", args=(self.content_type_id, self.object_pk))
class FlatPage(models.Model): url = models.CharField(_('URL'), max_length=100, db_index=True) title = models.CharField(_('title'), max_length=200) content = models.TextField(_('content'), blank=True) enable_comments = models.BooleanField(_('enable comments')) template_name = models.CharField( _('template name'), max_length=70, blank=True, help_text= _("Example: 'flatpages/contact_page.html'. If this isn't provided, the system will use 'flatpages/default.html'." )) registration_required = models.BooleanField( _('registration required'), help_text= _("If this is checked, only logged-in users will be able to view the page." )) sites = models.ManyToManyField(Site) class Meta: db_table = 'django_flatpage' verbose_name = _('flat page') verbose_name_plural = _('flat pages') ordering = ('url', ) def __unicode__(self): return u"%s -- %s" % (self.url, self.title) def get_absolute_url(self): return self.url
class Session(models.Model): """ Django provides full support for anonymous sessions. The session framework lets you store and retrieve arbitrary data on a per-site-visitor basis. It stores data on the server side and abstracts the sending and receiving of cookies. Cookies contain a session ID -- not the data itself. The Django sessions framework is entirely cookie-based. It does not fall back to putting session IDs in URLs. This is an intentional design decision. Not only does that behavior make URLs ugly, it makes your site vulnerable to session-ID theft via the "Referer" header. For complete documentation on using Sessions in your code, consult the sessions documentation that is shipped with Django (also available on the Django Web site). """ session_key = models.CharField(_('session key'), max_length=40, primary_key=True) session_data = models.TextField(_('session data')) expire_date = models.DateTimeField(_('expire date'), db_index=True) objects = SessionManager() class Meta: db_table = 'django_session' verbose_name = _('session') verbose_name_plural = _('sessions') def get_decoded(self): return SessionStore().decode(self.session_data)
def handle_label(self, tablename, **options): db = options.get('database') cache = BaseDatabaseCache(tablename, {}) if not router.allow_syncdb(db, cache.cache_model_class): return connection = connections[db] fields = ( # "key" is a reserved word in MySQL, so use "cache_key" instead. models.CharField(name='cache_key', max_length=255, unique=True, primary_key=True), models.TextField(name='value'), models.DateTimeField(name='expires', db_index=True), ) table_output = [] index_output = [] qn = connection.ops.quote_name for f in fields: field_output = [qn(f.name), f.db_type(connection=connection)] field_output.append("%sNULL" % (not f.null and "NOT " or "")) if f.primary_key: field_output.append("PRIMARY KEY") elif f.unique: field_output.append("UNIQUE") if f.db_index: unique = f.unique and "UNIQUE " or "" index_output.append("CREATE %sINDEX %s ON %s (%s);" % \ (unique, qn('%s_%s' % (tablename, f.name)), qn(tablename), qn(f.name))) table_output.append(" ".join(field_output)) full_statement = ["CREATE TABLE %s (" % qn(tablename)] for i, line in enumerate(table_output): full_statement.append( ' %s%s' % (line, i < len(table_output) - 1 and ',' or '')) full_statement.append(');') curs = connection.cursor() try: curs.execute("\n".join(full_statement)) except DatabaseError, e: self.stderr.write( self.style.ERROR( "Cache table '%s' could not be created.\nThe error was: %s.\n" % (tablename, e))) transaction.rollback_unless_managed(using=db)
class Comment(BaseCommentAbstractModel): """ A user comment about some object. """ # Who posted this comment? If ``user`` is set then it was an authenticated # user; otherwise at least user_name should have been set and the comment # was posted by a non-authenticated user. user = models.ForeignKey(User, verbose_name=_('user'), blank=True, null=True, related_name="%(class)s_comments") user_name = models.CharField(_("user's name"), max_length=50, blank=True) user_email = models.EmailField(_("user's email address"), blank=True) user_url = models.URLField(_("user's URL"), blank=True) comment = models.TextField(_('comment'), max_length=COMMENT_MAX_LENGTH) # Metadata about the comment submit_date = models.DateTimeField(_('date/time submitted'), default=None) ip_address = models.IPAddressField(_('IP address'), blank=True, null=True) is_public = models.BooleanField(_('is public'), default=True, help_text=_('Uncheck this box to make the comment effectively ' \ 'disappear from the site.')) is_removed = models.BooleanField(_('is removed'), default=False, help_text=_('Check this box if the comment is inappropriate. ' \ 'A "This comment has been removed" message will ' \ 'be displayed instead.')) # Manager objects = CommentManager() class Meta: db_table = "django_comments" ordering = ('submit_date', ) permissions = [("can_moderate", "Can moderate comments")] verbose_name = _('comment') verbose_name_plural = _('comments') def __unicode__(self): return "%s: %s..." % (self.name, self.comment[:50]) def save(self, *args, **kwargs): if self.submit_date is None: self.submit_date = timezone.now() super(Comment, self).save(*args, **kwargs) def _get_userinfo(self): """ Get a dictionary that pulls together information about the poster safely for both authenticated and non-authenticated comments. This dict will have ``name``, ``email``, and ``url`` fields. """ if not hasattr(self, "_userinfo"): self._userinfo = { "name": self.user_name, "email": self.user_email, "url": self.user_url } if self.user_id: u = self.user if u.email: self._userinfo["email"] = u.email # If the user has a full name, use that for the user name. # However, a given user_name overrides the raw user.username, # so only use that if this comment has no associated name. if u.get_full_name(): self._userinfo["name"] = self.user.get_full_name() elif not self.user_name: self._userinfo["name"] = u.username return self._userinfo userinfo = property(_get_userinfo, doc=_get_userinfo.__doc__) def _get_name(self): return self.userinfo["name"] def _set_name(self, val): if self.user_id: raise AttributeError(_("This comment was posted by an authenticated "\ "user and thus the name is read-only.")) self.user_name = val name = property(_get_name, _set_name, doc="The name of the user who posted this comment") def _get_email(self): return self.userinfo["email"] def _set_email(self, val): if self.user_id: raise AttributeError(_("This comment was posted by an authenticated "\ "user and thus the email is read-only.")) self.user_email = val email = property(_get_email, _set_email, doc="The email of the user who posted this comment") def _get_url(self): return self.userinfo["url"] def _set_url(self, val): self.user_url = val url = property(_get_url, _set_url, doc="The URL given by the user who posted this comment") def get_absolute_url(self, anchor_pattern="#c%(id)s"): return self.get_content_object_url() + (anchor_pattern % self.__dict__) def get_as_text(self): """ Return this comment as plain text. Useful for emails. """ d = { 'user': self.user or self.name, 'date': self.submit_date, 'comment': self.comment, 'domain': self.site.domain, 'url': self.get_absolute_url() } return _( 'Posted by %(user)s at %(date)s\n\n%(comment)s\n\nhttp://%(domain)s%(url)s' ) % d
class HgRepository(models.Model): """A version controlled source code repository""" name = models.SlugField(_("Name"), unique=True) summary = models.CharField(_('Summary'), max_length=255, blank=True, null=True) description = models.TextField(_('Description'), blank=True, null=True) anonymous_access = models.BooleanField(_("Allow Anonymous Viewing"), default=True) repo_path = models.CharField(editable=False, max_length=255) repo_url = models.CharField(editable=False, max_length=255) members = models.ManyToManyField(User, through='RepositoryUser', verbose_name="list of members") cmds = [ 'between', 'branches', 'branchmap', 'capabilities', 'changegroupsubset', 'heads', 'unbundle', 'changegroup' ] class Meta: verbose_name = _("Repository") verbose_name_plural = _("Repositories") def __unicode__(self): return self.name objects = HgRepositoryManager() @models.permalink def get_absolute_url(self): """ The path to the project page """ return ("hg-repo", {}, {'name': self.name}) def owners(self): """ Get the project owners """ return self.repositoryuser_set.filter( permission=7).order_by('user__username').select_related() def contributors(self): """ Get the write users """ return self.repositoryuser_set.filter(permission__gte=3).order_by( '-permission', 'user__username').select_related() def members(self): """ Get all users involved in a project """ return self.repositoryuser_set.filter(permission__gte=1).order_by( '-permission', 'user__username').select_related() def user_is_owner(self, userobj): """ Is this user an owner of the project """ try: repousr = self.repositoryuser_set.get(user=userobj) except: return False return repousr.permission > 4 def user_can_write(self, userobj): """ Does this user have write access to this repository """ try: repousr = self.repositoryuser_set.get(user=userobj) except: return False return repousr.permission > 2 def user_can_read(self, userobj): """ Does the user have read access to this repository? """ if self.anonymous_access: return True try: repousr = self.repositoryuser_set.get(user=userobj) except: return False return True # If we have a user, it must have at least read/write def move_to_public(self): """ Move a repository from private to public """ import os #os.umask(0) dest = os.path.abspath( os.path.join(global_settings.DJANGO_HG_REPOSITORIES_DIR['public'], self.name)) source = self.repo_path shutil.move(source, dest) def move_to_private(self): """ Move a repository from public to private """ import os #os.umask(0) source = self.repo_path dest = os.path.abspath( os.path.join(global_settings.DJANGO_HG_REPOSITORIES_DIR['private'], self.name)) shutil.move(source, dest) def get_size(self, directory=None): #The following is the code snippet to get the size of a directory in python: if directory is None: directory = self.repo_path dir_size = 0 for (path, dirs, files) in os.walk(directory): for file in files: try: filename = os.path.join(path, file) dir_size += os.path.getsize(filename) except: pass return dir_size def get_version_controller(self): if not controller: return return getattr(controller, VC_TYPE_CHOICES[self.vc_system - 1][1])(self.repo_path) def create_repository(self): """ Create a source code repository """ import os #os.umask(0) u = ui.ui() if self.anonymous_access: hg.repository( u, os.path.join( global_settings.DJANGO_HG_REPOSITORIES_DIR['public'], self.repo_path), True) else: hg.repository( u, os.path.join( global_settings.DJANGO_HG_REPOSITORIES_DIR['public'], self.repo_path), True) def save(self, force_insert=False, force_update=False): """ Do a little maintenence before doing the final save """ new = self.id is None if self.anonymous_access: repo_path = os.path.abspath( os.path.join( global_settings.DJANGO_HG_REPOSITORIES_DIR['public'], self.name)) repo_url = "%s%s/" % ( global_settings.DJANGO_HG_REPOSITORIES_DIR['public'], self.name) if not new and repo_path != self.repo_path: self.move_to_public() else: repo_path = os.path.abspath( os.path.join( global_settings.DJANGO_HG_REPOSITORIES_DIR['private'], self.name)) repo_url = "%s%s/" % ( global_settings.DJANGO_HG_REPOSITORIES_DIR['private'], self.name) if not new and repo_path != self.repo_path: self.move_to_private() self.repo_path = repo_path self.repo_url = repo_url super(HgRepository, self).save(force_insert, force_update) # create the source repository here, if new if not os.path.isdir(self.repo_path): #new: self.create_repository() def get_hash(self): return self.get_context().ctx def get_context(self): return self.context def set_context(self, ctx): self.context = ctx def delete(self): # Delete the source repository here shutil.rmtree(self.repo_path) super(HgRepository, self).delete()
class Article(models.Model): topic = models.ForeignKey(Topic) text = models.TextField(u'Текст') filter = models.CharField(u'Фильтр', max_length=50, choices=[(k, k) for k in filters.keys()]) created = models.DateTimeField(default=datetime.now, db_index=True) updated = models.DateTimeField(auto_now=True, db_index=True) author = models.ForeignKey(Profile) guest_name = models.CharField(max_length=255, blank=True) deleted = models.DateTimeField(null=True, db_index=True) spawned_to = models.OneToOneField(Topic, null=True, related_name='spawned_from') spam_status = models.CharField(max_length=20, default='clean') ip = models.IPAddressField(default='127.0.0.1') # -- is_forum = models.BooleanField(default=True) objects = ArticleManager() deleted_objects = DeletedArticleManager() class Meta: ordering = ['created'] def __unicode__(self): #return u"topic: %s, author: %s, created: %s" % (self.topic, self.author, self.created) #.replace(microsecond=0)) return u"{topic: %s, author: %s, created: %s}" % ( self.topic, self.author, self.created.replace(microsecond=0)) #return 'topic': self.topic, 'author': self.author, 'created': self.created} def delete(self): topic = self.topic super(Article, self).delete() if topic.article_set.count() == 0: topic.delete() def html(self): ''' Возвращает HTML-текст статьи, полученный фильтрацией содержимого через указанный фильтр. ''' if self.filter in filters: result = filters[self.filter](self.text) else: result = linebreaks(escape(self.text)) return mark_safe(result) soup = BeautifulSoup(result) def urlify(s): s = re.sub(WWW_PATTERN, r'\1http://www.', s) s = re.sub(FTP_PATTERN, r'\1ftp://ftp.', s) s = re.sub(PROTOCOL_PATTERN, r'<a href="\1\2">\1\2</a>\3\4', s) return BeautifulSoup(s) def has_parents(node, tags): if node is None: return False return node.name in tags or has_parents(node.parent, tags) text_chunks = [ c for c in soup.recursiveChildGenerator() if isinstance(c, unicode) ] for chunk in text_chunks: s = chunk if not has_parents(chunk.parent, ['code']): s = re.sub(ur'\B--\B', u'—', s) if not has_parents(chunk.parent, ['a', 'code']): s = urlify(s) chunk.replaceWith(s) for link in soup.findAll('a'): if 'rel' in link: link['rel'] += ' ' else: link['rel'] = '' link['rel'] += 'nofollow' result = unicode(soup) return mark_safe(result) def from_guest(self): ''' Была ли написана статья от имени гостя. Используется, в основном, в шаблонах. ''' return self.author.user == 'cicero_guest' def spawned(self): ''' Перенесена ли статья в новый топик. ''' return self.spawned_to_id is not None def ping_external_links(self): ''' Пингование внешних ссылок через Pingback (http://www.hixie.ch/specs/pingback/pingback) ''' domain = Site.objects.get_current().domain index_url = reverse('cicero_index') topic_url = utils.absolute_url( reverse('cicero.views.topic', args=(self.topic.forum.slug, self.topic.id))) def is_external(url): scheme, server, path, query, fragment = urlsplit(url) return server != '' and \ (server != domain or not path.startswith(index_url)) def search_link(content): match = re.search(r'<link rel="pingback" href="([^"]+)" ?/?>', content) return match and match.group(1) soup = BeautifulSoup(self.html()) links = (a['href'] for a in soup.findAll('a') if is_external(a['href'])) links = (l.encode('utf-8') for l in links) for link in links: try: f = urlopen(link) try: info = f.info() server_url = info.get('X-Pingback', '') or \ search_link(f.read(512 * 1024)) if server_url: server = ServerProxy(server_url) server.pingback.ping(topic_url, link) finally: f.close() except (IOError, Fault, ProtocolError, ResponseError, ExpatError): pass def set_spam_status(self, spam_status): ''' Проставляет статус спамности, поправляя, если надо, аналогичный статус топика. Сохраняет результат в базу. ''' if self.spam_status == spam_status: return self.spam_status = spam_status self.save() if self.topic.spam_status != spam_status and self.topic.article_set.count( ) == 1: self.topic.spam_status = spam_status self.topic.save()