Index: subscription.py |
=================================================================== |
new file mode 100755 |
--- /dev/null |
+++ b/subscription.py |
@@ -0,0 +1,272 @@ |
+#This file is part of Tryton. The COPYRIGHT file at the top level of |
+#this repository contains the full copyright notices and license terms. |
+#The idea for subscription Module has been taken from OpenERP. |
+ |
+"Subscriptions and Recurring operations" |
+ |
+from datetime import datetime |
+from trytond.model import ModelView, ModelSQL, fields |
+from trytond.pyson import Eval, Get |
+ |
+class SubscriptionTemplate(ModelSQL, ModelView): |
+ 'Subscription Template' |
+ _name = 'subscription.template' |
+ _description = __doc__ |
+ _order = 'name' |
+ |
+ name = fields.Char('Name', size=64, required=True,) |
+ active = fields.Boolean('Active',) |
+ model = fields.Many2One('ir.model', 'Object', required=True,) |
+ fields = fields.One2Many( |
+ 'subscription.template.defaults', |
+ 'template', 'Subscription Template Fields', |
+ ) |
+ |
+ def __init__(self): |
+ super(SubscriptionTemplate, self).__init__() |
+ self._sql_constraints += [ |
+ ('name_uniq', 'UNIQUE(name)', 'The Name must be unique!'), |
+ ] |
+ self._error_messages.update({ |
+ 'modify_template': 'You cannot modify the Object linked to the ' |
+ 'Template Type!\nCreate another Template instead !', |
+ }) |
+ |
+ def default_active(self, *args): |
+ ''' |
+ Default vale for Active >> True |
+ ''' |
+ return True |
+ |
+ def write(self, cursor, user, ids, values, context=None): |
+ ''' |
+ Super the write function to include an user error. |
+ ''' |
+ if 'model' in values: |
+ self.raise_user_error(cursor, 'modify_template', context=context,) |
+ return super(SubscriptionTemplate, self).write(cursor, user, |
+ ids, values, context=context |
+ ) |
+ |
+SubscriptionTemplate() |
+ |
+ |
+class SubscriptionTemplateFields(ModelSQL, ModelView): |
+ 'Subscription Template fields' |
+ _name = "subscription.template.defaults" |
+ _description = __doc__ |
+ _rec_name = 'field' |
+ |
+ template = fields.Many2One( |
+ 'subscription.template', |
+ 'Subscription Template', |
+ ondelete='CASCADE', |
+ ) |
+ field = fields.Many2One( |
+ 'ir.model.field', |
+ 'Field', |
+ domain=[ |
+ ( |
+ 'model', '=', Get(Eval('_parent_template', |
+ {}), 'model' |
+ ), |
+ ) |
+ ], |
+ required=True, |
+ ) |
+ value = fields.Selection([ |
+ ('false', 'False'), |
+ ('date', 'Current Date') |
+ ], 'Default Value', |
+ ) |
+ |
+SubscriptionTemplateFields() |
+ |
+ |
+class SubscriptionRule(ModelSQL, ModelView): |
+ 'Subscription Rule' |
+ _name = "subscription.rule" |
+ _description = __doc__ |
+ _inherits = {'ir.cron': 'cron_job'} |
+ |
+ cron_job = fields.Many2One('ir.cron', 'Cron Job', required=True) |
+ notes = fields.Text('Notes',) |
+ template = fields.Reference( |
+ 'Source template', |
+ required=True, |
+ selection='_get_templates', |
+ ) |
+ subscription_history_lines = fields.One2Many( |
+ 'subscription.history', |
+ 'subscription', |
+ 'Records Created', |
+ readonly=True, |
+ ) |
+ |
+ def __init__(self): |
+ super(SubscriptionRule, self).__init__() |
+ self._error_messages.update({ |
+ 'cant_delete' : "Subscriptions with existing lines can only be" |
+ " deactivated.\nNot deleted. ", |
+ }) |
+ |
+ def default_model(self, *args): |
+ ''' |
+ Sets the model as 'subscription.rule'. |
+ ''' |
+ return 'subscription.rule' |
+ |
+ def default_function(self, *args): |
+ ''' |
+ Sets the function to becalled by the scheduler as 'model_copy'. |
+ ''' |
+ return 'model_copy' |
+ |
+ def default_priority(self, *args): |
+ ''' |
+ Sets the default priority as 1.. |
+ ''' |
+ return 1 |
+ |
+ def default_first_date(self, cursor, user, context=None): |
+ ''' |
+ Default First Date >> Current date |
+ ''' |
+ date_obj = self.pool.get('ir.date') |
+ return date_obj.today(cursor, user, context=context) |
+ |
+ def create(self, cursor, user, vals, context=None): |
+ ''' |
+ Super the create function to include id of subscription in arguments. |
+ ''' |
+ create_id = super(SubscriptionRule, self).create(cursor, user, |
+ vals, context) |
+ self.write(cursor, user, create_id, { |
+ 'args': [[int(create_id)]], |
+ }, context) |
+ return create_id |
+ |
+ def delete(self, cursor, user, ids, context=None): |
+ ''' |
+ Super the delete function to restrict user from |
+ deleting a subscription . |
+ ''' |
+ if ids: |
+ history_lines = self.browse(cursor, user, ids[0], |
+ context).subscription_history_lines |
+ if history_lines: |
+ self.raise_user_error(cursor, 'cant_delete', context=context) |
+ else: |
+ super(SubscriptionRule, self).delete(cursor, user, ids, context) |
+ return True |
+ |
+ def _get_templates(self, cursor, user, context=None): |
+ ''' |
+ Only models in the template are available in the |
+ template field for setting duplication. |
+ |
+ :param cursor: Database cursor |
+ :param user: ID of current user |
+ :param context: Context from parent method.(no direct use) |
+ |
+ :param return: List of model id and model name |
+ ''' |
+ model_obj = self.pool.get('ir.model') |
+ template_obj = self.pool.get('subscription.template') |
+ ids = template_obj.search(cursor, user, [], context=context) |
+ model_ids = [] |
+ model_ids_list = template_obj.read(cursor, user, ids, ['model']) |
+ for model in model_ids_list : |
+ if model['model'] not in model_ids: |
+ model_ids.append(model['model']) |
+ doc_links = model_obj.browse(cursor, user, model_ids, |
+ context=context |
+ ) |
+ return [(link.model, link.name) for link in doc_links] |
+ |
+ def model_copy(self, cursor, user, ids, context=None): |
+ ''' |
+ This function is called from the cron being made for this subcription. |
+ This will duplicate this subscription for recurrant operations. |
+ |
+ :param cursor : Database Cursor |
+ :param user : ID of current user |
+ ;param ids : ID of current Database |
+ :param context : Context of parent method(No direct use) |
+ |
+ ;param return : Returns True |
+ ''' |
+ template_obj = self.pool.get('subscription.template') |
+ for row in self.read(cursor, user, ids, ['cron_job', |
+ 'id', 'template'], context): |
+ if not row.get('cron_job', False): |
+ continue |
+ (model_name, model_id) = row['template'].split(',') |
+ model_id = int(model_id) |
+ template_ids = template_obj.search(cursor, user, [ |
+ ('model.model', '=', model_name), |
+ ], context |
+ ) |
+ defaults = {} |
+ current_date = datetime.today() |
+ doc = template_obj.browse(cursor, user, template_ids, context)[0] |
+ for each_field in doc.fields: |
+ if each_field.value == 'date': |
+ defaults[each_field.field.name] = current_date |
+ else: |
+ defaults[each_field.field.name] = False |
+ duplicate_model_id = self.pool.get(model_name).copy( |
+ cursor, user, model_id, defaults, context |
+ ) |
+ history_vals = { |
+ 'subscription': row['id'], |
+ 'date': current_date, |
+ 'template': u"%s,%s" % (model_name, |
+ unicode(duplicate_model_id)) |
+ } |
+ self.pool.get('subscription.history').create(cursor, |
+ user, |
+ history_vals, |
+ context, |
+ ) |
+ return True |
+ |
+SubscriptionRule() |
+ |
+ |
+class SubscriptionHistory(ModelSQL, ModelView): |
+ 'Subscription history' |
+ _name = "subscription.history" |
+ _description = __doc__ |
+ _rec_name = 'date' |
+ |
+ date = fields.DateTime('Date',) |
+ subscription = fields.Many2One( |
+ 'subscription.rule', |
+ string='Subscription', |
+ ) |
+ template = fields.Reference( |
+ 'Source Template', |
+ required=True, |
+ selection='links_get' |
+ ) |
+ |
+ def links_get(self, cursor, user, context=None): |
+ ''' |
+ Gives the list of all the models installed in the database |
+ and creates the selection field for them. |
+ |
+ :param cursor: Database Cursor |
+ :param user: ID of current user |
+ :param context: Context from parent method.(no direct use) |
+ |
+ :param return: List of model id and model name |
+ ''' |
+ model_obj = self.pool.get('ir.model') |
+ ids = model_obj.search(cursor, user, [], context=context) |
+ subscription_links = model_obj.browse(cursor, user, ids, |
+ context=context) |
+ return [(link.model, link.name) for link in subscription_links] |
+ |
+SubscriptionHistory() |
+ |