<commit_before>from django.db import models
from django.contrib.auth.models import User
import datetime
import random
from django.core.cache import cache
from apps.rss_feeds.models import Feed, Story
from utils import feedparser, object_manager, json
from apps.analyzer.models import ClassifierFeed, ClassifierAuthor, ClassifierTag, ClassifierTitle
from apps.analyzer.models import apply_classifier_titles, apply_classifier_feeds, apply_classifier_authors, apply_classifier_tags

DAYS_OF_UNREAD = 14

class UserSubscription(models.Model):
    user = models.ForeignKey(User)
    feed = models.ForeignKey(Feed)
    last_read_date = models.DateTimeField(default=datetime.datetime.now()
                                                  - datetime.timedelta(days=DAYS_OF_UNREAD))
    mark_read_date = models.DateTimeField(default=datetime.datetime.now()
                                                 - datetime.timedelta(days=DAYS_OF_UNREAD))
    unread_count_neutral = models.IntegerField(default=0)
    unread_count_positive = models.IntegerField(default=0)
    unread_count_negative = models.IntegerField(default=0)
    unread_count_updated = models.DateTimeField(default=datetime.datetime(2000,1,1))

    def __unicode__(self):
        return '[' + self.feed.feed_title + '] '

    def save(self, force_insert=False, force_update=False):
        self.unread_count_updated = datetime.datetime.now()
        super(UserSubscription, self).save(force_insert, force_update)

    def get_user_feeds(self):
        return Feed.objects.get(user=self.user, feed=feeds)

    def count_unread(self):
        if self.unread_count_updated > self.feed.last_update:
            return self.unread_count

        count = (self.stories_newer_lastread()
                 + self.stories_between_lastread_allread())
        if count == 0:
            self.mark_read_date = datetime.datetime.now()
            self.last_read_date = datetime.datetime.now()
            self.unread_count_updated = datetime.datetime.now()
            self.unread_count = 0
            self.save()
        else:
            self.unread_count = count
            self.unread_count_updated = datetime.datetime.now()
            self.save()
        return count

    def mark_read(self):
        self.last_read_date = datetime.datetime.now()
        self.unread_count -= 1
        self.unread_count_updated = datetime.datetime.now()
        self.save()

    def mark_feed_read(self):
        self.last_read_date = datetime.datetime.now()
        self.mark_read_date = datetime.datetime.now()
        self.unread_count = 0
        self.unread_count_updated = datetime.datetime.now()
        self.save()
        # readstories = ReadStories.objects.filter(user=self.user, feed=self.feed)
        # readstories.delete()

    def stories_newer_lastread(self):
        return self.feed.new_stories_since_date(self.last_read_date).count()

    def stories_between_lastread_allread(self):
        story_count =   Story.objects.filter(
                            story_date__gte=self.mark_read_date,
                            story_date__lte=self.last_read_date,
                            story_feed=self.feed
                        ).count()
        read_count =    UserStory.objects.filter(
                            feed=self.feed,
                            read_date__gte=self.mark_read_date,
                            read_date__lte=self.last_read_date
                        ).count()
        return story_count - read_count

    def subscribe_to_feed(self, feed_id):
        feed = Feed.objects.get(id=feed_id)
        new_subscription = UserSubscription(user=self.user, feed=feed)
        new_subscription.save()

    def calculate_feed_scores(self):
        print self
        feed_scores = dict(negative=0, neutral=0, positive=0)
        date_delta = datetime.datetime.now()-datetime.timedelta(days=DAYS_OF_UNREAD)
        if date_delta < self.mark_read_date:
            date_delta = self.mark_read_date
        read_stories = UserStory.objects.filter(user=self.user,
                                                feed=self.feed,
                                                story__story_date__gte=date_delta)
        print "Read stories: %s " % read_stories.count()
        stories_db = Story.objects.filter(story_date__gte=date_delta,
                                          story_feed=self.feed)\
                                  .exclude(id__in=[rs.story.id for rs in read_stories])
        print 'Stories_db: %s' % stories_db.count()
        stories = self.feed.format_stories(stories_db)
        classifier_feeds = ClassifierFeed.objects.filter(user=self.user, feed=self.feed)
        classifier_authors = ClassifierAuthor.objects.filter(user=self.user, feed=self.feed)
        classifier_titles = ClassifierTitle.objects.filter(user=self.user, feed=self.feed)
        classifier_tags = ClassifierTag.objects.filter(user=self.user, feed=self.feed)

        scores = {
            'feed': apply_classifier_feeds(classifier_feeds, self.feed),
        }

        for story in stories:
            scores.update({
                'author': apply_classifier_authors(classifier_authors, story),
                'tags': apply_classifier_tags(classifier_tags, story),
                'title': apply_classifier_titles(classifier_titles, story),
            })

            max_score = max(scores['feed'], scores['author'], scores['tags'], scores['title'])
            min_score = min(scores['feed'], scores['author'], scores['tags'], scores['title'])
            if max_score > 0:
                feed_scores['positive'] += 1
            if min_score < 0:
                feed_scores['negative'] += 1
            if max_score == 0 and min_score == 0:
                feed_scores['neutral'] += 1

        self.unread_count_positive = feed_scores['positive']
        self.unread_count_neutral = feed_scores['neutral']
        self.unread_count_negative = feed_scores['negative']

        self.save()

        return

    def get_scores(self):
        scores = json.decode(self.scores)
        return scores

    class Meta:
        unique_together = ("user", "feed")

class UserStory(models.Model):
    user = models.ForeignKey(User)
    feed = models.ForeignKey(Feed)
    story = models.ForeignKey(Story)
    read_date = models.DateTimeField(auto_now=True)
    opinion = models.IntegerField(default=0)

    def __unicode__(self):
        return ('[' + self.feed.feed_title + '] '
                + self.story.story_title)

    class Meta:
        verbose_name_plural = "user stories"
        verbose_name = "user story"
        unique_together = ("user", "feed", "story")

class UserSubscriptionFolders(models.Model):
    user = models.ForeignKey(User)
    user_sub = models.ForeignKey(UserSubscription)
    feed = models.ForeignKey(Feed)
    folder = models.CharField(max_length=255)

    def __unicode__(self):
        return ('[' + self.feed.feed_title + '] '
                + self.folder)

    class Meta:
        verbose_name_plural = "folders"
        verbose_name = "folder"
        unique_together = ("user", "user_sub")<commit_msg>Reducing load of calculate_scores()<commit_after>from django.db import models
from django.contrib.auth.models import User
import datetime
import random
from django.core.cache import cache
from apps.rss_feeds.models import Feed, Story
from utils import feedparser, object_manager, json
from apps.analyzer.models import ClassifierFeed, ClassifierAuthor, ClassifierTag, ClassifierTitle
from apps.analyzer.models import apply_classifier_titles, apply_classifier_feeds, apply_classifier_authors, apply_classifier_tags

DAYS_OF_UNREAD = 14

class UserSubscription(models.Model):
    user = models.ForeignKey(User)
    feed = models.ForeignKey(Feed)
    last_read_date = models.DateTimeField(default=datetime.datetime.now()
                                                  - datetime.timedelta(days=DAYS_OF_UNREAD))
    mark_read_date = models.DateTimeField(default=datetime.datetime.now()
                                                 - datetime.timedelta(days=DAYS_OF_UNREAD))
    unread_count_neutral = models.IntegerField(default=0)
    unread_count_positive = models.IntegerField(default=0)
    unread_count_negative = models.IntegerField(default=0)
    unread_count_updated = models.DateTimeField(default=datetime.datetime(2000,1,1))

    def __unicode__(self):
        return '[' + self.feed.feed_title + '] '

    def save(self, force_insert=False, force_update=False):
        self.unread_count_updated = datetime.datetime.now()
        super(UserSubscription, self).save(force_insert, force_update)

    def get_user_feeds(self):
        return Feed.objects.get(user=self.user, feed=feeds)

    def count_unread(self):
        if self.unread_count_updated > self.feed.last_update:
            return self.unread_count

        count = (self.stories_newer_lastread()
                 + self.stories_between_lastread_allread())
        if count == 0:
            self.mark_read_date = datetime.datetime.now()
            self.last_read_date = datetime.datetime.now()
            self.unread_count_updated = datetime.datetime.now()
            self.unread_count = 0
            self.save()
        else:
            self.unread_count = count
            self.unread_count_updated = datetime.datetime.now()
            self.save()
        return count

    def mark_read(self):
        self.last_read_date = datetime.datetime.now()
        self.unread_count -= 1
        self.unread_count_updated = datetime.datetime.now()
        self.save()

    def mark_feed_read(self):
        self.last_read_date = datetime.datetime.now()
        self.mark_read_date = datetime.datetime.now()
        self.unread_count = 0
        self.unread_count_updated = datetime.datetime.now()
        self.save()
        # readstories = ReadStories.objects.filter(user=self.user, feed=self.feed)
        # readstories.delete()

    def stories_newer_lastread(self):
        return self.feed.new_stories_since_date(self.last_read_date).count()

    def stories_between_lastread_allread(self):
        story_count =   Story.objects.filter(
                            story_date__gte=self.mark_read_date,
                            story_date__lte=self.last_read_date,
                            story_feed=self.feed
                        ).count()
        read_count =    UserStory.objects.filter(
                            feed=self.feed,
                            read_date__gte=self.mark_read_date,
                            read_date__lte=self.last_read_date
                        ).count()
        return story_count - read_count

    def subscribe_to_feed(self, feed_id):
        feed = Feed.objects.get(id=feed_id)
        new_subscription = UserSubscription(user=self.user, feed=feed)
        new_subscription.save()

    def calculate_feed_scores(self):
        print self
        feed_scores = dict(negative=0, neutral=0, positive=0)
        date_delta = datetime.datetime.now()-datetime.timedelta(days=DAYS_OF_UNREAD)
        if date_delta < self.mark_read_date:
            date_delta = self.mark_read_date
        # read_stories = UserStory.objects.filter(user=self.user,
        #                                         feed=self.feed,
        #                                         story__story_date__gte=date_delta)
        # print "Read stories: %s " % read_stories.count()
        stories_db = Story.objects.filter(story_feed=self.feed,
                                          story_date__gte=date_delta)[:100]
                                  # .exclude(id__in=[rs.story.id for rs in read_stories])
        print 'Stories_db: %s' % stories_db.count()
        stories = self.feed.format_stories(stories_db)
        classifier_feeds = ClassifierFeed.objects.filter(user=self.user, feed=self.feed)
        classifier_authors = ClassifierAuthor.objects.filter(user=self.user, feed=self.feed)
        classifier_titles = ClassifierTitle.objects.filter(user=self.user, feed=self.feed)
        classifier_tags = ClassifierTag.objects.filter(user=self.user, feed=self.feed)

        scores = {
            'feed': apply_classifier_feeds(classifier_feeds, self.feed),
        }

        for story in stories:
            scores.update({
                'author': apply_classifier_authors(classifier_authors, story),
                'tags': apply_classifier_tags(classifier_tags, story),
                'title': apply_classifier_titles(classifier_titles, story),
            })

            max_score = max(scores['feed'], scores['author'], scores['tags'], scores['title'])
            min_score = min(scores['feed'], scores['author'], scores['tags'], scores['title'])
            if max_score > 0:
                feed_scores['positive'] += 1
            if min_score < 0:
                feed_scores['negative'] += 1
            if max_score == 0 and min_score == 0:
                feed_scores['neutral'] += 1

        self.unread_count_positive = feed_scores['positive']
        self.unread_count_neutral = feed_scores['neutral']
        self.unread_count_negative = feed_scores['negative']

        self.save()

        return

    def get_scores(self):
        scores = json.decode(self.scores)
        return scores

    class Meta:
        unique_together = ("user", "feed")

class UserStory(models.Model):
    user = models.ForeignKey(User)
    feed = models.ForeignKey(Feed)
    story = models.ForeignKey(Story)
    read_date = models.DateTimeField(auto_now=True)
    opinion = models.IntegerField(default=0)

    def __unicode__(self):
        return ('[' + self.feed.feed_title + '] '
                + self.story.story_title)

    class Meta:
        verbose_name_plural = "user stories"
        verbose_name = "user story"
        unique_together = ("user", "feed", "story")

class UserSubscriptionFolders(models.Model):
    user = models.ForeignKey(User)
    user_sub = models.ForeignKey(UserSubscription)
    feed = models.ForeignKey(Feed)
    folder = models.CharField(max_length=255)

    def __unicode__(self):
        return ('[' + self.feed.feed_title + '] '
                + self.folder)

    class Meta:
        verbose_name_plural = "folders"
        verbose_name = "folder"
        unique_together = ("user", "user_sub")<|endoftext|>


<commit_before>from django.contrib import admin

from .models import Scene, Image

admin.site.register(Scene, admin.ModelAdmin)
admin.site.register(Image, admin.ModelAdmin)<commit_msg>add ScheduledDownload model to admin<commit_after>from django.contrib import admin

from .models import Scene, Image, ScheduledDownload

admin.site.register(Scene, admin.ModelAdmin)
admin.site.register(Image, admin.ModelAdmin)
admin.site.register(ScheduledDownload, admin.ModelAdmin)<|endoftext|>


<commit_before><commit_msg>add accept header to siesta<commit_after><|endoftext|>


<commit_before>"""
Tests for the MWS.Finances API class.
"""
import unittest
import mws
from .utils import CommonRequestTestTools

class FinancesTestCase(unittest.TestCase, CommonRequestTestTools):
    """
    Test cases for Finances.
    """
    # TODO: Add remaining methods for Finances
    def setUp(self):
        self.api = mws.Finances(
            self.CREDENTIAL_ACCESS,
            self.CREDENTIAL_SECRET,
            self.CREDENTIAL_ACCOUNT,
            auth_token=self.CREDENTIAL_TOKEN
        )
        self.api._test_request_params = True
<commit_msg>Add tests for Finances API<commit_after>"""
Tests for the Finances API class.
"""
import unittest
import datetime
import mws
from .utils import CommonRequestTestTools

class FinancesTestCase(unittest.TestCase, CommonRequestTestTools):
    """
    Test cases for Finances.
    """
    # TODO: Add remaining methods for Finances
    def setUp(self):
        self.api = mws.Finances(
            self.CREDENTIAL_ACCESS,
            self.CREDENTIAL_SECRET,
            self.CREDENTIAL_ACCOUNT,
            auth_token=self.CREDENTIAL_TOKEN
        )
        self.api._test_request_params = True

    def test_list_financial_event_groups(self):
        """
        ListFinancialEventGroups operation.
        """
        created_after = datetime.datetime.utcnow()
        created_after_stamp = created_after.iso_format()
        created_before = datetime.datetime.utcnow()
        created_before_stamp = created_before.iso_format()
        max_results = 659
        params = api.list_financial_event_groups(
            created_after=created_after,
            created_before=created_before,
            max_results=max_results,
        )
        self.assert_common_params(params)
        assert params['Action'] == 'ListFinancialEventGroups'
        assert params['FinancialEventGroupStartedAfter'] == created_after_stamp
        assert params['FinancialEventGroupStartedBefore'] == created_before_stamp
        assert params['MaxResultsPerPage'] == str(max_results)

    def test_list_financial_event_groups_by_next_token(self):
        """
        ListFinancialEventGroupsByNextToken operation, via method decorator.
        """
        next_token = 'VcNq06R0dO'
        params = self.api.list_financial_event_groups(next_token=next_token)
        self.assert_common_params(params)
        assert params['Action'] == 'ListFinancialEventGroupsByNextToken'
        assert params['NextToken'] == next_token

    def test_list_financial_event_groups_by_next_token_alias(self):
        """
        ListFinancialEventGroupsByNextToken operation, via alias method.
        """
        next_token = 'uhEPBAvUYR'
        params = self.api.list_financial_event_groups_by_next_token(next_token)
        self.assert_common_params(params)
        assert params['Action'] == 'ListFinancialEventGroupsByNextToken'
        assert params['NextToken'] == next_token

    def test_list_financial_events(self):
        """
        ListFinancialEvents operation.
        """
        posted_after = datetime.datetime.utcnow()
        posted_after_stamp = posted_after.iso_format()
        posted_before = datetime.datetime.utcnow()
        posted_before_stamp = posted_before.iso_format()
        amazon_order_id = '123-4567890-1234567'
        financial_event_group_id = '22YgYW55IGNhcm5hbCBwbGVhEXAMPLE'
        max_results = 156
        params = api.list_financial_events(
            financial_event_group_id=financial_event_group_id,
            amazon_order_id=amazon_order_id,
            posted_after=posted_after,
            posted_before=posted_before,
            max_results=max_results,
        )
        self.assert_common_params(params)
        assert params['Action'] == 'ListFinancialEvents'
        assert params['FinancialEventGroupId'] == financial_event_group_id
        assert params['AmazonOrderId'] == amazon_order_id
        assert params['PostedAfter'] == posted_after_stamp
        assert params['PostedBefore'] == posted_before_stamp
        assert params['MaxResultsPerPage'] == str(max_results)

    def test_list_financial_events_by_next_token(self):
        """
        ListFinancialEventsByNextToken operation, via method decorator.
        """
        next_token = '2t1DdnGqgf'
        params = self.api.list_financial_events(next_token=next_token)
        self.assert_common_params(params)
        assert params['Action'] == 'ListFinancialEventsByNextToken'
        assert params['NextToken'] == next_token

    def test_list_financial_events_by_next_token_alias(self):
        """
        ListFinancialEventsByNextToken operation, via alias method.
        """
        next_token = '7Ijm9Kmrgp'
        params = self.api.list_financial_events_by_next_token(next_token)
        self.assert_common_params(params)
        assert params['Action'] == 'ListFinancialEventsByNextToken'
        assert params['NextToken'] == next_token
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
from __future__ import absolute_import, print_function

import tensorflow as tf
from niftynet.layer.squeeze_excitation_layer import SELayer

class SETest(tf.test.TestCase):
    def test_3d_se_shape(self):
        input_shape = (2, 16, 16, 16, 32)
        x = tf.ones(input_shape)
        se_layer = SELayer()
        out_se = se_layer(x)

        with self.test_session() as sess:
            sess.run(tf.global_variables_initializer())
            out = sess.run(out_se)
            x_shape = tuple(x.shape.as_list())
            self.assertAllClose(x_shape, out.shape)

    def test_2d_se_shape(self):
        input_shape = (2, 16, 16, 32)
        x = tf.ones(input_shape)
        se_layer = SELayer()
        out_se = se_layer(x)

        with self.test_session() as sess:
            sess.run(tf.global_variables_initializer())
            out = sess.run(out_se)
            x_shape = tuple(x.shape.as_list())
            self.assertAllClose(x_shape, out.shape)

    def test_3d_se_excitation_op(self):
        input_shape = (2, 16, 16, 16, 32)
        x = tf.random_uniform(input_shape,seed=0)
        se_layer = SELayer()
        out_se = se_layer(x)

        with tf.Session() as sess:
            sess.run(tf.global_variables_initializer())
            x=sess.run(x)
            x_0_0=float(x[0,0,0,0,0])
            x_1_0=float(x[0,1,0,0,0])
            x_0_1=float(x[0,0,0,0,1])
            x_1_1=float(x[0,1,0,0,1])

        with tf.Session() as sess:
            sess.run(tf.global_variables_initializer())
            out = sess.run(out_se)
            out_0_0=float(out[0,0,0,0,0])
            out_1_0=float(out[0,1,0,0,0])
            out_0_1=float(out[0,0,0,0,1])
            out_1_1=float(out[0,1,0,0,1])

        div_0_0=out_0_0/x_0_0
        div_1_0=out_1_0/x_1_0
        div_0_1=out_0_1/x_0_1
        div_1_1=out_1_1/x_1_1

        with self.test_session() as sess:
            self.assertAlmostEqual(div_0_0, div_1_0)
            self.assertAlmostEqual(div_0_1, div_1_1)

    def test_2d_se_excitation_op(self):
        input_shape = (2, 16, 16, 32)
        x = tf.random_uniform(input_shape,seed=0)
        se_layer = SELayer()
        out_se = se_layer(x)

        with tf.Session() as sess:
            sess.run(tf.global_variables_initializer())
            x=sess.run(x)
            x_0_0=float(x[0,0,0,0])
            x_1_0=float(x[0,1,0,0])
            x_0_1=float(x[0,0,0,1])
            x_1_1=float(x[0,1,0,1])

        with tf.Session() as sess:
            sess.run(tf.global_variables_initializer())
            out = sess.run(out_se)
            out_0_0=float(out[0,0,0,0])
            out_1_0=float(out[0,1,0,0])
            out_0_1=float(out[0,0,0,1])
            out_1_1=float(out[0,1,0,1])

        div_0_0=out_0_0/x_0_0
        div_1_0=out_1_0/x_1_0
        div_0_1=out_0_1/x_0_1
        div_1_1=out_1_1/x_1_1

        with self.test_session() as sess:
            self.assertAlmostEqual(div_0_0, div_1_0)
            self.assertAlmostEqual(div_0_1, div_1_1)

    def test_pooling_op_error(self):
            with self.test_session() as sess:
                sess.run(tf.global_variables_initializer())

                with self.assertRaises(ValueError):
                    SELayer(func='ABC')

    def test_reduction_ratio_error(self):
        input_shape = (2, 16, 16, 16, 33)
        x = tf.ones(input_shape)
        se_layer = SELayer()

        with self.assertRaises(ValueError):
            se_layer(x)

if __name__ == "__main__":
    tf.test.main()
<commit_msg>added unit tests for ChannelSELayer, SpatialSELayer and ChannelSpatialSELayer<commit_after># -*- coding: utf-8 -*-
from __future__ import absolute_import, print_function

import tensorflow as tf
from niftynet.layer.squeeze_excitation_layer import ChannelSELayer
from niftynet.layer.squeeze_excitation_layer import SpatialSELayer
from niftynet.layer.squeeze_excitation_layer import ChannelSpatialSELayer

class SETest(tf.test.TestCase):
    def test_cSE_3d_shape(self):
        input_shape = (2, 16, 16, 16, 32)
        x = tf.ones(input_shape)
        se_layer = ChannelSELayer()
        out_se = se_layer(x)

        with self.test_session() as sess:
            sess.run(tf.global_variables_initializer())
            out = sess.run(out_se)
            x_shape = tuple(x.shape.as_list())
            self.assertAllClose(x_shape, out.shape)

    def test_sSE_3d_shape(self):
        input_shape = (2, 16, 16, 16, 32)
        x = tf.ones(input_shape)
        se_layer = SpatialSELayer()
        out_se = se_layer(x)

        with self.test_session() as sess:
            sess.run(tf.global_variables_initializer())
            out = sess.run(out_se)
            x_shape = tuple(x.shape.as_list())
            self.assertAllClose(x_shape, out.shape)

    def test_csSE_3d_shape(self):
        input_shape = (2, 16, 16, 16, 32)
        x = tf.ones(input_shape)
        se_layer = ChannelSpatialSELayer()
        out_se = se_layer(x)

        with self.test_session() as sess:
            sess.run(tf.global_variables_initializer())
            out = sess.run(out_se)
            x_shape = tuple(x.shape.as_list())
            self.assertAllClose(x_shape, out.shape)

    def test_cSE_2d_shape(self):
        input_shape = (2, 16, 16, 32)
        x = tf.ones(input_shape)
        se_layer = ChannelSELayer()
        out_se = se_layer(x)

        with self.test_session() as sess:
            sess.run(tf.global_variables_initializer())
            out = sess.run(out_se)
            x_shape = tuple(x.shape.as_list())
            self.assertAllClose(x_shape, out.shape)

    def test_sSE_2d_shape(self):
        input_shape = (2, 16, 16, 32)
        x = tf.ones(input_shape)
        se_layer = SpatialSELayer()
        out_se = se_layer(x)

        with self.test_session() as sess:
            sess.run(tf.global_variables_initializer())
            out = sess.run(out_se)
            x_shape = tuple(x.shape.as_list())
            self.assertAllClose(x_shape, out.shape)

    def test_csSE_2d_shape(self):
        input_shape = (2, 16, 16, 32)
        x = tf.ones(input_shape)
        se_layer = ChannelSpatialSELayer()
        out_se = se_layer(x)

        with self.test_session() as sess:
            sess.run(tf.global_variables_initializer())
            out = sess.run(out_se)
            x_shape = tuple(x.shape.as_list())
            self.assertAllClose(x_shape, out.shape)

    def test_cSE_3d_excitation_op(self):
        input_shape = (2, 16, 16, 16, 32)
        x = tf.random_uniform(input_shape,seed=0)
        se_layer = ChannelSELayer()
        out_se = se_layer(x)

        with tf.Session() as sess:
            sess.run(tf.global_variables_initializer())
            x=sess.run(x)
            x_0_0=float(x[0,0,0,0,0])
            x_1_0=float(x[0,1,0,0,0])
            x_0_1=float(x[0,0,0,0,1])
            x_1_1=float(x[0,1,0,0,1])

        with tf.Session() as sess:
            sess.run(tf.global_variables_initializer())
            out = sess.run(out_se)
            out_0_0=float(out[0,0,0,0,0])
            out_1_0=float(out[0,1,0,0,0])
            out_0_1=float(out[0,0,0,0,1])
            out_1_1=float(out[0,1,0,0,1])

        div_0_0=out_0_0/x_0_0
        div_1_0=out_1_0/x_1_0
        div_0_1=out_0_1/x_0_1
        div_1_1=out_1_1/x_1_1

        with self.test_session() as sess:
            self.assertAlmostEqual(div_0_0, div_1_0,places=5)
            self.assertAlmostEqual(div_0_1, div_1_1,places=5)

    def test_sSE_3d_excitation_op(self):
        input_shape = (2, 16, 16, 16, 32)
        x = tf.random_uniform(input_shape,seed=0)
        se_layer = SpatialSELayer()
        out_se = se_layer(x)

        with tf.Session() as sess:
            sess.run(tf.global_variables_initializer())
            x=sess.run(x)
            x_0_0=float(x[0,0,0,0,0])
            x_0_1=float(x[0,0,0,0,1])

        with tf.Session() as sess:
            sess.run(tf.global_variables_initializer())
            out = sess.run(out_se)
            out_0_0=float(out[0,0,0,0,0])
            out_0_1=float(out[0,0,0,0,1])

        div_0_0=out_0_0/x_0_0
        div_0_1=out_0_1/x_0_1

        with self.test_session() as sess:
            self.assertAlmostEqual(div_0_0, div_0_1,places=5)

    def test_cSE_2d_excitation_op(self):
        input_shape = (2, 16, 16, 32)
        x = tf.random_uniform(input_shape,seed=0)
        se_layer = ChannelSELayer()
        out_se = se_layer(x)

        with tf.Session() as sess:
            sess.run(tf.global_variables_initializer())
            x=sess.run(x)
            x_0_0=float(x[0,0,0,0])
            x_1_0=float(x[0,1,0,0])
            x_0_1=float(x[0,0,0,1])
            x_1_1=float(x[0,1,0,1])

        with tf.Session() as sess:
            sess.run(tf.global_variables_initializer())
            out = sess.run(out_se)
            out_0_0=float(out[0,0,0,0])
            out_1_0=float(out[0,1,0,0])
            out_0_1=float(out[0,0,0,1])
            out_1_1=float(out[0,1,0,1])

        div_0_0=out_0_0/x_0_0
        div_1_0=out_1_0/x_1_0
        div_0_1=out_0_1/x_0_1
        div_1_1=out_1_1/x_1_1

        with self.test_session() as sess:
            self.assertAlmostEqual(div_0_0, div_1_0,places=5)
            self.assertAlmostEqual(div_0_1, div_1_1,places=5)

    def test_sSE_2d_excitation_op(self):
        input_shape = (2, 16, 16, 32)
        x = tf.random_uniform(input_shape,seed=0)
        se_layer = SpatialSELayer()
        out_se = se_layer(x)

        with tf.Session() as sess:
            sess.run(tf.global_variables_initializer())
            x=sess.run(x)
            x_0_0=float(x[0,0,0,0])
            x_0_1=float(x[0,0,0,1])

        with tf.Session() as sess:
            sess.run(tf.global_variables_initializer())
            out = sess.run(out_se)
            out_0_0=float(out[0,0,0,0])
            out_0_1=float(out[0,0,0,1])

        div_0_0=out_0_0/x_0_0
        div_0_1=out_0_1/x_0_1

        with self.test_session() as sess:
            self.assertAlmostEqual(div_0_0, div_0_1,places=5)

    def test_cSE_pooling_op_error(self):
            with self.test_session() as sess:
                sess.run(tf.global_variables_initializer())

                with self.assertRaises(ValueError):
                    ChannelSELayer(func='ABC')

    def test_cSE_reduction_ratio_error(self):
        input_shape = (2, 16, 16, 16, 33)
        x = tf.ones(input_shape)
        se_layer = ChannelSELayer()

        with self.assertRaises(ValueError):
            se_layer(x)

if __name__ == "__main__":
    tf.test.main()
<|endoftext|>


<commit_before>##
#	\namespace	blur3d.api.classes.filesequence
#
#	\remarks	This file defines the FileSequence class that allows to manipulate a file sequence.
#
#	\author		douglas@blur.com
#	\author		Blur Studio
#	\date		09/11/11
#

#------------------------------------------------------------------------------------------------------------------------

import os

from framerange import FrameRange

#------------------------------------------------------------------------------------------------------------------------

class FileSequence( object ):

	def __init__( self, path, step=1 ):
		"""
			\remarks	Initialize the class.
		"""
		self._path = path
		self._step = step

	def path( self ):
		return os.path.abspath( self._path )

	def step( self ):
		return self._step

	def name( self ):
		return os.path.split( self._path )[1]

	def nameMask( self ):
		return '%(baseName)s.%(start)s-%(end)s.%(extension)s'

	def nameTokens( self ):
		import re
		regex = re.compile(r'^((?P<extension>[A-Za-z][A-Za-z][A-Za-z]).)((?P<range>(?P<end>[0-9]+)\-(?P<start>[0-9]+)).)(?P<baseName>[A-Za-z0-9_.]+)$')
		match = regex.match( self.name()[::-1] )
		if match:
			dict =  match.groupdict()
			for key in dict.keys():
				dict[ key ] = dict[ key ][::-1]
			return dict
		return None

	def nameToken( self, key ):
		if self.nameTokens().has_key( key ):
			return self.nameTokens()[ key ]
		else:
			return None

	def baseName( self ):
		return self.nameToken( 'baseName' )

	def uniqueName( self, rangePlaceHolder='' ):
		nameSplit = [ self.baseName(), self.extension() ]
		if rangePlaceHolder:
			nameSplit.insert( 1, rangePlaceHolder )
		return '.'.join( nameSplit )

	def frameRange( self, returnsAsString=False ):
		if returnsAsString:
			return self.nameToken( 'range' )
		return FrameRange( [ self.start(), self.end() ] )

	def start( self ):
		return int( self.nameToken( 'start' ) )

	def end( self ):
		return int( self.nameToken( 'end' ) )

	def extension( self ):
		return self.nameToken( 'extension' )

	def count( self ):
		return self.end() - self.start() + 1

	def setRange( self, range ):
		tokens = self.nameTokens()
		tokens[ 'start' ] = str( range[0] )
		tokens[ 'end' ] = str( range[1] )
		fileName = self.nameMask() % tokens
		self._path = os.path.join( self.basePath(), fileName )

	def padding( self ):
		padding = 1
		start = self.nameToken( 'start' )
		if not start == '0':
			for digit in start:
				if digit == '0':
					padding = padding + 1
		return padding

	def basePath( self ):
		return os.path.split( self._path )[0]

	def paddingCode( self ):
		return '%' + str( self.padding() ) + 'd'

	def codeName( self ):
		return self.baseName() + '.' + self.paddingCode() + '.' + self.extension()

	def codePath( self ):
		return os.path.join( self.basePath(), self.codeName() )

	def uniquePath( self, rangePlaceHolder='' ):
		return os.path.join( self.basePath(), self.uniqueName( rangePlaceHolder ) )

	def exists( self ):
		import glob
		paths = glob.glob( os.path.join( self.basePath(), self.baseName() + '.*.' + self.extension() ) )
		if len( paths ) > 0:
			return True
		return False

	def paths( self ):
		paths = []
		for frame in range( self.start(), self.end() + 1, self._step ):
			name = self.baseName() + '.' + str( frame ).zfill( self.padding() ) + '.' + self.extension()
			paths.append( os.path.join( self.basePath(), name ) )
		return paths

	def isComplete( self ):
		if len( self.missingFrames() ) > 0:
			return False
		return True

	def missingFrames( self ):
		frames = []
		for frame in range( self.start(), self.end() + 1, self._step ):
			name = self.baseName() + '.' + str( frame ).zfill( self.padding() ) + '.' + self.extension()
			path = os.path.join( self.basePath(), name )
			if not os.path.exists( path ):
				frames.append( frame )
		return frames

	def copy( self, output ):
		if self.isComplete():
			if self.count() == output.count():
				import shutil
				for i, o in zip( self.paths(), output.paths() ):
					shutil.copy( i, o )
				return True
			return False

	def delete( self, deletesBasePath=False ):
		if deletesBasePath:
			basePath = self.basePath()
			if os.path.exists( basePath ):
				os.rmdir( basePath )
		else:
			for path in self.paths():
				if os.path.exists( path ):
					os.remove( path )

	def generateMOV( self, outputPath=None, fps=30, ffmpeg='ffmpeg' ):
		if not outputPath:
			outputPath = os.path.join(( self.basePath() ), self.baseName() + '.mov' )
		extension = os.path.splitext( outputPath )[1]
		if extension == '.mov':
			if self.isComplete():
				normalisedSequencePath = os.path.join(  self.basePath(), self.baseName() + '_Temp.1-' + str( self.count() ) + '.' + self.extension() )
				normalisedSequence = FileSequence( normalisedSequencePath )
				self.copy( normalisedSequence )
				while not normalisedSequence.isComplete():
					continue
				import subprocess
				command = [ ffmpeg, '-r', str( fps ), "-i", normalisedSequence.codePath(), '-vcodec', 'mjpeg', '-qscale', '2', '-y', outputPath ]
				process = subprocess.Popen( command )
				process.communicate()
				normalisedSequence.delete()
				return True
			else:
				raise( 'Input sequence ' + str( self._path ) + ' is missing frames' )
		else:
			raise( 'Argument 1 should be a path to a mov file' )
		return True

	def link( self, output ):
		if self.isComplete():
			if input.count() == output.count():
				import subprocess
				for input, ouput in zip( input.paths(), output.paths() ):
					command = " ".join( [ "mklink", output, input ] )
					process = subprocess.Popen( command, shell = True )
				return True
		return False<commit_msg>Fixed encoding quality for FileSequence class.<commit_after>##
#	\namespace	blur3d.api.classes.filesequence
#
#	\remarks	This file defines the FileSequence class that allows to manipulate a file sequence.
#
#	\author		douglas@blur.com
#	\author		Blur Studio
#	\date		09/11/11
#

#------------------------------------------------------------------------------------------------------------------------

import os

from framerange import FrameRange

#------------------------------------------------------------------------------------------------------------------------

class FileSequence( object ):

	def __init__( self, path, step=1 ):
		"""
			\remarks	Initialize the class.
		"""
		self._path = path
		self._step = step

	def path( self ):
		return os.path.abspath( self._path )

	def step( self ):
		return self._step

	def name( self ):
		return os.path.split( self._path )[1]

	def nameMask( self ):
		return '%(baseName)s.%(start)s-%(end)s.%(extension)s'

	def nameTokens( self ):
		import re
		regex = re.compile(r'^((?P<extension>[A-Za-z][A-Za-z][A-Za-z]).)((?P<range>(?P<end>[0-9]+)\-(?P<start>[0-9]+)).)(?P<baseName>[A-Za-z0-9_.]+)$')
		match = regex.match( self.name()[::-1] )
		if match:
			dict =  match.groupdict()
			for key in dict.keys():
				dict[ key ] = dict[ key ][::-1]
			return dict
		return None

	def nameToken( self, key ):
		if self.nameTokens().has_key( key ):
			return self.nameTokens()[ key ]
		else:
			return None

	def baseName( self ):
		return self.nameToken( 'baseName' )

	def uniqueName( self, rangePlaceHolder='' ):
		nameSplit = [ self.baseName(), self.extension() ]
		if rangePlaceHolder:
			nameSplit.insert( 1, rangePlaceHolder )
		return '.'.join( nameSplit )

	def frameRange( self, returnsAsString=False ):
		if returnsAsString:
			return self.nameToken( 'range' )
		return FrameRange( [ self.start(), self.end() ] )

	def start( self ):
		return int( self.nameToken( 'start' ) )

	def end( self ):
		return int( self.nameToken( 'end' ) )

	def extension( self ):
		return self.nameToken( 'extension' )

	def count( self ):
		return self.end() - self.start() + 1

	def setRange( self, range ):
		tokens = self.nameTokens()
		tokens[ 'start' ] = str( range[0] )
		tokens[ 'end' ] = str( range[1] )
		fileName = self.nameMask() % tokens
		self._path = os.path.join( self.basePath(), fileName )

	def padding( self ):
		padding = 1
		start = self.nameToken( 'start' )
		if not start == '0':
			for digit in start:
				if digit == '0':
					padding = padding + 1
		return padding

	def basePath( self ):
		return os.path.split( self._path )[0]

	def paddingCode( self ):
		return '%' + str( self.padding() ) + 'd'

	def codeName( self ):
		return self.baseName() + '.' + self.paddingCode() + '.' + self.extension()

	def codePath( self ):
		return os.path.join( self.basePath(), self.codeName() )

	def uniquePath( self, rangePlaceHolder='' ):
		return os.path.join( self.basePath(), self.uniqueName( rangePlaceHolder ) )

	def exists( self ):
		import glob
		paths = glob.glob( os.path.join( self.basePath(), self.baseName() + '.*.' + self.extension() ) )
		if len( paths ) > 0:
			return True
		return False

	def paths( self ):
		paths = []
		for frame in range( self.start(), self.end() + 1, self._step ):
			name = self.baseName() + '.' + str( frame ).zfill( self.padding() ) + '.' + self.extension()
			paths.append( os.path.join( self.basePath(), name ) )
		return paths

	def isComplete( self ):
		if len( self.missingFrames() ) > 0:
			return False
		return True

	def missingFrames( self ):
		frames = []
		for frame in range( self.start(), self.end() + 1, self._step ):
			name = self.baseName() + '.' + str( frame ).zfill( self.padding() ) + '.' + self.extension()
			path = os.path.join( self.basePath(), name )
			if not os.path.exists( path ):
				frames.append( frame )
		return frames

	def copy( self, output ):
		if self.isComplete():
			if self.count() == output.count():
				import shutil
				for i, o in zip( self.paths(), output.paths() ):
					shutil.copy( i, o )
				return True
			return False

	def delete( self, deletesBasePath=False ):
		if deletesBasePath:
			basePath = self.basePath()
			if os.path.exists( basePath ):
				os.rmdir( basePath )
		else:
			for path in self.paths():
				if os.path.exists( path ):
					os.remove( path )

	def generateMOV( self, outputPath=None, fps=30, ffmpeg='ffmpeg' ):
		if not outputPath:
			outputPath = os.path.join(( self.basePath() ), self.baseName() + '.mov' )
		extension = os.path.splitext( outputPath )[1]
		if extension == '.mov':
			if self.isComplete():
				normalisedSequencePath = os.path.join(  self.basePath(), self.baseName() + '_Temp.1-' + str( self.count() ) + '.' + self.extension() )
				normalisedSequence = FileSequence( normalisedSequencePath )
				self.copy( normalisedSequence )
				while not normalisedSequence.isComplete():
					continue
				import subprocess
				command = [ ffmpeg, '-r', str( fps ), "-i", normalisedSequence.codePath(), '-vcodec', 'mjpeg', '-vb', '20000k', '-y', outputPath ]
				process = subprocess.Popen( command )
				process.communicate()
				normalisedSequence.delete()
				return True
			else:
				raise( 'Input sequence ' + str( self._path ) + ' is missing frames' )
		else:
			raise( 'Argument 1 should be a path to a mov file' )
		return True

	def link( self, output ):
		if self.isComplete():
			if input.count() == output.count():
				import subprocess
				for input, ouput in zip( input.paths(), output.paths() ):
					command = " ".join( [ "mklink", output, input ] )
					process = subprocess.Popen( command, shell = True )
				return True
		return False<|endoftext|>


<commit_before># pylint:disable=ungrouped-imports

from unittest.mock import patch

import pytest

import auditor

from event_manager.events import job as job_events
from factories.factory_jobs import JobFactory
from factories.factory_projects import ProjectFactory
from tests.utils import BaseTest

@pytest.mark.auditor_mark
class AuditorJobTest(BaseTest):
    """Testing subscribed events"""
    DISABLE_AUDITOR = False
    DISABLE_EXECUTOR = False

    def setUp(self):
        super().setUp()
        self.job = JobFactory(project=ProjectFactory())

    @patch('executor.service.ExecutorService.record_event')
    @patch('notifier.service.NotifierService.record_event')
    @patch('tracker.service.TrackerService.record_event')
    @patch('activitylogs.service.ActivityLogService.record_event')
    def test_job_created(self,
                         activitylogs_record,
                         tracker_record,
                         notifier_record,
                         executor_record):
        auditor.record(event_type=job_events.JOB_CREATED,
                       instance=self.job)

        assert tracker_record.call_count == 1
        assert activitylogs_record.call_count == 1
        assert notifier_record.call_count == 0
        assert executor_record.call_count == 1

    @patch('executor.service.ExecutorService.record_event')
    @patch('notifier.service.NotifierService.record_event')
    @patch('tracker.service.TrackerService.record_event')
    @patch('activitylogs.service.ActivityLogService.record_event')
    def test_job_updated(self,
                         activitylogs_record,
                         tracker_record,
                         notifier_record,
                         executor_record):
        auditor.record(event_type=job_events.JOB_UPDATED,
                       instance=self.job,
                       actor_id=1,
                       actor_name='foo')

        assert tracker_record.call_count == 1
        assert activitylogs_record.call_count == 1
        assert notifier_record.call_count == 0
        assert executor_record.call_count == 0

    @patch('executor.service.ExecutorService.record_event')
    @patch('notifier.service.NotifierService.record_event')
    @patch('tracker.service.TrackerService.record_event')
    @patch('activitylogs.service.ActivityLogService.record_event')
    def test_job_started(self,
                         activitylogs_record,
                         tracker_record,
                         notifier_record,
                         executor_record):
        auditor.record(event_type=job_events.JOB_STARTED,
                       instance=self.job)

        assert tracker_record.call_count == 1
        assert activitylogs_record.call_count == 0
        assert notifier_record.call_count == 0
        assert executor_record.call_count == 1

    @patch('executor.service.ExecutorService.record_event')
    @patch('notifier.service.NotifierService.record_event')
    @patch('tracker.service.TrackerService.record_event')
    @patch('activitylogs.service.ActivityLogService.record_event')
    def test_job_started_triggered(self,
                                   activitylogs_record,
                                   tracker_record,
                                   notifier_record,
                                   executor_record):
        auditor.record(event_type=job_events.JOB_STARTED_TRIGGERED,
                       instance=self.job,
                       actor_id=1,
                       actor_name='foo')

        assert tracker_record.call_count == 1
        assert activitylogs_record.call_count == 1
        assert notifier_record.call_count == 0
        assert executor_record.call_count == 0

    @patch('executor.service.ExecutorService.record_event')
    @patch('notifier.service.NotifierService.record_event')
    @patch('tracker.service.TrackerService.record_event')
    @patch('activitylogs.service.ActivityLogService.record_event')
    def test_job_cleaned_triggered(self,
                                   activitylogs_record,
                                   tracker_record,
                                   notifier_record,
                                   executor_record):
        auditor.record(event_type=job_events.JOB_CLEANED_TRIGGERED,
                       instance=self.job,
                       actor_id=1,
                       actor_name='foo')

        assert tracker_record.call_count == 0
        assert activitylogs_record.call_count == 0
        assert notifier_record.call_count == 0
        assert executor_record.call_count == 1

    @patch('executor.service.ExecutorService.record_event')
    @patch('notifier.service.NotifierService.record_event')
    @patch('tracker.service.TrackerService.record_event')
    @patch('activitylogs.service.ActivityLogService.record_event')
    def test_job_deleted(self,
                         activitylogs_record,
                         tracker_record,
                         notifier_record,
                         executor_record):
        auditor.record(event_type=job_events.JOB_DELETED,
                       instance=self.job)

        assert tracker_record.call_count == 1
        assert activitylogs_record.call_count == 0
        assert notifier_record.call_count == 0
        assert executor_record.call_count == 1

    @patch('executor.service.ExecutorService.record_event')
    @patch('notifier.service.NotifierService.record_event')
    @patch('tracker.service.TrackerService.record_event')
    @patch('activitylogs.service.ActivityLogService.record_event')
    def test_job_triggered_deleted(self,
                                   activitylogs_record,
                                   tracker_record,
                                   notifier_record,
                                   executor_record):
        auditor.record(event_type=job_events.JOB_DELETED_TRIGGERED,
                       instance=self.job,
                       actor_id=1,
                       actor_name='foo')

        assert tracker_record.call_count == 1
        assert activitylogs_record.call_count == 1
        assert notifier_record.call_count == 0
        assert executor_record.call_count == 0

    @patch('executor.service.ExecutorService.record_event')
    @patch('notifier.service.NotifierService.record_event')
    @patch('tracker.service.TrackerService.record_event')
    @patch('activitylogs.service.ActivityLogService.record_event')
    def test_job_stopped(self,
                         activitylogs_record,
                         tracker_record,
                         notifier_record,
                         executor_record):
        auditor.record(event_type=job_events.JOB_STOPPED,
                       instance=self.job)

        assert tracker_record.call_count == 1
        assert activitylogs_record.call_count == 0
        assert notifier_record.call_count == 1
        assert executor_record.call_count == 1

    @patch('executor.service.ExecutorService.record_event')
    @patch('notifier.service.NotifierService.record_event')
    @patch('tracker.service.TrackerService.record_event')
    @patch('activitylogs.service.ActivityLogService.record_event')
    def test_job_stopped_triggered(self,
                                   activitylogs_record,
                                   tracker_record,
                                   notifier_record,
                                   executor_record):
        auditor.record(event_type=job_events.JOB_STOPPED_TRIGGERED,
                       instance=self.job,
                       actor_id=1,
                       actor_name='foo')

        assert tracker_record.call_count == 1
        assert activitylogs_record.call_count == 1
        assert notifier_record.call_count == 0
        assert executor_record.call_count == 0

    @patch('executor.service.ExecutorService.record_event')
    @patch('notifier.service.NotifierService.record_event')
    @patch('tracker.service.TrackerService.record_event')
    @patch('activitylogs.service.ActivityLogService.record_event')
    def test_job_viewed(self,
                        activitylogs_record,
                        tracker_record,
                        notifier_record,
                        executor_record):
        auditor.record(event_type=job_events.JOB_VIEWED,
                       instance=self.job,
                       actor_id=1,
                       actor_name='foo')

        assert tracker_record.call_count == 1
        assert activitylogs_record.call_count == 1
        assert notifier_record.call_count == 0
        assert executor_record.call_count == 0

    @patch('executor.service.ExecutorService.record_event')
    @patch('notifier.service.NotifierService.record_event')
    @patch('tracker.service.TrackerService.record_event')
    @patch('activitylogs.service.ActivityLogService.record_event')
    def test_job_archived(self,
                          activitylogs_record,
                          tracker_record,
                          notifier_record,
                          executor_record):
        auditor.record(event_type=job_events.JOB_ARCHIVED,
                       instance=self.job,
                       actor_id=1,
                       actor_name='foo')

        assert tracker_record.call_count == 1
        assert activitylogs_record.call_count == 1
        assert notifier_record.call_count == 0
        assert executor_record.call_count == 0

    @patch('executor.service.ExecutorService.record_event')
    @patch('notifier.service.NotifierService.record_event')
    @patch('tracker.service.TrackerService.record_event')
    @patch('activitylogs.service.ActivityLogService.record_event')
    def test_job_RESTORED(self,
                            activitylogs_record,
                            tracker_record,
                            notifier_record,
                            executor_record):
        auditor.record(event_type=job_events.JOB_RESTORED,
                       instance=self.job,
                       actor_id=1,
                       actor_name='foo')

        assert tracker_record.call_count == 1
        assert activitylogs_record.call_count == 1
        assert notifier_record.call_count == 0
        assert executor_record.call_count == 0

    @patch('executor.service.ExecutorService.record_event')
    @patch('notifier.service.NotifierService.record_event')
    @patch('tracker.service.TrackerService.record_event')
    @patch('activitylogs.service.ActivityLogService.record_event')
    def test_job_bookmarked(self,
                            activitylogs_record,
                            tracker_record,
                            notifier_record,
                            executor_record):
        auditor.record(event_type=job_events.JOB_BOOKMARKED,
                       instance=self.job,
                       actor_id=1,
                       actor_name='foo')

        assert tracker_record.call_count == 1
        assert activitylogs_record.call_count == 1
        assert notifier_record.call_count == 0
        assert executor_record.call_count == 0

    @patch('executor.service.ExecutorService.record_event')
    @patch('notifier.service.NotifierService.record_event')
    @patch('tracker.service.TrackerService.record_event')
    @patch('activitylogs.service.ActivityLogService.record_event')
    def test_job_unbookmarked(self,
                              activitylogs_record,
                              tracker_record,
                              notifier_record,
                              executor_record):
        auditor.record(event_type=job_events.JOB_UNBOOKMARKED,
                       instance=self.job,
                       actor_id=1,
                       actor_name='foo')

        assert tracker_record.call_count == 1
        assert activitylogs_record.call_count == 1
        assert notifier_record.call_count == 0
        assert executor_record.call_count == 0

    @patch('executor.service.ExecutorService.record_event')
    @patch('notifier.service.NotifierService.record_event')
    @patch('tracker.service.TrackerService.record_event')
    @patch('activitylogs.service.ActivityLogService.record_event')
    def test_job_new_status(self,
                            activitylogs_record,
                            tracker_record,
                            notifier_record,
                            executor_record):
        auditor.record(event_type=job_events.JOB_NEW_STATUS,
                       instance=self.job)

        assert tracker_record.call_count == 1
        assert activitylogs_record.call_count == 0
        assert notifier_record.call_count == 0
        assert executor_record.call_count == 1

    @patch('executor.service.ExecutorService.record_event')
    @patch('notifier.service.NotifierService.record_event')
    @patch('tracker.service.TrackerService.record_event')
    @patch('activitylogs.service.ActivityLogService.record_event')
    def test_job_failed(self,
                        activitylogs_record,
                        tracker_record,
                        notifier_record,
                        executor_record):
        auditor.record(event_type=job_events.JOB_FAILED,
                       instance=self.job)

        assert tracker_record.call_count == 1
        assert activitylogs_record.call_count == 0
        assert notifier_record.call_count == 1
        assert executor_record.call_count == 1

    @patch('executor.service.ExecutorService.record_event')
    @patch('notifier.service.NotifierService.record_event')
    @patch('tracker.service.TrackerService.record_event')
    @patch('activitylogs.service.ActivityLogService.record_event')
    def test_job_succeeded(self,
                           activitylogs_record,
                           tracker_record,
                           notifier_record,
                           executor_record):
        auditor.record(event_type=job_events.JOB_SUCCEEDED,
                       instance=self.job)

        assert tracker_record.call_count == 1
        assert activitylogs_record.call_count == 0
        assert notifier_record.call_count == 1
        assert executor_record.call_count == 1

    @patch('executor.service.ExecutorService.record_event')
    @patch('notifier.service.NotifierService.record_event')
    @patch('tracker.service.TrackerService.record_event')
    @patch('activitylogs.service.ActivityLogService.record_event')
    def test_job_done(self,
                      activitylogs_record,
                      tracker_record,
                      notifier_record,
                      executor_record):
        auditor.record(event_type=job_events.JOB_DONE,
                       instance=self.job)

        assert tracker_record.call_count == 1
        assert activitylogs_record.call_count == 0
        assert notifier_record.call_count == 0
        assert executor_record.call_count == 1

    @patch('executor.service.ExecutorService.record_event')
    @patch('notifier.service.NotifierService.record_event')
    @patch('tracker.service.TrackerService.record_event')
    @patch('activitylogs.service.ActivityLogService.record_event')
    def test_job_logs_viewed_triggered(self,
                                       activitylogs_record,
                                       tracker_record,
                                       notifier_record,
                                       executor_record):
        auditor.record(event_type=job_events.JOB_LOGS_VIEWED,
                       instance=self.job,
                       actor_id=1,
                       actor_name='foo')

        assert tracker_record.call_count == 1
        assert activitylogs_record.call_count == 1
        assert notifier_record.call_count == 0
        assert executor_record.call_count == 0

    @patch('executor.service.ExecutorService.record_event')
    @patch('notifier.service.NotifierService.record_event')
    @patch('tracker.service.TrackerService.record_event')
    @patch('activitylogs.service.ActivityLogService.record_event')
    def test_job_restarted_triggered(self,
                                     activitylogs_record,
                                     tracker_record,
                                     notifier_record,
                                     executor_record):
        auditor.record(event_type=job_events.JOB_RESTARTED_TRIGGERED,
                       instance=self.job,
                       actor_id=1,
                       actor_name='foo')

        assert tracker_record.call_count == 1
        assert activitylogs_record.call_count == 1
        assert notifier_record.call_count == 0
        assert executor_record.call_count == 0

    @patch('executor.service.ExecutorService.record_event')
    @patch('notifier.service.NotifierService.record_event')
    @patch('tracker.service.TrackerService.record_event')
    @patch('activitylogs.service.ActivityLogService.record_event')
    def test_job_statuses_viewed_triggered(self,
                                           activitylogs_record,
                                           tracker_record,
                                           notifier_record,
                                           executor_record):
        auditor.record(event_type=job_events.JOB_STATUSES_VIEWED,
                       instance=self.job,
                       actor_id=1,
                       actor_name='foo')

        assert tracker_record.call_count == 1
        assert activitylogs_record.call_count == 1
        assert notifier_record.call_count == 0
        assert executor_record.call_count == 0

    @patch('executor.service.ExecutorService.record_event')
    @patch('notifier.service.NotifierService.record_event')
    @patch('tracker.service.TrackerService.record_event')
    @patch('activitylogs.service.ActivityLogService.record_event')
    def test_job_outputs_downloaded(self,
                                    activitylogs_record,
                                    tracker_record,
                                    notifier_record,
                                    executor_record):
        auditor.record(event_type=job_events.JOB_OUTPUTS_DOWNLOADED,
                       instance=self.job,
                       actor_id=1,
                       actor_name='foo')

        assert tracker_record.call_count == 1
        assert activitylogs_record.call_count == 1
        assert notifier_record.call_count == 0
        assert executor_record.call_count == 0
<commit_msg>Fix tests prospector<commit_after># pylint:disable=ungrouped-imports

from unittest.mock import patch

import pytest

import auditor

from event_manager.events import job as job_events
from factories.factory_jobs import JobFactory
from factories.factory_projects import ProjectFactory
from tests.utils import BaseTest

@pytest.mark.auditor_mark
class AuditorJobTest(BaseTest):
    """Testing subscribed events"""
    DISABLE_AUDITOR = False
    DISABLE_EXECUTOR = False

    def setUp(self):
        super().setUp()
        self.job = JobFactory(project=ProjectFactory())

    @patch('executor.service.ExecutorService.record_event')
    @patch('notifier.service.NotifierService.record_event')
    @patch('tracker.service.TrackerService.record_event')
    @patch('activitylogs.service.ActivityLogService.record_event')
    def test_job_created(self,
                         activitylogs_record,
                         tracker_record,
                         notifier_record,
                         executor_record):
        auditor.record(event_type=job_events.JOB_CREATED,
                       instance=self.job)

        assert tracker_record.call_count == 1
        assert activitylogs_record.call_count == 1
        assert notifier_record.call_count == 0
        assert executor_record.call_count == 1

    @patch('executor.service.ExecutorService.record_event')
    @patch('notifier.service.NotifierService.record_event')
    @patch('tracker.service.TrackerService.record_event')
    @patch('activitylogs.service.ActivityLogService.record_event')
    def test_job_updated(self,
                         activitylogs_record,
                         tracker_record,
                         notifier_record,
                         executor_record):
        auditor.record(event_type=job_events.JOB_UPDATED,
                       instance=self.job,
                       actor_id=1,
                       actor_name='foo')

        assert tracker_record.call_count == 1
        assert activitylogs_record.call_count == 1
        assert notifier_record.call_count == 0
        assert executor_record.call_count == 0

    @patch('executor.service.ExecutorService.record_event')
    @patch('notifier.service.NotifierService.record_event')
    @patch('tracker.service.TrackerService.record_event')
    @patch('activitylogs.service.ActivityLogService.record_event')
    def test_job_started(self,
                         activitylogs_record,
                         tracker_record,
                         notifier_record,
                         executor_record):
        auditor.record(event_type=job_events.JOB_STARTED,
                       instance=self.job)

        assert tracker_record.call_count == 1
        assert activitylogs_record.call_count == 0
        assert notifier_record.call_count == 0
        assert executor_record.call_count == 1

    @patch('executor.service.ExecutorService.record_event')
    @patch('notifier.service.NotifierService.record_event')
    @patch('tracker.service.TrackerService.record_event')
    @patch('activitylogs.service.ActivityLogService.record_event')
    def test_job_started_triggered(self,
                                   activitylogs_record,
                                   tracker_record,
                                   notifier_record,
                                   executor_record):
        auditor.record(event_type=job_events.JOB_STARTED_TRIGGERED,
                       instance=self.job,
                       actor_id=1,
                       actor_name='foo')

        assert tracker_record.call_count == 1
        assert activitylogs_record.call_count == 1
        assert notifier_record.call_count == 0
        assert executor_record.call_count == 0

    @patch('executor.service.ExecutorService.record_event')
    @patch('notifier.service.NotifierService.record_event')
    @patch('tracker.service.TrackerService.record_event')
    @patch('activitylogs.service.ActivityLogService.record_event')
    def test_job_cleaned_triggered(self,
                                   activitylogs_record,
                                   tracker_record,
                                   notifier_record,
                                   executor_record):
        auditor.record(event_type=job_events.JOB_CLEANED_TRIGGERED,
                       instance=self.job,
                       actor_id=1,
                       actor_name='foo')

        assert tracker_record.call_count == 0
        assert activitylogs_record.call_count == 0
        assert notifier_record.call_count == 0
        assert executor_record.call_count == 1

    @patch('executor.service.ExecutorService.record_event')
    @patch('notifier.service.NotifierService.record_event')
    @patch('tracker.service.TrackerService.record_event')
    @patch('activitylogs.service.ActivityLogService.record_event')
    def test_job_deleted(self,
                         activitylogs_record,
                         tracker_record,
                         notifier_record,
                         executor_record):
        auditor.record(event_type=job_events.JOB_DELETED,
                       instance=self.job)

        assert tracker_record.call_count == 1
        assert activitylogs_record.call_count == 0
        assert notifier_record.call_count == 0
        assert executor_record.call_count == 1

    @patch('executor.service.ExecutorService.record_event')
    @patch('notifier.service.NotifierService.record_event')
    @patch('tracker.service.TrackerService.record_event')
    @patch('activitylogs.service.ActivityLogService.record_event')
    def test_job_triggered_deleted(self,
                                   activitylogs_record,
                                   tracker_record,
                                   notifier_record,
                                   executor_record):
        auditor.record(event_type=job_events.JOB_DELETED_TRIGGERED,
                       instance=self.job,
                       actor_id=1,
                       actor_name='foo')

        assert tracker_record.call_count == 1
        assert activitylogs_record.call_count == 1
        assert notifier_record.call_count == 0
        assert executor_record.call_count == 0

    @patch('executor.service.ExecutorService.record_event')
    @patch('notifier.service.NotifierService.record_event')
    @patch('tracker.service.TrackerService.record_event')
    @patch('activitylogs.service.ActivityLogService.record_event')
    def test_job_stopped(self,
                         activitylogs_record,
                         tracker_record,
                         notifier_record,
                         executor_record):
        auditor.record(event_type=job_events.JOB_STOPPED,
                       instance=self.job)

        assert tracker_record.call_count == 1
        assert activitylogs_record.call_count == 0
        assert notifier_record.call_count == 1
        assert executor_record.call_count == 1

    @patch('executor.service.ExecutorService.record_event')
    @patch('notifier.service.NotifierService.record_event')
    @patch('tracker.service.TrackerService.record_event')
    @patch('activitylogs.service.ActivityLogService.record_event')
    def test_job_stopped_triggered(self,
                                   activitylogs_record,
                                   tracker_record,
                                   notifier_record,
                                   executor_record):
        auditor.record(event_type=job_events.JOB_STOPPED_TRIGGERED,
                       instance=self.job,
                       actor_id=1,
                       actor_name='foo')

        assert tracker_record.call_count == 1
        assert activitylogs_record.call_count == 1
        assert notifier_record.call_count == 0
        assert executor_record.call_count == 0

    @patch('executor.service.ExecutorService.record_event')
    @patch('notifier.service.NotifierService.record_event')
    @patch('tracker.service.TrackerService.record_event')
    @patch('activitylogs.service.ActivityLogService.record_event')
    def test_job_viewed(self,
                        activitylogs_record,
                        tracker_record,
                        notifier_record,
                        executor_record):
        auditor.record(event_type=job_events.JOB_VIEWED,
                       instance=self.job,
                       actor_id=1,
                       actor_name='foo')

        assert tracker_record.call_count == 1
        assert activitylogs_record.call_count == 1
        assert notifier_record.call_count == 0
        assert executor_record.call_count == 0

    @patch('executor.service.ExecutorService.record_event')
    @patch('notifier.service.NotifierService.record_event')
    @patch('tracker.service.TrackerService.record_event')
    @patch('activitylogs.service.ActivityLogService.record_event')
    def test_job_archived(self,
                          activitylogs_record,
                          tracker_record,
                          notifier_record,
                          executor_record):
        auditor.record(event_type=job_events.JOB_ARCHIVED,
                       instance=self.job,
                       actor_id=1,
                       actor_name='foo')

        assert tracker_record.call_count == 1
        assert activitylogs_record.call_count == 1
        assert notifier_record.call_count == 0
        assert executor_record.call_count == 0

    @patch('executor.service.ExecutorService.record_event')
    @patch('notifier.service.NotifierService.record_event')
    @patch('tracker.service.TrackerService.record_event')
    @patch('activitylogs.service.ActivityLogService.record_event')
    def test_job_restored(self,
                          activitylogs_record,
                          tracker_record,
                          notifier_record,
                          executor_record):
        auditor.record(event_type=job_events.JOB_RESTORED,
                       instance=self.job,
                       actor_id=1,
                       actor_name='foo')

        assert tracker_record.call_count == 1
        assert activitylogs_record.call_count == 1
        assert notifier_record.call_count == 0
        assert executor_record.call_count == 0

    @patch('executor.service.ExecutorService.record_event')
    @patch('notifier.service.NotifierService.record_event')
    @patch('tracker.service.TrackerService.record_event')
    @patch('activitylogs.service.ActivityLogService.record_event')
    def test_job_bookmarked(self,
                            activitylogs_record,
                            tracker_record,
                            notifier_record,
                            executor_record):
        auditor.record(event_type=job_events.JOB_BOOKMARKED,
                       instance=self.job,
                       actor_id=1,
                       actor_name='foo')

        assert tracker_record.call_count == 1
        assert activitylogs_record.call_count == 1
        assert notifier_record.call_count == 0
        assert executor_record.call_count == 0

    @patch('executor.service.ExecutorService.record_event')
    @patch('notifier.service.NotifierService.record_event')
    @patch('tracker.service.TrackerService.record_event')
    @patch('activitylogs.service.ActivityLogService.record_event')
    def test_job_unbookmarked(self,
                              activitylogs_record,
                              tracker_record,
                              notifier_record,
                              executor_record):
        auditor.record(event_type=job_events.JOB_UNBOOKMARKED,
                       instance=self.job,
                       actor_id=1,
                       actor_name='foo')

        assert tracker_record.call_count == 1
        assert activitylogs_record.call_count == 1
        assert notifier_record.call_count == 0
        assert executor_record.call_count == 0

    @patch('executor.service.ExecutorService.record_event')
    @patch('notifier.service.NotifierService.record_event')
    @patch('tracker.service.TrackerService.record_event')
    @patch('activitylogs.service.ActivityLogService.record_event')
    def test_job_new_status(self,
                            activitylogs_record,
                            tracker_record,
                            notifier_record,
                            executor_record):
        auditor.record(event_type=job_events.JOB_NEW_STATUS,
                       instance=self.job)

        assert tracker_record.call_count == 1
        assert activitylogs_record.call_count == 0
        assert notifier_record.call_count == 0
        assert executor_record.call_count == 1

    @patch('executor.service.ExecutorService.record_event')
    @patch('notifier.service.NotifierService.record_event')
    @patch('tracker.service.TrackerService.record_event')
    @patch('activitylogs.service.ActivityLogService.record_event')
    def test_job_failed(self,
                        activitylogs_record,
                        tracker_record,
                        notifier_record,
                        executor_record):
        auditor.record(event_type=job_events.JOB_FAILED,
                       instance=self.job)

        assert tracker_record.call_count == 1
        assert activitylogs_record.call_count == 0
        assert notifier_record.call_count == 1
        assert executor_record.call_count == 1

    @patch('executor.service.ExecutorService.record_event')
    @patch('notifier.service.NotifierService.record_event')
    @patch('tracker.service.TrackerService.record_event')
    @patch('activitylogs.service.ActivityLogService.record_event')
    def test_job_succeeded(self,
                           activitylogs_record,
                           tracker_record,
                           notifier_record,
                           executor_record):
        auditor.record(event_type=job_events.JOB_SUCCEEDED,
                       instance=self.job)

        assert tracker_record.call_count == 1
        assert activitylogs_record.call_count == 0
        assert notifier_record.call_count == 1
        assert executor_record.call_count == 1

    @patch('executor.service.ExecutorService.record_event')
    @patch('notifier.service.NotifierService.record_event')
    @patch('tracker.service.TrackerService.record_event')
    @patch('activitylogs.service.ActivityLogService.record_event')
    def test_job_done(self,
                      activitylogs_record,
                      tracker_record,
                      notifier_record,
                      executor_record):
        auditor.record(event_type=job_events.JOB_DONE,
                       instance=self.job)

        assert tracker_record.call_count == 1
        assert activitylogs_record.call_count == 0
        assert notifier_record.call_count == 0
        assert executor_record.call_count == 1

    @patch('executor.service.ExecutorService.record_event')
    @patch('notifier.service.NotifierService.record_event')
    @patch('tracker.service.TrackerService.record_event')
    @patch('activitylogs.service.ActivityLogService.record_event')
    def test_job_logs_viewed_triggered(self,
                                       activitylogs_record,
                                       tracker_record,
                                       notifier_record,
                                       executor_record):
        auditor.record(event_type=job_events.JOB_LOGS_VIEWED,
                       instance=self.job,
                       actor_id=1,
                       actor_name='foo')

        assert tracker_record.call_count == 1
        assert activitylogs_record.call_count == 1
        assert notifier_record.call_count == 0
        assert executor_record.call_count == 0

    @patch('executor.service.ExecutorService.record_event')
    @patch('notifier.service.NotifierService.record_event')
    @patch('tracker.service.TrackerService.record_event')
    @patch('activitylogs.service.ActivityLogService.record_event')
    def test_job_restarted_triggered(self,
                                     activitylogs_record,
                                     tracker_record,
                                     notifier_record,
                                     executor_record):
        auditor.record(event_type=job_events.JOB_RESTARTED_TRIGGERED,
                       instance=self.job,
                       actor_id=1,
                       actor_name='foo')

        assert tracker_record.call_count == 1
        assert activitylogs_record.call_count == 1
        assert notifier_record.call_count == 0
        assert executor_record.call_count == 0

    @patch('executor.service.ExecutorService.record_event')
    @patch('notifier.service.NotifierService.record_event')
    @patch('tracker.service.TrackerService.record_event')
    @patch('activitylogs.service.ActivityLogService.record_event')
    def test_job_statuses_viewed_triggered(self,
                                           activitylogs_record,
                                           tracker_record,
                                           notifier_record,
                                           executor_record):
        auditor.record(event_type=job_events.JOB_STATUSES_VIEWED,
                       instance=self.job,
                       actor_id=1,
                       actor_name='foo')

        assert tracker_record.call_count == 1
        assert activitylogs_record.call_count == 1
        assert notifier_record.call_count == 0
        assert executor_record.call_count == 0

    @patch('executor.service.ExecutorService.record_event')
    @patch('notifier.service.NotifierService.record_event')
    @patch('tracker.service.TrackerService.record_event')
    @patch('activitylogs.service.ActivityLogService.record_event')
    def test_job_outputs_downloaded(self,
                                    activitylogs_record,
                                    tracker_record,
                                    notifier_record,
                                    executor_record):
        auditor.record(event_type=job_events.JOB_OUTPUTS_DOWNLOADED,
                       instance=self.job,
                       actor_id=1,
                       actor_name='foo')

        assert tracker_record.call_count == 1
        assert activitylogs_record.call_count == 1
        assert notifier_record.call_count == 0
        assert executor_record.call_count == 0
<|endoftext|>


<commit_before>'''
Created on 5 févr. 2014

@author: inso
'''

from ucoinpy.api import bma
from ..core.person import Person
from ..tools.exceptions import NoPeerAvailable
from PyQt5.QtCore import QAbstractTableModel, QSortFilterProxyModel, Qt, \
                        QDateTime, QModelIndex
from PyQt5.QtGui import QColor
import logging

class IdentitiesFilterProxyModel(QSortFilterProxyModel):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.community = None

    def setSourceModel(self, sourceModel):
        self.community = sourceModel.community
        super().setSourceModel(sourceModel)

    def lessThan(self, left, right):
        """
        Sort table by given column number.
        """
        source_model = self.sourceModel()
        left_data = source_model.data(left, Qt.DisplayRole)
        right_data = source_model.data(right, Qt.DisplayRole)
        return (left_data < right_data)

    def data(self, index, role):
        source_index = self.mapToSource(index)
        source_data = self.sourceModel().data(source_index, role)
        expiration_col = self.sourceModel().columns_ids.index('expiration')
        expiration_index = self.sourceModel().index(source_index.row(), expiration_col)
        expiration_data = self.sourceModel().data(expiration_index, Qt.DisplayRole)
        current_time = QDateTime().currentDateTime().toMSecsSinceEpoch()
        sig_validity = self.community.parameters['sigValidity']
        warning_expiration_time = int(sig_validity / 3)
        #logging.debug("{0} > {1}".format(current_time, expiration_data))
        will_expire_soon = (current_time > expiration_data*1000 - warning_expiration_time*1000)
        if role == Qt.DisplayRole:
            if source_index.column() == self.sourceModel().columns_ids.index('renewed'):
                date = QDateTime.fromTime_t(source_data)
                return date.date()
            if source_index.column() == self.sourceModel().columns_ids.index('expiration'):
                date = QDateTime.fromTime_t(source_data)
                return date.date()
            if source_index.column() == self.sourceModel().columns_ids.index('pubkey'):
                return "pub:{0}".format(source_data[:5])

        if role == Qt.ForegroundRole:
            if will_expire_soon:
                return QColor(Qt.red)
        return source_data

class IdentitiesTableModel(QAbstractTableModel):

    '''
    A Qt abstract item model to display communities in a tree
    '''

    def __init__(self, community, parent=None):
        '''
        Constructor
        '''
        super().__init__(parent)
        self.community = community
        self.columns_titles = {
                               'uid': self.tr('UID'),
                               'pubkey': self.tr('Pubkey'),
                               'renewed': self.tr('Renewed'),
                               'expiration': self.tr('Expiration')}
        self.columns_ids = ('uid', 'pubkey', 'renewed', 'expiration')
        self.identities_data = []

    @property
    def pubkeys(self):
        '''
        Get pubkeys of displayed identities
        '''
        return [i[1] for i in self.identities_data]

    def identity_data(self, person):
        join_block = person.membership(self.community)['blockNumber']
        try:
            join_date = self.community.get_block(join_block).mediantime
        except NoPeerAvailable:
            join_date = 0
        parameters = self.community.parameters
        expiration_date = join_date + parameters['sigValidity']
        logging.debug((person.uid, person.pubkey, join_date, expiration_date))
        return (person.uid, person.pubkey, join_date, expiration_date)

    def refresh_identities(self, persons):
        logging.debug("Refresh {0} identities".format(len(persons)))
        self.identities_data = []
        self.beginResetModel()
        for person in persons:
            self.identities_data.append(self.identity_data(person))
        self.endResetModel()

    def rowCount(self, parent):
        return len(self.identities_data)

    def columnCount(self, parent):
        return len(self.columns_ids)

    def headerData(self, section, orientation, role):
        if role == Qt.DisplayRole:
            col_id = self.columns_ids[section]
            return self.columns_titles[col_id]

    def data(self, index, role):
        if role == Qt.DisplayRole:
            row = index.row()
            col = index.column()
            return self.identities_data[row][col]

    def person_index(self, pubkey):
        try:
            row = self.pubkeys.index(pubkey)
            index_start = self.index(row, 0)
            index_end = self.index(row, len(self.columns_ids))
            return (index_start, index_end)
        except ValueError:
            return (QModelIndex(), QModelIndex())

    def flags(self, index):
        return Qt.ItemIsSelectable | Qt.ItemIsEnabled
<commit_msg>Fix bug with no membership data accounts<commit_after>'''
Created on 5 févr. 2014

@author: inso
'''

from ucoinpy.api import bma
from ..core.person import Person
from ..tools.exceptions import NoPeerAvailable, MembershipNotFoundError
from PyQt5.QtCore import QAbstractTableModel, QSortFilterProxyModel, Qt, \
                        QDateTime, QModelIndex
from PyQt5.QtGui import QColor
import logging

class IdentitiesFilterProxyModel(QSortFilterProxyModel):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.community = None

    def setSourceModel(self, sourceModel):
        self.community = sourceModel.community
        super().setSourceModel(sourceModel)

    def lessThan(self, left, right):
        """
        Sort table by given column number.
        """
        source_model = self.sourceModel()
        left_data = source_model.data(left, Qt.DisplayRole)
        right_data = source_model.data(right, Qt.DisplayRole)
        left_data = 0 if left_data is None else left_data
        right_data = 0 if right_data is None else right_data
        return (left_data < right_data)

    def data(self, index, role):
        source_index = self.mapToSource(index)
        source_data = self.sourceModel().data(source_index, role)
        expiration_col = self.sourceModel().columns_ids.index('expiration')
        expiration_index = self.sourceModel().index(source_index.row(), expiration_col)
        expiration_data = self.sourceModel().data(expiration_index, Qt.DisplayRole)
        current_time = QDateTime().currentDateTime().toMSecsSinceEpoch()
        sig_validity = self.community.parameters['sigValidity']
        warning_expiration_time = int(sig_validity / 3)
        #logging.debug("{0} > {1}".format(current_time, expiration_data))
        if expiration_data is not None:
            will_expire_soon = (current_time > expiration_data*1000 - warning_expiration_time*1000)
        if role == Qt.DisplayRole:
            if source_index.column() == self.sourceModel().columns_ids.index('renewed'):
                date = QDateTime.fromTime_t(source_data).date() if source_data is not None else ""
                return date
            if source_index.column() == self.sourceModel().columns_ids.index('expiration'):
                date = QDateTime.fromTime_t(source_data).date() if source_data is not None else ""
                return date
            if source_index.column() == self.sourceModel().columns_ids.index('pubkey'):
                return "pub:{0}".format(source_data[:5])

        if role == Qt.ForegroundRole:
            if expiration_data:
                if will_expire_soon:
                    return QColor(Qt.red)
            else:
                return QColor(Qt.blue)
        return source_data

class IdentitiesTableModel(QAbstractTableModel):

    '''
    A Qt abstract item model to display communities in a tree
    '''

    def __init__(self, community, parent=None):
        '''
        Constructor
        '''
        super().__init__(parent)
        self.community = community
        self.columns_titles = {
                               'uid': self.tr('UID'),
                               'pubkey': self.tr('Pubkey'),
                               'renewed': self.tr('Renewed'),
                               'expiration': self.tr('Expiration')}
        self.columns_ids = ('uid', 'pubkey', 'renewed', 'expiration')
        self.identities_data = []

    @property
    def pubkeys(self):
        '''
        Get pubkeys of displayed identities
        '''
        return [i[1] for i in self.identities_data]

    def identity_data(self, person):
        parameters = self.community.parameters
        try:
            join_block = person.membership(self.community)['blockNumber']
            try:
                join_date = self.community.get_block(join_block).mediantime
                expiration_date = join_date + parameters['sigValidity']
            except NoPeerAvailable:
                join_date = None
                expiration_date = None
        except MembershipNotFoundError:
            join_date = None
            expiration_date = None

        return (person.uid, person.pubkey, join_date, expiration_date)

    def refresh_identities(self, persons):
        logging.debug("Refresh {0} identities".format(len(persons)))
        self.identities_data = []
        self.beginResetModel()
        for person in persons:
            self.identities_data.append(self.identity_data(person))
        self.endResetModel()

    def rowCount(self, parent):
        return len(self.identities_data)

    def columnCount(self, parent):
        return len(self.columns_ids)

    def headerData(self, section, orientation, role):
        if role == Qt.DisplayRole:
            col_id = self.columns_ids[section]
            return self.columns_titles[col_id]

    def data(self, index, role):
        if role == Qt.DisplayRole:
            row = index.row()
            col = index.column()
            return self.identities_data[row][col]

    def person_index(self, pubkey):
        try:
            row = self.pubkeys.index(pubkey)
            index_start = self.index(row, 0)
            index_end = self.index(row, len(self.columns_ids))
            return (index_start, index_end)
        except ValueError:
            return (QModelIndex(), QModelIndex())

    def flags(self, index):
        return Qt.ItemIsSelectable | Qt.ItemIsEnabled
<|endoftext|>