1. Udi Bauman
  2. ekkli-maps


ekkli-maps / common / views.py

# Create your views here.
import datetime
from django.conf import settings
from django.core.mail import send_mail
from django.shortcuts import render_to_response
from django.contrib.auth.models import User
from django.http import HttpResponse
from django.utils import simplejson
from common.models import UserChannel
from maps.models import Story, UserProfile, GroupProfile, MapTemplate

def setup_first_user(request):
    if not User.objects.filter(username="admin").count():
        user = User()
        user.username = "admin"
        user.email = "inna.bauman@gmail.com "
        user.is_staff = True
        user.is_superuser = True
        template = MapTemplate()
        template.name = "Default"
        group = GroupProfile()
        group.name = "All"
        group.manager = user
        group.default_template = template
        profile = UserProfile()
        profile.user = user
        profile.group_profile = group
        return HttpResponse("First user created")
        return HttpResponse("User already exists")

def schedule_date_task(request):

    stories = Story.objects.all()
    current_date = datetime.date.today()

    for story in stories:
        time_delta = story.schedule_date - current_date
        if time_delta.days < 3:
            initiator = story.map.initiator
            send_mail('End of story date', 'Please pay attention, there is %s days till end of story ' % time_delta, settings.DEFAULT_FROM_EMAIL,
                [initiator.email], fail_silently=False)

def online_users(request):

    users = [obj.user for obj in UserChannel.objects.filter(connected=True, page_url = request.GET['path'])]

    json_users = [_user_to_json(user) for user in users ]

    data = simplejson.dumps(json_users)
    return HttpResponse(data)

def _user_to_json(user):
    json = {'user_id':user.id,'email':user.email,'username':user.username,'first_name':user.first_name,'last_name':user.last_name}
    return json