real time notification app DateTimeField issue

152 views Asked by At

I wrote a notification app using swampdragon. Unfortunately, pub_date variable includes microseconds which I don't want it to be there. How can I format it like 'dd-MM-yyyy HH:mm:ss' or like in the admin site(a.m p.m version)? My controller.js:

var AnnounceControllers = angular.module('AnnounceControllers', []);


AnnounceControllers.controller('AnnounceListCtrl', ['$scope', '$dragon', function ($scope, $dragon) {
$scope.announceList = {};
$scope.announcements = [];
$scope.channel = 'announce';

$dragon.onReady(function() {
    $dragon.subscribe('announcements', $scope.channel, {announce_list__id: 1}).then(function(response) {
        $scope.dataMapper = new DataMapper(response.data);
    });

    $dragon.getSingle('announce-list', {id:1}).then(function(response) {
        $scope.announceList = response.data;
    });

    $dragon.getList('announcements', {list_id:1}).then(function(response) {
        $scope.announcements = response.data;
    });
});

$dragon.onChannelMessage(function(channels, message) {
    if (indexOf.call(channels, $scope.channel) > -1) {
        $scope.$apply(function() {
            $scope.dataMapper.mapData($scope.announcements, message);
        });
    }
});
}]);

My app.js:

var AnnounceApp = angular.module('AnnounceApp', [
'SwampDragonServices',
'AnnounceControllers'
]);

In HTML :

<h3 ng-repeat="item in announcements">
        <div class="alert alert-info" role="alert">
          {{ item.content }} {{item.pub_date| date:'dd-MM-yyyy HH:mm:ss'}}
          <br>
        </div>
      </h3>

models.py:

pub_date = models.DateTimeField(auto_now_add = True)

serializers.py:

class AnnounceSerializer(ModelSerializer):
class Meta:
    model = "post.Announce"
    publish_fields = ("content","pub_date")
2

There are 2 answers

0
kings856 On BEST ANSWER

I used some info from Daniel Cottone and did those:

class Announce(SelfPublishModel, models.Model):
serializer_class = AnnounceSerializer
announce_list = models.ForeignKey(AnnounceList)
content = models.CharField(max_length=100)
pub_date = models.DateTimeField(default=datetime.now)
date = models.TextField(null=True)

def __unicode__(self):
    return self.content


@receiver(pre_save,sender=Announce)
   def date_handler(sender,instance,*args,**kwargs):
       instance.date = instance.pub_date.isoformat()

isoformat without parameters worked for me.

9
Daniel Cottone On

You have to convert the DateTimeField to the ISO 8061 format so that the AngularJS date filter will work:

pub_date = models.DateTimeField(auto_now_add = True).isoformat(' ')

See this for more info.

EDIT:

After further clarification, the problem is that the timestamp you are getting is not formatted properly for javascript to recognize it as a date. See this plunkr for to how fix that.