from flask import request, render_template
import time
import lib.es as es
import web.util.tools as tools
import web.modules.post.services.workflow as workflow
import web.modules.post.services.upload as upload
import web.util.jinja as jinja
import traceback
import web.modules.post.services.notification as notification


def get(p):
    host = p['c']['host']; index = p['c']['index'];

    # init workflow
    wf = tools.get("wf", 'comment')
    p['workflow'] = workflow.init(wf, host, index)

    # for workflow actions
    p["post"] = es.get(host, index, 'post', tools.get("post_id"))
    if not p["post"]:
        return tools.alert('post id is not valid- {}'.format(tools.get("post_id")))

    ######################################################
    # validate
    if p['workflow'] and p['workflow'].get('validation'):
        try:
            exec (p['workflow']['validation'], globals())
            validation(p)
        except SystemExit: pass
        except Exception, e:
            return "{}\n{}".format(e.message, traceback.format_exc())
    ######################################################

    # create comment
    id = str(time.time()).replace(".", "")
    p['comment'] = {
        "id": id,
        "comment": tools.get("comment"),
        "created": es.now(),
        "created_by": p['login']
    }
    comments = p["post"].get("comment") \
        if p["post"].get("comment") \
        else []
    comments.append(p['comment'])

    es.update(host, index, 'post', p["post"]['id'], {"comment": comments})
    es.flush(host, index)

    ######################################################
    # Post action
    if p['workflow'] and p['workflow'].get('postaction'):
        try:
            exec (p['workflow']['postaction'], globals())
            postaction(p)
        except SystemExit: pass
        except Exception, e:
            return "{}\n{}".format(e.message, traceback.format_exc())
    ######################################################

    ######################################################
    # notification
    if p['workflow']:
        notifications = es.list(host, index, 'notification', 'workflow:{}'.format(p['workflow'].get('name')))
        for p['notification'] in notifications:
            p['notification']['recipients'] = jinja.getlist(p['notification'].get('recipients'))

            if p['notification'] and p['notification'].get('condition'):
                try:
                    exec (p['notification'].get('condition'), globals())
                    ret = condition(p)
                    if ret != True and ret: return ret
                except SystemExit: pass
                except Exception, e:
                    return "{}\n{}".format(e.message, traceback.format_exc())

                # send notification
                notification.send(p)
    ######################################################

    return tools.redirect(request.referrer)

@@ -757,16 +757,23 @@
   # 
-validate
+check condition
 %0A   
@@ -813,22 +813,21 @@
 '%5D.get('
-valida
+condi
 tion'):%0A
@@ -872,22 +872,21 @@
 flow'%5D%5B'
-valida
+condi
 tion'%5D, 
@@ -912,21 +912,73 @@
     
-validation(p)
+ret = condition(p)%0A            if ret != True and ret: return ret
 %0A   
@@ -1508,16 +1508,489 @@
 ent'%5D)%0A%0A
+    ######################################################%0A    # validate%0A    if p%5B'workflow'%5D and p%5B'workflow'%5D.get('validation'):%0A        try:%0A            exec (p%5B'workflow'%5D%5B'validation'%5D, globals())%0A            ret = validation(p)%0A            if ret != True and ret: return ret%0A        except SystemExit: pass%0A        except Exception, e:%0A            return %22%7B%7D%5Cn%7B%7D%22.format(e.message, traceback.format_exc())%0A    ######################################################%0A%0A
     es.u

comment feature